How do you print to the console/log from native code on OSX ?

Hi,

I have written some native code unity plugins and need to print debugging info to the console/logs.

On android, for debug logging I used:

#define DEBUG_LOG_FMT(fmt, ...)	\
    do { \
    static char buf[4096]; \
    sprintf(buf, fmt, ##__VA_ARGS__); \
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "%s(%d): %s

", FILE, LINE, buf);
} while (0)

and my debug info showed up in the unity logs.

Now I am testing my plugin for OSX and for debug logging I am using:

#define DEBUG_LOG_FMT(fmt, ...)	\
    do { \
    static char buf[4096]; \
    sprintf(buf, fmt, ##__VA_ARGS__); \
    std::string str("%s(%d): %s

");
printf(str.c_str(), FILE, LINE, buf);
} while (0)

but my debug info does not show up in the unity logs / console.

How do you print to the unity console from native code on OSX and get it to show up in the logs ?

The best answer I was able to find involved writing a callback on the Unity script side and a function pointer on the plugin/native code side. This enables your plugin to call a unity script function like Debug.Log.

An example of it can be found here:

Oh and I ended up changing my native code debug logging macro to:

#define DEBUG_LOG_FMT(fmt, ...)	\
    do { \
    static char buf[4096]; \
    sprintf(buf, fmt, ##__VA_ARGS__); \
    DebugLog(buf); \
    } while (0)

Now I can log once again!