最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年08月04日 18:09:38   投稿:lqh  
本文主要講解Android LOG,這里對(duì)如何創(chuàng)建自己的Android LOG信息做了詳細(xì)的介紹,并附簡(jiǎn)單代碼示例,有需要的小伙伴可以參考下

       在程序開發(fā)過(guò)程中,LOG是廣泛使用的用來(lái)記錄程序執(zhí)行過(guò)程的機(jī)制,它既可以用于程序調(diào)試,也可以用于產(chǎn)品運(yùn)營(yíng)中的事件記錄。在Android系統(tǒng)中,提供了簡(jiǎn)單、便利的LOG機(jī)制,開發(fā)人員可以方便地使用。在這一篇文章中,我們簡(jiǎn)單介紹在Android內(nèi)核空間和用戶空間中LOG的使用和查看方法。

        一. 內(nèi)核開發(fā)時(shí)LOG的使用。Android內(nèi)核是基于Linux Kerne 2.36的,因此,Linux Kernel的LOG機(jī)制同樣適合于Android內(nèi)核,它就是有名的printk,與C語(yǔ)言的printf齊名。與printf類似,printk提供格式化輸入功能,同時(shí),它也具有所有LOG機(jī)制的特點(diǎn)--提供日志級(jí)別過(guò)慮功能。printk提供了8種日志級(jí)別(<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表示日志級(jí)別,后面緊跟著要格式化字符串。

       在Android系統(tǒng)中,printk輸出的日志信息保存在/proc/kmsg中,要查看/proc/kmsg的內(nèi)容,參照Android內(nèi)核源碼 在Ubuntu上下載,編譯,安裝一文,在后臺(tái)中運(yùn)行模擬器:

       USER-NAME@MACHINE-NAME:~/Android$ emulator &

       啟動(dòng)adb shell工具:

       USER-NAME@MACHINE-NAME:~/Android$ adb shell

       查看/proc/kmsg文件:

       root@android:/ # cat  /proc/kmsg

        二. 用戶空間程序開發(fā)時(shí)LOG的使用。Android系統(tǒng)在用戶空間中提供了輕量級(jí)的logger日志系統(tǒng),它是在內(nèi)核中實(shí)現(xiàn)的一種設(shè)備驅(qū)動(dòng),與用戶空間的logcat工具配合使用能夠方便地跟蹤調(diào)試程序。在Android系統(tǒng)中,分別為C/C++ 和Java語(yǔ)言提供兩種不同的logger訪問(wèn)接口。C/C++日志接口一般是在編寫硬件抽象層模塊或者編寫JNI方法時(shí)使用,而Java接口一般是在應(yīng)用層編寫APP時(shí)使用。

       Android系統(tǒng)中的C/C++日志接口是通過(guò)宏來(lái)使用的。在system/core/include/android/log.h定義了日志的級(jí)別:

/* 
 * 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中,定義了對(duì)應(yīng)的宏,如對(duì)應(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.");

           再來(lái)看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)境下運(yùn)行模擬器,并且安裝了Android插件,那么,很簡(jiǎn)單,直接在Eclipse就可以查看了:

 

  如果是在自己編譯的Android源代碼工程中使用,則在后臺(tái)中運(yùn)行模擬器:

       USER-NAME@MACHINE-NAME:~/Android$ emulator &

       啟動(dòng)adb shell工具:

       USER-NAME@MACHINE-NAME:~/Android$ adb shell

       使用logcat命令查看日志:

       root@android:/ # logcat

       這樣就可以看到輸出的日志了。

 以上就是Android 自己的LOG信息實(shí)現(xiàn)方法,有需要的朋友看下。

相關(guān)文章

  • Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法

    Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法,較為詳細(xì)的分析了Android短信與Notification的原理及對(duì)應(yīng)的設(shè)置取消技巧,需要的朋友可以參考下
    2015-12-12
  • Android 修改adb端口的方法

    Android 修改adb端口的方法

    今天小編就為大家分享一篇Android 修改adb端口的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • android6.0運(yùn)行時(shí)權(quán)限完美封裝方法

    android6.0運(yùn)行時(shí)權(quán)限完美封裝方法

    今天小編就為大家分享一篇android6.0運(yùn)行時(shí)權(quán)限完美封裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Android使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回示例

    Android使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回示例

    這篇文章主要介紹了Android使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Android應(yīng)用閃屏頁(yè)延遲跳轉(zhuǎn)的三種寫法

    Android應(yīng)用閃屏頁(yè)延遲跳轉(zhuǎn)的三種寫法

    這篇文章主要介紹了 Android應(yīng)用閃屏頁(yè)延遲跳轉(zhuǎn)的三種寫法,需要的朋友可以參考下
    2017-03-03
  • Android中的WebView詳細(xì)介紹

    Android中的WebView詳細(xì)介紹

    這篇文章主要介紹了Android中的WebView詳細(xì)介紹,本文講解了WebView的概念、使用方法、各種使用實(shí)例等,需要的朋友可以參考下
    2015-06-06
  • Android?Jetpack庫(kù)重要組件WorkManager的使用

    Android?Jetpack庫(kù)重要組件WorkManager的使用

    WorkManager是Android?Jetpack的一個(gè)強(qiáng)大的組件,用于處理后臺(tái)耗時(shí)任務(wù)。后臺(tái)任務(wù)可以是一次性的,也可以是重復(fù)的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)

    Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要為大家介紹了Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)的圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼

    Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android使用Activity實(shí)現(xiàn)簡(jiǎn)單的可輸入對(duì)話框

    Android使用Activity實(shí)現(xiàn)簡(jiǎn)單的可輸入對(duì)話框

    大家在做彈出對(duì)話框效果的時(shí)候最容易想到的是用Dialog顯示,但其實(shí)彈出對(duì)話框的實(shí)現(xiàn)效果有兩種:Dialog和Activity,那么下面這篇文章就來(lái)給大家介紹了關(guān)于Android使用Activity如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的可輸入對(duì)話框的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-10-10

最新評(píng)論

托里县| 临朐县| 老河口市| 西华县| 浦县| 革吉县| 丹凤县| 梨树县| 龙南县| 开封市| 通辽市| 襄城县| 龙陵县| 绥宁县| 张北县| 东海县| 体育| 泸溪县| 苏州市| 轮台县| 天津市| 余姚市| 山东省| 松阳县| 亚东县| 延川县| 英超| 岑巩县| 伊宁市| 沅陵县| 鄱阳县| 公主岭市| 林州市| 诸暨市| 嘉祥县| 汉中市| 滁州市| 静乐县| 安龙县| 乐安县| 鄄城县|