log的使用

来源:互联网 发布:c语言论坛 编辑:程序博客网 时间:2024/06/07 06:45

1.内核开发log的使用printk

8种日志级别(<linux/kernel.h>)

#define KERN_EMERG "<0>" /* system is unusable */

#define KERN_ALERT "<1>"/* action must be taken immediately*/
#define KERN_CRIT "<2>"/* critical conditions*/
#deinfe KERN_ERR "<3>"/* error conditions*/
#deinfe KERN_WARNING "<4>" /* warning conditions*/
#deinfe KERN_NOTICE "<5>" /* normal but significant condition*/
#deinfe KERN_INFO "<6>"/* informational*/
#deinfe KERN_DEBUG "<7>"/* debug-level messages*/

使用方法:


       printk(KERN_ALERT"This is the log printed by printk in linux kernel space.");

       KERN_ALERT表示日志级别,后面紧跟着要格式化字符串。

在Android系统中,printk输出的日志信息保存在/proc/kmsg中

2.用户空间程序开发时LOG的使用

Android系统中的C/C++日志接口是通过宏来使用的。在system/core/include/android/log.h定义了日志的级别

/*
 * Android log priority values, in ascending priority order.
 */
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT,/* only for SetMinPriority() */
ANDROID_LOG_VERBOSE, // 输出价值比较低的信息
ANDROID_LOG_DEBUG, // 打印输出调试信息,这种信息在软件发行后作用不大,因此,发行版本中不会输出
ANDROID_LOG_INFO, // 输出打印软件运行时一般性的提示信息,例如,软件运行状态等
ANDROID_LOG_WARN, // 用来输出警告信息,提示软件遇到一些可疑地,或未预料的情况
ANDROID_LOG_ERROR, // 输出软件运行时发生的错误,例如,抛出的例外。
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT,/* only for SetMinPriority(); must be last */
} android_LogPriority;

在system/core/include/cutils/log.h中,定义了对应的宏,如对应于ANDROID_LOG_VERBOSE的宏LOGV:

因此,如果要使用C/C++日志接口,只要定义自己的LOG_TAG宏和包含头文件system/core/include/cutils/log.h就可以了:


         #define LOG_TAG "MY LOG TAG"

         #include <cutils/log.h>

         就可以了,例如使用LOGV:

         LOGV("This is the log printed by LOGV in android user space.");


再来看Android系统中的Java日志接口。Android系统在Frameworks层中定义了Log接口(frameworks/base/core/java/android/util/Log.java):

 因此,如果要使用Java日志接口,只要在类中定义的LOG_TAG常量和引用android.util.Log就可以了:

        private static final String LOG_TAG = "MY_LOG_TAG";

        Log.i(LOG_TAG, "This is the log printed by Log.i in android user space.");

        要查看这些LOG的输出,可以配合logcat工具。

adb shell 

logcat

log量巨大,因此Android 把log输出到了不同的缓冲区。目前定义了四个log缓冲区:

1.radio: 输出通信系统的log

2.system:输出系统组件的log

3. event: 输出event模块的log

4. main: 所有java层的log,以及不属于上面三层的log。

了解一个类最好的办法,就是看一看这个类提供了哪些公共变量和函数

JNI编程-- undefined reference to `__android_log_print' 的解决办法

按如下步骤操作:

1、在Android.mk 文件中找到

include $(CLEAR_VARS)  这一行,

在下面增加一行:

LOCAL_LDLIBS    := -lm -llog 


2、文件头部引入:

#include <android/log.h>

3、宏定义

#define LOG_TAG "Native"

#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)



原创粉丝点击