Jaidev Ramakrishna
Jaidev Ramakrishna

Reputation: 87

How can logging to Crashlytics be done from C++ code on iOS?

The Crashlytics documentation for iOS only mentions custom logging from Swift and Objective-C. It mentions nothing about doing this from C++. On Android, we can use the log() and set() functions from the Crashlytics native header to do logging from C++ code called through the JNI. Is there an equivalent method for iOS? Is there any other way to do it?

Upvotes: 1

Views: 622

Answers (2)

Rémy Blanc
Rémy Blanc

Reputation: 313

Yep, I don't think there is a C-function for that but I had the problem some time ago and I used a function pointer. So here it is.

In the C scope (.cpp and .h file)

    void yourCFunction(void (*logFunc)(const char*) = NULL);

In the ObjectiveC scope:

CYourCClass *yourCClass = [...];
yourCClass->yourCFunction( [](const char* t){
    CLS_LOG(@"%s", t);
});

To call the log function in C++:

if (logFunc!=NULL) {
    std::string debugChr("Some message");
    (*logFunc)(debugChr.c_str());
}

This can be improved using macro and stuff but you have the idea.

Upvotes: 0

Todd Burner
Todd Burner

Reputation: 2262

Todd from Crashlytics here! Right now this is not possible without some sort of custom adapter on your end to go back to Swift or Objective-C. As C++ becomes more common in advanced apps I'd expect to see the team consider this :)

Upvotes: 2

Related Questions