java和c混合开发调试的时候,我们希望c代码也能打印logcat,这样代码跑一遍就能看到完整的调用流程,调试方便很多。
模仿android.util.Log,根据优先级打印,代码如下:
#ifndef __DLOG_H_
#define __DLOG_H_
#include
#include
#undef LOG_TAG
#define LOG_TAG "DLOG"
#define DLOGD(fmt,...) \
do { \
printf(fmt,##__VA_ARGS__); \
ALOGD(fmt,##__VA_ARGS__); \
} \
while (0)
#define DLOGV(fmt,...) \
do { \
printf(fmt,##__VA_ARGS__); \
ALOGV(fmt,##__VA_ARGS__); \
} \
while (0)
#define DLOGI(fmt,...) \
do { \
printf(fmt,##__VA_ARGS__); \
ALOGI(fmt,##__VA_ARGS__); \
} \
while (0)
#define DLOGW(fmt,...) \
do { \
printf(fmt,##__VA_ARGS__); \
ALOGW(fmt,##__VA_ARGS__); \
} \
while (0)
#define DLOGE(fmt,...) \
do { \
printf(fmt,##__VA_ARGS__); \
ALOGE(fmt,##__VA_ARGS__); \
} \
while (0)
#endif
printf是标准输出,ALOG…是啥呢,接下来分析代码。
system/core/include/log/log.h
#ifndef ALOGD
#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#endif
#ifndef ALOG
#define ALOG(priority, tag, ...) \
LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
#endif
#ifndef LOG_PRI
#define LOG_PRI(priority, tag, ...) \
android_printLog(priority, tag, __VA_ARGS__)
#endif
#define android_printLog(prio, tag, fmt...) \
__android_log_print(prio, tag, fmt)
从上往下看,好几层宏定义,转到了__android_log_print。
__android_log_print函数:
system/core/liblog/logger_write.c
LIBLOG_ABI_PUBLIC int __android_log_print(int prio, const char *tag,
const char *fmt, ...)
{
va_list ap;
char buf[LOG_BUF_SIZE];
va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
va_end(ap);
return __android_log_write(prio, tag, buf);
}
LIBLOG_ABI_PUBLIC int __android_log_write(int prio, const char *tag,
const char *msg)
{
return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
}
最后调用__android_log_buf_write。
2.java代码如何打印import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "hello ");//这里打印日志
}
}
打印日志调用了android.util.Log类里面的Log.d方法,我们看下它是怎么实现的。
frameworks/base/core/java/android/util/Log.java
public static final int DEBUG = 3; //优先级
public static final int LOG_ID_MAIN = 0; //main类型
public static int d(String tag, String msg) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
}
//调用native方法
public static native int println_native(int bufID,
int priority, String tag, String msg);
frameworks//base/core/jni/android_util_Log.cpp
//这个方法是println_native的jni实现,这里只是做了一些参数检查,然后继续往下调用__android_log_buf_write
#include
static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
jint bufID, jint priority, jstring tagObj, jstring msgObj)
{
const char* tag = NULL;
const char* msg = NULL;
if (msgObj == NULL) {
if (tagObj != NULL) {
const char* tag1 = env->GetStringUTFChars(tagObj, NULL);
const char* tag2 = "SetupWizard.CheckFrpFragment";
if (strncmp(tag1, tag2, 28) == 0) {
return 0;
}
}
jniThrowNullPointerException(env, "println needs a message");
return -1;
}
if (bufID = LOG_ID_MAX) {
jniThrowNullPointerException(env, "bad bufID");
return -1;
}
if (tagObj != NULL)
tag = env->GetStringUTFChars(tagObj, NULL);
msg = env->GetStringUTFChars(msgObj, NULL);
int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
if (tag != NULL)
env->ReleaseStringUTFChars(tagObj, tag);
env->ReleaseStringUTFChars(msgObj, msg);
return res;
}
static const JNINativeMethod gMethods[] = {
{ "isLoggable", "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
{ "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
{ "logger_entry_max_payload_native", "()I", (void*) android_util_Log_logger_entry_max_payload_native },
};
jni继续往下,调用了__android_log_buf_write,其实跟第1小节调到了同一个函数。
android_util_Log.cpp include了utils/Log.h,这个文件定义在system/core/include/utils/Log.h
实际上,Log.h这个头文件里面并没有声明__android_log_buf_write,但它include了另外一个头文件log.h
#include
system/core/include/cutils/log.h,这个文件只有一句话:
#include
system/core/include/log/log.h声明了__android_log_buf_write函数。
绕了几圈,终于找到了真正定义的地方!
声明:
system/core/include/log/log.h
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
实现:
system/core/liblog/logger_write.c
LIBLOG_ABI_PUBLIC int __android_log_buf_write(int bufID, int prio,
const char *tag, const char *msg)
{
struct iovec vec[3];
char tmp_tag[32];
if (!tag)
tag = "";
if ((bufID != LOG_ID_RADIO) &&
(!strcmp(tag, "HTC_RIL") ||
!strncmp(tag, "RIL", 3) ||
!strncmp(tag, "IMS", 3) ||
!strcmp(tag, "AT") ||
!strcmp(tag, "GSM") ||
!strcmp(tag, "STK") ||
!strcmp(tag, "CDMA") ||
!strcmp(tag, "PHONE") ||
!strcmp(tag, "SMS"))) {
bufID = LOG_ID_RADIO;
snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
tag = tmp_tag;
}
#if __BIONIC__
if (prio == ANDROID_LOG_FATAL) {
android_set_abort_message(msg);
}
#endif
vec[0].iov_base = (unsigned char *)&prio;
vec[0].iov_len = 1;
vec[1].iov_base = (void *)tag;
vec[1].iov_len = strlen(tag) + 1;
vec[2].iov_base = (void *)msg;
vec[2].iov_len = strlen(msg) + 1;
return write_to_log(bufID, vec, 3);
}
作者:zhangchao2280