Search code examples
crashlyticsgoogle-fabric

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?


Solution

  • 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.