Simple Logging for Android & iPhone

来源:互联网 发布:知乎 日本手工皮具 编辑:程序博客网 时间:2024/06/06 14:28

Simple Logging for Android & iPhone

http://codingincircles.com/2010/12/simple-logging-for-android-iphone/

Dec 18, 2010
Logging is one of those things I don’t want to implement until I get an error somewhere and I’d rather not try to track it down with a debugger. This happens very quickly when dealing with XCode’s included and rather lacking debugger, or on Android’s command-line-only native code debugger.
So, for my and public domain usage: A simple logging macro


Source code   
#ifndef REMOVE_LOGGING
#ifdef ANDROID
#define LOG_DEBUG(message, ...) \
    do { __android_log_print(ANDROID_LOG_DEBUG, "Terrasweeper", "[%s:%d] " message, __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
#else
#define LOG_DEBUG(message, ...) \
    do { std::printf("[%s:%d] " message "\n", __FILE__, __LINE__,  ##__VA_ARGS__); } while(0)
#endif
#else
#define LOG_DEBUG(message, ...) do { LOG_UNUSED(message); } while (0)
#endif


As you can see, it supports Windows, Unix and iPhone through stdout, and android through the custom logging that you can retrieve with the LogCat tool

Posted by Tatham | Categories: Blog |



Log to logcat.

1) To invoke the logger in native code include the header and call _android_log_write(..).

#include <android/log.h>__android_log_write(ANDROID_LOG_INFO, "tag here", "message here");

2) In your Android.mk file include the log lib like this.

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 

0 0