- Home /
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\n", __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\n"); \
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 ?
Answer by TreasureKey · Nov 22, 2013 at 07:06 AM
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:
http://hojjatjafary.blogspot.ca/2013/01/c-plugin-debug-log.html
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!
I cant upvote this tag because I don't have enough score, but I have to confess that this link is very useful.
Your answer
Follow this Question
Related Questions
What's the difference between print() and Debug.Log() 1 Answer
Method contains unsupported native code ? 0 Answers
Can a prefab be written in code? 3 Answers
How do you get console messages in order? 0 Answers
Native debugging 0 Answers