-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadlog.cpp
99 lines (81 loc) · 2.08 KB
/
threadlog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdio>
#include <ctime>
#include <cstdarg>
#include <cstring>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#ifndef __linux__
#include <windows.h>
#else
#include <sys/syscall.h>
#endif
#include "threadlog.h"
#include "HandlerThread.h"
static bool isFirst=true;
pthread_mutex_t logMutex;
#ifndef __linux__
const static char * pLogPath = "D:/";
#else
struct passwd * pw = getpwuid(getuid());
const static char * pLogPath = pw->pw_dir;
#endif
char strFilePath[MAXLOGLEN] = {0};
const char * get_filename(const char * pFilePath)
{
const char * pFileName = strrchr(pFilePath, static_cast<int>('/'));
if (NULL != pFileName)
{
pFileName += 1;
}
else
{
pFileName = pFilePath;
}
return pFileName;
}
void log_dbg_print(const char * pFuncName, const char * pFormat, ...)
{
char str_buffer[MAXLOGLEN];
std::va_list args;
va_start(args, pFormat);
vsnprintf(str_buffer, sizeof(str_buffer), pFormat, args);
va_end(args);
struct timeval tmv = {0};
time_t now = time(0);
gettimeofday(&tmv, NULL);
struct tm * pNow = localtime(&now);
pid_t pid = getpid();
#ifndef __linux__
pid_t tid = GetCurrentThreadId();
#else
pid_t tid = syscall(SYS_gettid);
#endif
char strtime[MAXLOGLEN];
snprintf(strtime, sizeof(strtime), "%4d-%02d-%2d %02d:%02d:%02d.%06ld", (1900 + pNow->tm_year), (1 + pNow->tm_mon), pNow->tm_mday, pNow->tm_hour, pNow->tm_min, pNow->tm_sec, tmv.tv_usec);
char strLine[MAXLOGLEN];
snprintf(strLine, sizeof(strLine), "%s %d %d %s: %s\n", strtime, pid, tid, pFuncName, str_buffer);
pthread_mutex_lock(&logMutex);
FILE * fp = NULL;
if (isFirst)
{
strcpy(strFilePath, pLogPath);
strcat(strFilePath, "/tombstone");
fp = fopen(strFilePath, "w");
isFirst = false;
}
else
{
fp = fopen(strFilePath, "a");
}
if (fp == NULL)
{
pthread_mutex_unlock(&logMutex);
return ;
}
fputs(strLine, fp);
fclose(fp);
fp = NULL;
pthread_mutex_unlock(&logMutex);
}