adb logcat 带线程名和id

本文介绍了如何在C++代码中结合pthread_self()获取线程ID,并利用__android_log_print将带有线程ID的日志输出到adb logcat。通过设置宏定义实现不同级别日志的打印,同时提供了如何在shell下将printf输出重定向到logcat以及在JNI中启用ALOGV的方法。

将以下3者结合,写一个Log宏,随便加随便打。如下:


///////////////////////////////////////////////////////////////////////////////////
//personal debug switch////////////////////////////////////////////////////////// 
/********************************************************************************
P_DEBUG:
0     turn off debug 
1	    turn on debug 
 ********************************************************************************/
#ifndef P_LOGI 
#ifndef _PTHREAD_H_
#include <pthread.h>
//#define _PTHREAD_H_
#endif

#include "utils/Log.h"
#define P_DEBUG	1
#if P_DEBUG
static char mark_label[64] = "pmain";
#define SEPARATOR_LINE "----------------------------------"
      #define STA_LINE ">>>>>>>>>>>>>>>>>>>>>>>>>>>st.func"
      #define END_LINE "<<<<<<<<<<<<<<<<<<<<<<<<<<<ed.func"
#define LONNG_LINE = "------------------------------------------------------------------------------"
//static char mark_label[18] = "";//for the label you wish to mark when you adb logcat or whatever
#define P_LOGI(fmt, ...) ALOGI("Th%u,%s-,F:%s:%d, Fuc:%s  " fmt,(unsigned int)pthread_self(),mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__)
//#define P_LOGI(fmt, ...) printf("%s-,F:%s:%d, Fuc:%s  " fmt,mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__)
#else
#define P_LOGI(...)
#endif

#endif

///////////////////////////////////////////////////////////////////////////////////
    //P_LOGI(SEPARATOR_LINE);
///////////////////////////////////////////////////////////////////////////////////

 

 

 

std::this_thread::get_id();

or

GetCurrentThreadId();

 

 

线程相关函数(2)-pthread_self()获取调用线程ID

获取调用线程tid

#include <pthread.h>
pthread_t pthread_self(void);

示例:

复制代码

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void *printids(void *arg)
{
    const char *str = (const char *)arg;

    pid_t pid;
    pthread_t tid;
    
    pid = getpid();
    tid = pthread_self();
    printf("%s pid %u tid %u (0x%x)\n", str, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
    
}




int main()
{    

    pthread_t tid;
    int err;    

    err = pthread_create(&tid, NULL, printids, "new thread: ");
    if (err != 0) {
        fprintf(stderr, "can't create thread: %s\n", strerror(err));
        exit(1);
    }    
    printids("main thread: ");    
    sleep(1);
    return 0;

}

复制代码

运行结果:

main thread: pid 4959 tid 9791296 (0x956740)
new thread: pid 4959 tid 1480448 (0x169700)

 

 

logcat命令学习好文章:

http://blog.youkuaiyun.com/hudashi/article/details/7062914

 一、 如何将C++的标准输出打印到logcat中

在default状态下调用printf等std C/C++接口输出的log不会被打印到eclipse的logcat中,

但是android提供了__android_log_print这个函数可以将log重定向到eclipse的logcat中.

 

1)      在对应的mk文件中加入:LOCAL_LDLIBS := -llog

2)      在要使用LOG的cpp文件中加入:
#include <android/log.h>

使用方法如下;
__android_log_print(ANDROID_LOG_DEBUG,"Tag", __VA_ARGS__)

ANDROID_LOG_DEBUG: log的level

Tag:module的tag

__VA_ARGS__:格式化参数列表

例子:

__android_log_print(ANDROID_LOG_DEBUG,"libAirplay",  "service URI : %s", "www.163.com.video.xixi.mp4");

3)      直接使用并不是个好的想法,如果能够可以和eclipse一样实现分等级打印,那还不错;实现如下:

#define LOGV(...)__android_log_print(ANDROID_LOG_VERBOSE, " Tag", __VA_ARGS__)   // VERBOSE
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , " Tag ", __VA_ARGS__)    // DEBUG
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO  , " Tag ",__VA_ARGS__)          // INFO
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN  , " Tag ", __VA_ARGS__)    //WARN
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , " Tag ",__VA_ARGS__)      // ERROR

 

注意:括号中是省略号,不用改为参数.

在需要的地方直接使用

LOGV(“www.163.com.video.xixi.mp4”);

或者LOGD(“www.163.com.video.xixi.mp4”); 就好

4)      使用以上的函数打印出的log会出现在eclipse中(前提是:devices已经和PC adb connect了).

其他资料:

如何在shell下将printf打印的信息定向到logcat中:

touch /data/local.prop

vi local.prop   输入log.redirect=true

重启

logcat stdout:* stderr:*  *:s

之后就可以看到printf的输出了

 

或者

adb shell stop  

adb shell setprop log.redirect-stdio true

adb shell start

 

 

 

二、 如何输出Jni 接口中LOGV 信息。

在调试android cts问题时, isIEffectCommandSecure 接口中ALOGV("transact status: %d", status);

不会被打印, 看了下面文章才明白。

http://blog.youkuaiyun.com/mike8825/article/details/49386725

 Android的编译参数中,加入了-DNDEBUG,也就是默认是no debug的,当然还需要LOG_NDEBUG LOG_NIDEBUG LOG_NDDEBUG这三个宏设置。
当-DNDEBUG被打上后,默认ALOGV会被禁止。

在log.h中可以看到

#ifndef LOG_NDEBUG
#ifdef NDEBUG
#define LOG_NDEBUG 1
#else
#define LOG_NDEBUG 0
#endif
#endif

 

#ifndef ALOGV
#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#if LOG_NDEBUG
#define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
#else
#define ALOGV(...) __ALOGV(__VA_ARGS__)
#endif
#endif

因此, 可以在当前文件的最前面 加入 如下定义, 这样就可以输出

打开ALOGV: #define LOG_NDEBUG 0
---------------------  
作者:victor_wys  
来源:优快云  
原文:https://blog.youkuaiyun.com/wys7250578/article/details/18841923?utm_source=copy  
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值