欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 實現(xiàn)自己的LOG信息

 更新時間:2016年08月04日 18:09:38   投稿:lqh  
本文主要講解Android LOG,這里對如何創(chuàng)建自己的Android 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)方法,有需要的朋友看下。

相關(guān)文章

最新評論