Android 實現(xiàn)自己的LOG信息
在程序開發(fā)過程中,LOG是廣泛使用的用來記錄程序執(zhí)行過程的機制,它既可以用于程序調(diào)試,也可以用于產(chǎn)品運營中的事件記錄。在Android系統(tǒng)中,提供了簡單、便利的LOG機制,開發(fā)人員可以方便地使用。在這一篇文章中,我們簡單介紹在Android內(nèi)核空間和用戶空間中LOG的使用和查看方法。
一. 內(nèi)核開發(fā)時LOG的使用。Android內(nèi)核是基于Linux Kerne 2.36的,因此,Linux Kernel的LOG機制同樣適合于Android內(nèi)核,它就是有名的printk,與C語言的printf齊名。與printf類似,printk提供格式化輸入功能,同時,它也具有所有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的使用方法:
printk(KERN_ALERT"This is the log printed by printk in linux kernel space.");
KERN_ALERT表示日志級別,后面緊跟著要格式化字符串。
在Android系統(tǒng)中,printk輸出的日志信息保存在/proc/kmsg中,要查看/proc/kmsg的內(nèi)容,參照Android內(nèi)核源碼 在Ubuntu上下載,編譯,安裝一文,在后臺中運行模擬器:
USER-NAME@MACHINE-NAME:~/Android$ emulator &
啟動adb shell工具:
USER-NAME@MACHINE-NAME:~/Android$ adb shell
查看/proc/kmsg文件:
root@android:/ # cat /proc/kmsg
二. 用戶空間程序開發(fā)時LOG的使用。Android系統(tǒng)在用戶空間中提供了輕量級的logger日志系統(tǒng),它是在內(nèi)核中實現(xiàn)的一種設(shè)備驅(qū)動,與用戶空間的logcat工具配合使用能夠方便地跟蹤調(diào)試程序。在Android系統(tǒng)中,分別為C/C++ 和Java語言提供兩種不同的logger訪問接口。C/C++日志接口一般是在編寫硬件抽象層模塊或者編寫JNI方法時使用,而Java接口一般是在應(yīng)用層編寫APP時使用。
Android系統(tǒng)中的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中,定義了對應(yīng)的宏,如對應(yīng)于ANDROID_LOG_VERBOSE的宏LOGV:
/* * This is the local tag used for the following simplified * logging macros. You can change this preprocessor definition * before using the other macros to change the tag. */ #ifndef LOG_TAG #define LOG_TAG NULL #endif /* * Simplified macro to send a verbose log message using the current LOG_TAG. */ #ifndef LOGV #if LOG_NDEBUG #define LOGV(...) ((void)0) #else #define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) #endif #endif /* * Basic log message macro. * * Example: * LOG(LOG_WARN, NULL, "Failed with error %d", errno); * * The second argument may be NULL or "" to indicate the "global" tag. */ #ifndef LOG #define LOG(priority, tag, ...) \ LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) #endif /* * Log macro that allows you to specify a number for priority. */ #ifndef LOG_PRI #define LOG_PRI(priority, tag, ...) \ android_printLog(priority, tag, __VA_ARGS__) #endif /* * ================================================================ * * The stuff in the rest of this file should not be used directly. */ #define android_printLog(prio, tag, fmt...) \ __android_log_print(prio, tag, fmt)
因此,如果要使用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系統(tǒng)中的Java日志接口。Android系統(tǒng)在Frameworks層中定義了Log接口:
(frameworks/base/core/java/android/util/Log.java):
................................................ public final class Log { ................................................ /** * Priority constant for the println method; use Log.v. */ public static final int VERBOSE = 2; /** * Priority constant for the println method; use Log.d. */ public static final int DEBUG = 3; /** * Priority constant for the println method; use Log.i. */ public static final int INFO = 4; /** * Priority constant for the println method; use Log.w. */ public static final int WARN = 5; /** * Priority constant for the println method; use Log.e. */ public static final int ERROR = 6; /** * Priority constant for the println method. */ public static final int ASSERT = 7; ..................................................... public static int v(String tag, String msg) { return println_native(LOG_ID_MAIN, VERBOSE, tag, msg); } public static int v(String tag, String msg, Throwable tr) { return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr)); } public static int d(String tag, String msg) { return println_native(LOG_ID_MAIN, DEBUG, tag, msg); } public static int d(String tag, String msg, Throwable tr) { return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr)); } public static int i(String tag, String msg) { return println_native(LOG_ID_MAIN, INFO, tag, msg); } public static int i(String tag, String msg, Throwable tr) { return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr)); } public static int w(String tag, String msg) { return println_native(LOG_ID_MAIN, WARN, tag, msg); } public static int w(String tag, String msg, Throwable tr) { return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr)); } public static int w(String tag, Throwable tr) { return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr)); } public static int e(String tag, String msg) { return println_native(LOG_ID_MAIN, ERROR, tag, msg); } public static int e(String tag, String msg, Throwable tr) { return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr)); } ..................................................................
因此,如果要使用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工具。如果是在Eclipse環(huán)境下運行模擬器,并且安裝了Android插件,那么,很簡單,直接在Eclipse就可以查看了:
如果是在自己編譯的Android源代碼工程中使用,則在后臺中運行模擬器:
USER-NAME@MACHINE-NAME:~/Android$ emulator &
啟動adb shell工具:
USER-NAME@MACHINE-NAME:~/Android$ adb shell
使用logcat命令查看日志:
root@android:/ # logcat
這樣就可以看到輸出的日志了。
以上就是Android 自己的LOG信息實現(xiàn)方法,有需要的朋友看下。
- Android開發(fā)筆記之:Log圖文詳解(Log.v,Log.d,Log.i,Log.w,Log.e)
- Android系統(tǒng)開發(fā)中l(wèi)og的使用方法及簡單的原理
- android輕松管理安卓應(yīng)用中的log日志 發(fā)布應(yīng)用時log日志全部去掉的方法
- android雜記:C++文件的添加log方法分享
- Android將應(yīng)用調(diào)試log信息保存在SD卡的方法
- Android編程之基于Log演示一個activity生命周期實例詳解
- microlog4android將Android Log日志寫到SD卡文件中實現(xiàn)方法
- Android 日志工具(log)的使用方法
- Android開發(fā)實現(xiàn)的Log統(tǒng)一管理類
相關(guān)文章
Android編程實現(xiàn)音量按鈕添加監(jiān)聽事件的方法
這篇文章主要介紹了Android編程實現(xiàn)音量按鈕添加監(jiān)聽事件的方法,結(jié)合實例形式分析了Android事件監(jiān)聽實現(xiàn)音量控制的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Android 虛擬機中的內(nèi)存分配與OOM問題詳解
這篇文章主要為大家介紹了Android 虛擬機中的內(nèi)存分配與OOM問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例
這篇文章主要介紹了Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-11-11RecyclerView+CardView實現(xiàn)橫向卡片式滑動效果
這篇文章主要為大家詳細介紹了RecyclerView+CardView實現(xiàn)橫向卡片式滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android應(yīng)用開發(fā)中自定義ViewGroup的究極攻略
這里我們要演示的自定義ViewGroup中將實現(xiàn)多種方式排列和滑動等效果,并且涵蓋子View之間Touch Event的攔截與處理等問題,完全干貨,下面就為大家送上Android應(yīng)用開發(fā)中自定義ViewGroup的究極實例攻略2016-05-05XListView實現(xiàn)下拉刷新和上拉加載原理解析
這篇文章主要為大家解析了XListView實現(xiàn)下拉刷新和上拉加載原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android的廣播Receiver動態(tài)注冊和靜態(tài)注冊示例
本篇文章主要介紹了Android的廣播Receiver動態(tài)注冊和靜態(tài)注冊示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02