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

相关文章
相关标签/搜索