Android JNI中記錄log

Android JNI中是有提供相關的接口來記錄log的,這樣的話,和java寫的代碼同樣,能夠直接在logcat中查看。若是代碼裏都是android提供的log api,一旦遇到新的需求,改起來會很麻煩,每一個地方都須要修改,因此說封裝android提供的log api是頗有必要的。
java


====android

android提供的經常使用api
api

__android_log_write(ANDROID_LOG_INFO, "tag here", "message here");
__android_log_print(ANDROID_LOG_INFO, "sometag", "test int = %d", testInt);


====code

如何使用?
orm

1. 包含頭文件接口

#include <android/log.h>it

2. 連接相關的庫文件io

android.mk須要加上引入庫form

LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llogclass


====

封裝

#include <stdio.h>
#include <stdarg.h>
#include <android/log.h>
#include <time.h>

static const char* TAG = "uninstall";

void kesyPrintf(const char *format, ...)
{
#ifdef KE_DEBUG
    char buf[2048] = "\0";
   
    va_list args;
    va_start(args,format);
    vsprintf(buf + strlen(buf), format, args);
    va_end(args);
    
     // 能夠添加功能,在這裏能夠把log記錄在文件中
     // -----

    __android_log_write(ANDROID_LOG_INFO, TAG, buf);
   
#endif
}

. KE_DEBUG是一個宏定義,做爲一個開關,只有定義了KE_DEBUG,纔會輸出log。能夠在代碼中定義,也能夠在mk文件中定義。

. 這樣就能夠像C語言中的printf那樣打印log了

kesyPrint("Hello world\n");

kesyPrint("abc---%s", "efg");

-------------------歡迎評頭品足-----by jacksonke

相關文章
相關標籤/搜索