c/c++打印带完整附加信息(带时间戳、pid等)的日志,类adb logcat -v threadtime格式

近来在Linux下开发中用到c/c++和nodejs混合编程,看到adb logcat -v threadtime 打出来的Log那么整齐规范,非常想把c/c++代码的log规范成这种效果一下,

需要这样做有两个原因:

1.有的人没有用到adb, 或者log不输出到控制台,没法用adb logcat -v threadtime。

2. adb logcat的时间戳是后加的,不是调用打印log当时的时间。我看到过logcat时间戳前后颠倒,或者间隔明显不对的情况;系统io占用较高情况下会出现这种问题。

于是尝试自己printf加上时间戳,也就是这句话了:

#define MY_DEBUG(format, ...) printf("%s %d %d D %s: %s "#format "\n", timeString(), (int)getpid(), (int)syscall(SYS_gettid), TAG, __FUNCTION__, ##__VA_ARGS__)

宏定义可以用...表示剩余其他参数,在这个基础上加上时间戳、pid、tid、TAG、functionname.

时间戳这个事确实没有现成的,我通过timeString()函数自己根据clocktime算出来月、日、时、分、秒、毫秒,并格式化为固定宽度2位、3位。
获取pid没问题,但tid如果用gettid()经常报error: ‘gettid’ was not declared in this scope. 查了一下是个常见的glibc问题,索性用syscall(SYS_gettid)绕过。

上完整实验代码:

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/syscall.h>
#define TAG "MY_TEST"
#define MY_DEBUG(format, ...) printf("%s %d %d D %s : %s "#format "\n", timeString(), (int)getpid(), (int)syscall(SYS_gettid), TAG, __FUNCTION__, ##__VA_ARGS__)
// #define LOG_D(format, ...) MY_DEBUG( D #format, ##__VA_ARGS__)


char * timeString() {
  struct timespec ts;
  clock_gettime( CLOCK_REALTIME, &ts);
  struct tm * timeinfo = localtime(&ts.tv_sec);
  static char timeStr[20];
  sprintf(timeStr, "%.2d-%.2d %.2d:%.2d:%.2d.%.3ld", timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, ts.tv_nsec / 1000000);
  return timeStr;
}


void testWait(int index, int second, int nanoSecond) {
    MY_DEBUG(wait time is: %ds %dns index: %d, second, nanoSecond, index);
  return;
}
int main(int argc, char** argv) {
  MY_DEBUG(test ok\n);
  for (int i = 0; i<4;i++) {
    testWait(i, 3, 2000020);
  }
  return 0;
}

adb logcat -v threadtime 输出结果:

01-05 03:22:51.076 22982 22982 I CONSOLE : testWait wait time is: 3s 2000020ns 


编译执行我的log后,g++  ./testWait.cpp  -o run -lrt 输出结果:


$:~/test_code/lab$ g++  ./testWait.cpp  -o run -lrt

$:~/test_code/lab$ ./run 

12-26 10:45:32.360 7286 7286 D MY_TEST : main test ok


12-26 10:45:32.360 7286 7286 D MY_TEST : testWait wait time is: 3s 2000020ns index: 0

12-26 10:45:32.360 7286 7286 D MY_TEST : testWait wait time is: 3s 2000020ns index: 1

12-26 10:45:32.360 7286 7286 D MY_TEST : testWait wait time is: 3s 2000020ns index: 2

12-26 10:45:32.360 7286 7286 D MY_TEST : testWait wait time is: 3s 2000020ns index: 3


到这里已经和logcat日志一样了,还剩下一个引号问题我一直没有解决,细心的同学可能已经看到了,调用打印log时都没有加引号
MY_DEBUG(wait time is: %ds %dns index: %d, second, nanoSecond, index);
因为加了引号后,输出的testWait wait time……前后会各加一个“,看着不好看。不知道有啥办法可以让调用时的引号不打印出来。


12-26 10:44:12.869 4960 4960 D MY_TEST : testWait "wait time is: 3s 2000020ns index: 0"




  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值