首頁 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當貝市場

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 11767|回復(fù): 0
上一主題 下一主題
[教程]

淺談Android系統(tǒng)開發(fā)中LOG的使用

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:30 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
在程序開發(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>):   
  1. #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                 */
復(fù)制代碼
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)容,參照在Ubuntu上下載、編譯和安裝Android最新內(nèi)核源代碼(Linux Kernel)一文,在后臺中運行模擬器:   
   
       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定義了日志的級別:   
  1. /*   
    * 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;
復(fù)制代碼
在system/core/include/cutils/log.h中,定義了對應(yīng)的宏,如對應(yīng)于ANDROID_LOG_VERBOSE的宏LOGV:   
  1. /*   
    * 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)
復(fù)制代碼
因此,如果要使用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):   
  1. ................................................   
       
    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 +
  2. + 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 +
  3. + 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 +
  4. + 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 +
  5. + 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 +
  6. + getStackTraceString(tr));   
            }   
       
    ..................................................................   
       
            /**@hide */ public static native int println_native(int bufID,   
                    int priority, String tag, String msg);   
    }
復(fù)制代碼
因此,如果要使用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   
       這樣就可以看到輸出的日志了

上一篇:制作可獨立分發(fā)的Android模擬器
下一篇:Android入門教程(四)之------Android工程目錄結(jié)構(gòu)介紹
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-19 20:41 , Processed in 0.065166 second(s), 17 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表