BLOG ARTICLE NDK log | 1 ARTICLE FOUND

  1. 2014.09.25 Android, NDK Log using __android_log

1. android/log.h

  In ndk library provide log functions that can be seen in LogCat. First you have to include <android/log.h>.

#include <android/log.h>

  You can use two functions [__android_log_write] and [__android_log_vprint]. If you want to write log like printf, Use [__android_log_vprint]. It support parameters like %d, %f, %s.etc else  [__android_log_write] only send a message. Below show example using  [__android_log_write] and [__android_log_vprint]

__android_log_write(ANDROID_LOG_INFO,	LOG_TAG, msg.c_str());
__android_log_vprint(ANDROID_LOG_INFO,	LOG_TAG, msg.c_str(), ap);


2. Tag

  Android Log have tags 

    ANDROID_LOG_UNKNOWN ,

    ANDROID_LOG_DEFAULT,

    ANDROID_LOG_VERBOSE,

    ANDROID_LOG_DEBUG,

    ANDROID_LOG_INFO,

    ANDROID_LOG_WARN,

    ANDROID_LOG_ERROR,

    ANDROID_LOG_FATAL,

    ANDROID_LOG_SILENT.

  we usually use INFO, WARN, ERROR, VERBOSE. DEBUG. LogCat in eclipse IDE show you message according to each tag.



3. Wrapping

  I recommend wrapping log functions for more clear and useful. 

class Log
{
public:
	static void info(string msg)
	{
#if defined(LOG) && !defined(PUBLISH)
		__android_log_write(ANDROID_LOG_INFO,	LOG_TAG, msg.c_str());
#endif
	}

	static void info_print(string msg, ...)
	{
#if defined(LOG) && !defined(PUBLISH)
		va_list ap;
		va_start(ap,msg);
		__android_log_vprint(ANDROID_LOG_INFO,	LOG_TAG, msg.c_str(), ap);
		va_end(ap);
#endif
	}

	static void debug(string msg)
	{
#if defined(LOG) && !defined(PUBLISH)
		__android_log_write(ANDROID_LOG_DEBUG,	LOG_TAG, msg.c_str());
#endif
	}

	static void debug_print(string msg, ...)
	{
#if defined(LOG) && !defined(PUBLISH)
		va_list ap;
		va_start(ap,msg);
		__android_log_vprint(ANDROID_LOG_DEBUG,	LOG_TAG, msg.c_str(), ap);
		va_end(ap);
#endif
	}


	static void warning(string msg)
	{
#if defined(LOG) && !defined(PUBLISH)
		__android_log_write(ANDROID_LOG_WARN,	LOG_TAG, msg.c_str());
#endif
	}

	static void warning_print(string msg, ...)
	{
#if defined(LOG) && !defined(PUBLISH)
		va_list ap;
		va_start(ap,msg);
		__android_log_vprint(ANDROID_LOG_WARN,	LOG_TAG, msg.c_str(), ap);
		va_end(ap);
#endif
	}

	static void error(string msg)
	{
#if defined(LOG) && !defined(PUBLISH)
		__android_log_write(ANDROID_LOG_ERROR,	LOG_TAG, msg.c_str());
#endif
	}

	static void error_print(string msg, ...)
	{
#if defined(LOG) && !defined(PUBLISH)
		va_list ap;
		va_start(ap,msg);
		__android_log_vprint(ANDROID_LOG_ERROR,	LOG_TAG, msg.c_str(), ap);
		va_end(ap);
#endif
	}

};

  I made 4 kind methods using tags is ERROR, WARN, INFO and DEBUG, and if publish mode, I don't write a log to improve performance. I used this methods like below code.

Log::info_print("GL_EXTENSIONS : %s",glGetString(GL_EXTENSIONS));
Log::info_print("GL_VENDOR : %s",glGetString(GL_VENDOR));
Log::info_print("GL_RENDERER : %s",glGetString(GL_RENDERER));
Log::info_print("GL_VERSION : %s",glGetString(GL_VERSION));
Log::info("GLManager::jdGLInit end");

  In OpenGL Dirary app, you can see this logs.