1. 程式人生 > >Android:Android NDK專案中C++檔案中列印日誌

Android:Android NDK專案中C++檔案中列印日誌

Android NDK專案中C++檔案中列印日誌

工作需要,在NDK專案中的C++檔案中列印日誌,還是費了點勁查詢 才達到自己 想要的效果了。

步驟

新增標頭檔案

新增標頭檔案 定義巨集函式

#include <android/log.h>

//這2行可以不用新增,是博主 特定需求
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>

//"C_TAG"
//static const char* _CTagHead(char* file,const char* fun,char* line)
//{
//    int len = strlen(file) + strlen(fun);
//    char *buf = new char[64]{0};
//    sprintf(buf,"%s:%s",file,fun);
//    return buf;
//}

//#define LOG_TAG _CTagHead(__FILE__,__func__)
#define LOG_TAG "C_TAG"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)

如何使用

這是博主的呼叫處

    sprintf(buf,"bl_id=%s,height=%s",string_tools::pod_to_hex(bl_id).c_str() ,heightStr);
    LOGD( "%s",buf );