Android Java層,Native層,Lib層打印Log簡介【轉】


本文轉載自:https://blog.csdn.net/AndroidMage/article/details/52225068

說明: 這里我根據個人工作情況說明在各個層打印log。如有問題歡迎拍磚。

1. Java層打印log。
這個比較簡單Android有封裝好的Log.java, 可以使用。
例如:

Log.d(String Tag, String msg);

個人習慣: 單獨寫一個類包裝一下:MLog.java:

import android.util.Log;

public class MLog {
public final static boolean DEBUG = true;

public static void d(String tag, String msg) {
if (DEBUG) {
Log.d(tag, msg);
}
}
...
}
1
2
3
4
5
6
7
8
9
10
11
12
2. Native 層打印log。
網絡上很多資料:http://stackoverflow.com/questions/12159316/logging-values-of-variables-in-android-native-ndk

步驟如下:

Android.mk 中添加:

LOCAL_LDLIBS :=-llog

在需要打印的log的地方使用:
#include <android/log.h>
#define TAG "DroidMage" // 這個是自定義的LOG的標識
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) // 定義LOGD類型

xxx_function() {

LOGD("%s hello world", "MSG");
}
1
2
3
4
5
6
7
8
9
NOTE: 這只是對__android_log_print() 函數做了包裝而已 原型如下:

/**
* int __android_log_print(int prio, const char *tag, const char *fmt, ...)
*
* ANDROID_LOG_DEBUG : 是在頭文件定義的log level,類似與java層Log.d()打印Debug信息。
* tag : char * 類型的字符串。
* format : char* 格式化的字符串
*/
// 例子:
char * tag = "DroidMage";
char * format = "%s";
char * msg = "This is for test!";
__android_log_print(ANDROID_LOG_DEBUG, tag, format, msg);
1
2
3
4
5
6
7
8
9
10
11
12
13
Android 中有 < cutils/log.h> 做好了封裝:

#define LOG_TAG DroidMage //一定要定義,原因是< log/log.h> 做了封裝,沒有定義則未NULL。

xxx_func() {
//log 的Tag就是所定義LOG_TAG
LOGD("%s", msg);
}
1
2
3
4
5
6
7
3. bionic層打印log(筆者做過系統開發的工作,有時需要調試lib層)
參考源碼:”/bionic/libc/bionic/private/libc_logging.cpp”

#include "private/libc_logging.h"


xxx_function() {

//ANDROID_LOG_DEBUG: int 類型, 定義log level, 參考"/bionic/libc/bionic/private/libc_logging.h" 定義。
__libc_format_log(ANDROID_LOG_DEBUG, tag, "%s", msg);
}
1
2
3
4
5
6
7
8
9
未完待續。。。
---------------------
作者:機器魔法師
來源:CSDN
原文:https://blog.csdn.net/AndroidMage/article/details/52225068
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM