一 /bionic/libc/include/unistd.h 添加
void clogi(const char* method, const char* info);
void cloge(const char* method, const char* error);
二 bionic/libc/async_safe/include/async_safe/log.h添加
#define YOOHA_TAG "YoohaLogC"
#define CLOGI(...) async_safe_format_log(ANDROID_LOG_INFO, YOOHA_TAG, __VA_ARGS__)
#define CLOGE(...) async_safe_format_log(ANDROID_LOG_ERROR, YOOHA_TAG, __VA_ARGS__)
三 创建/bionic/libc/bionic/clog.cpp文件
#include <unistd.h>
#include <async_safe/log.h>
#include <string.h>
#include <stdio.h>
void clogi(const char* method, const char* info){
int pid = getpid();
if (pid < 2000){
return;
}
char sPid[16] = {0};
sprintf(sPid, "\"PID\":\"%d\"", pid);
char sinfo[1024] = {0};
int lenInfo = strlen(info);
if (lenInfo > 1023){
strncpy(sinfo, info, 1023);
sinfo[1022] = 0;
}else{
strcpy(sinfo, info);
sinfo[lenInfo - 1] = 0;
}
CLOGI("{\"Type\":\"NativeLog\",%s,\"Info\":\"<%s>:%s\"}", sPid, method, sinfo);
}
void cloge(const char* method, const char* error){
int pid = getpid();
if (pid < 2000){
return;
}
char sPid[16] = {0};
sprintf(sPid, "\"PID\":\"%d\"", pid);
char sError[1024] = {0};
int lenError = strlen(error);
if (lenError > 1023){
strncpy(sError, error, 1023);
sError[1022] = 0;
}else{
strcpy(sError, error);
sError[lenError - 1] = 0;
}
CLOGE("{\"Type\":\"NativeLog\",%s,\"Info\":\"<%s>:%s\"}", sPid, method, sError);
}
四 添加编译选项 /bionic/libc/Android.bp 添加
cc_library_static {
defaults: ["libc_defaults"],
srcs: [
...
"bionic/clog.cpp", // add start
...
]
}
五 使用
#include <unistd.h>
clogi("func", "test test");