System

Elegant Logging in Android NDK Development

Elegant Logging in Android NDK Development

Park

Park

System

Written on

Share
Elegant Logging in Android NDK Development

Audio and video development inevitably requires Android NDK debugging, and logging is a frequent need. Here is a summary of how to log elegantly in Android NDK development:

Header File


#include <android/log.h> 

Log Levels

/** For internal use only.  */
ANDROID_LOG_UNKNOWN = 0,
/** The default priority, for internal use only.  */
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
/** Verbose logging. Should typically be disabled for a release apk. */
ANDROID_LOG_VERBOSE,
/** Debug logging. Should typically be disabled for a release apk. */
ANDROID_LOG_DEBUG,
/** Informational logging. Should typically be disabled for a release apk. */
ANDROID_LOG_INFO,
/** Warning logging. For use with recoverable failures. */
ANDROID_LOG_WARN,
/** Error logging. For use with unrecoverable failures. */
ANDROID_LOG_ERROR,
/** Fatal logging. For use when aborting. */
ANDROID_LOG_FATAL,
/** For internal use only.  */
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */

Usage

Basic usage:

#include <android/log.h> 
__android_log_print(ANDROID_LOG_INFO, "log_tag", "this is log:%d", val);

A more elegant approach: macro wrappers:

#include <android/log.h>

#define TAG "Your Log Tag" 
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) 	// 定义LOGD类型
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) 		// 定义LOGI类型 
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__) 		// 定义LOGW类型 
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) 	// 定义LOGE类型 
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__) 	// 定义LOGF类型

Usage:

LOGI("This is info log!"); 
LOGD("This is debug log!"); 
LOGW("This is warn log!"); 
LOGE("This is error log!"); 

References

@See https://developer.android.com/ndk/reference/group/logging