armink / EasyLogger

An ultra-lightweight(ROM<1.6K, RAM<0.3k), high-performance C/C++ log library. | 一款超轻量级(ROM<1.6K, RAM<0.3k)、高性能的 C/C++ 日志库

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

多线程写日志时,输出的线程id不是子线程的id

ahchengrui opened this issue · comments

修改linux demo为多线程模式,verbose里面的tid不是子线程本身的id?
I/elog [12-25 18:28:36] EasyLogger V2.0.3 is initialize success.
do multithread test...
start create thread........
start create thread........
id=0
tid =3152430848
id=1
tid =3144038144
elog_output----tid:13995
A/main [12-25 18:28:36 pid:31856 tid:13995] (main.c test_elog:86)Hello EasyLogger! 0
E/main [12-25 18:28:36] Hello EasyLogger! 0
W/main [12-25 18:28:36] Hello EasyLogger! 0
I/main [12-25 18:28:36] Hello EasyLogger! 0
elog_output----tid:13995
elog_output----tid:13995
elog_output----tid:13995
A/main [12-25 18:28:36 pid:31856 tid:13995] (main.c test_elog:86)Hello EasyLogger! 1
E/main [12-25 18:28:36] Hello EasyLogger! 1
W/main [12-25 18:28:36] Hello EasyLogger! 1
I/main [12-25 18:28:36] Hello EasyLogger! 1
elog_output----tid:13995
elog_output----tid:13995
D/main [12-25 18:28:36 pid:31856 tid:13995] (main.c:90)Hello EasyLogger! 0
V/main [12-25 18:28:36 pid:31856 tid:13995] (main.c:91)Hello EasyLogger! 0
D/main [12-25 18:28:36 pid:31856 tid:13995] (main.c:90)Hello EasyLogger! 1
V/main [12-25 18:28:36 pid:31856 tid:13995] (main.c:91)Hello EasyLogger! 1

咦~~你看下你的 tid 跟自带 demo 获取 tid 的方式一样吗?

/**
* get current thread name interface
*
* @return current thread name
*/
const char *elog_port_get_t_info(void) {
static char cur_thread_info[10] = { 0 };
snprintf(cur_thread_info, 10, "tid:%04ld", pthread_self());
return cur_thread_info;
}

demo/os/linux/main.c 代码修改如下
` /* start EasyLogger */
elog_start();

/* test logger output */
_test_multithread();

return EXIT_SUCCESS;

}
static void _test_multithread()
{
pthread_t ptt[2];
int i;
for (i = 0; i < 2; ++i) {
printf("start create thread........\n");
pthread_create(&ptt[i], NULL, test_elog, i);
}
for (i = 0; i < 2; ++i) {
pthread_join(ptt[i], NULL);
}
}
void test_elog(void *arg) {
int id = (int )arg;
static char cur_thread_info[10] = { 0 };
snprintf(cur_thread_info, 10, "tid:%04ld", pthread_self());
printf("pthread id is %s\n",cur_thread_info);
while(true) {
/
test log output for all level */
log_a("Hello EasyLogger! %d", id);
log_e("Hello EasyLogger! %d", id);
log_w("Hello EasyLogger! %d", id);
log_i("Hello EasyLogger! %d", id);
log_d("Hello EasyLogger! %d", id);
log_v("Hello EasyLogger! %d", id);
// elog_raw("Hello EasyLogger!");
sleep(5);
}
}
`
运行结果如下:
I/elog [12-25 21:44:15] EasyLogger V2.0.3 is initialize success.
start create thread........
start create thread........
pthread id is tid:20511
pthread id is tid:20427
elog_output----tid:14062
A/main [12-25 21:44:15 pid:23758 tid:14062] (main.c test_elog:81)Hello EasyLogger! 0
E/main [12-25 21:44:15] Hello EasyLogger! 0
W/main [12-25 21:44:15] Hello EasyLogger! 0
I/main [12-25 21:44:15] Hello EasyLogger! 0
elog_output----tid:14062
elog_output----tid:14062
D/main [12-25 21:44:15 pid:23758 tid:14062] (main.c:85)Hello EasyLogger! 0
V/main [12-25 21:44:15 pid:23758 tid:14062] (main.c:86)Hello EasyLogger! 0
elog_output----tid:14062
A/main [12-25 21:44:15 pid:23758 tid:14062] (main.c test_elog:81)Hello EasyLogger! 1
E/main [12-25 21:44:15] Hello EasyLogger! 1
W/main [12-25 21:44:15] Hello EasyLogger! 1
I/main [12-25 21:44:15] Hello EasyLogger! 1
elog_output----tid:14062
elog_output----tid:14062
D/main [12-25 21:44:15 pid:23758 tid:14062] (main.c:85)Hello EasyLogger! 1
V/main [12-25 21:44:15 pid:23758 tid:14062] (main.c:86)Hello EasyLogger! 1

获取到的线程id与V/main里面打印的线程id不一致

pthread 我现在用的不多,能深入查下原因吗?

线程号不一致的问题,估计是默认使用的异步输出,所以打印的线程号是异步线程的id。

应该不是,日志帧内容在转到异步输出前,就已经定好了

我才看了下代码,发现了问题,是因为snprintf(cur_thread_info, 10, "tid:%ld", pthread_self());这行代码导致的。
1、组包用的是%ld,和%d的输出不一样。
2、id长度很大,组包的时候有截断,无法输出全部id号。

@tianlongqin 分析的有道理 👍

  • snprintf(cur_thread_info, 10, "tid:%04ld", pthread_self()); ld要修改成lu才能正常打印出pthread_self()的返回值
  • pthread_self()的返回线程ID非top -H显示的线程ID,建议将pthread_self()更换成syscall的方式