-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlog.h
162 lines (141 loc) · 5.45 KB
/
log.h
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <libloaderapi.h>
#include <processthreadsapi.h>
#include <profileapi.h>
#define GetProcessId GetCurrentProcessId
#define GetThreadId GetCurrentThreadId
#define PID_FORMAT "%04x"
static LARGE_INTEGER freq;
#else
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <time.h>
#include <unistd.h>
#define GetProcessId getpid
#define GetThreadId gettid
#define PID_FORMAT "%08x"
#endif
#if !defined(LOG_CHANNEL)
#warning LOG_CHANNEL is not defined
#define LOG_CHANNEL "unknown"
#endif
enum LogLevel {
LogLevel_None = 0,
LogLevel_Error = 1,
LogLevel_Warn = 2,
LogLevel_Info = 3,
LogLevel_Trace = 4,
LogLevel_Debug = 5,
};
static LogLevel logLevel = LogLevel_None;
static void (*wineDbgLog)(const char*) = nullptr;
static thread_local char wineDbgLogMessageBuffer[1024];
static inline const char* LogLevelString(LogLevel level) {
switch (level) {
case LogLevel_Debug:
return "debug";
case LogLevel_Trace:
return "trace";
case LogLevel_Info:
return "info";
case LogLevel_Warn:
return "warn";
case LogLevel_Error:
return "err";
case LogLevel_None:
return "none";
default:
return "?";
}
}
struct LogTimestamp {
int32_t seconds;
int32_t milliseconds;
};
static inline struct LogTimestamp GetTimestamp(void) {
#ifdef _WIN32
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return {
.seconds = (int32_t)(count.QuadPart / freq.QuadPart),
.milliseconds = (int32_t)(((count.QuadPart % freq.QuadPart) * 1000) / freq.QuadPart),
};
#else
struct timespec timespec;
clock_gettime(CLOCK_MONOTONIC_RAW, ×pec);
return {
.seconds = (int32_t)timespec.tv_sec,
.milliseconds = (int32_t)(timespec.tv_nsec / 1000000),
};
#endif
}
#define LOG(level, fmt, ...) \
do { \
if (logLevel >= level) { \
struct LogTimestamp time = GetTimestamp(); \
if (wineDbgLog) { \
snprintf( \
wineDbgLogMessageBuffer, sizeof(wineDbgLogMessageBuffer), \
"%3u.%03u:" PID_FORMAT ":" PID_FORMAT ":%s:" LOG_CHANNEL ":%s " fmt "\n", \
(int)time.seconds, (int)time.milliseconds, \
(unsigned int)GetProcessId(), (unsigned int)GetThreadId(), \
LogLevelString(level), \
__func__ __VA_OPT__(, ) __VA_ARGS__); \
wineDbgLog(wineDbgLogMessageBuffer); \
} else { \
fprintf( \
stderr, \
"%3u.%03u:" PID_FORMAT ":" PID_FORMAT ":%s:" LOG_CHANNEL ":%s " fmt "\n", \
(int)time.seconds, (int)time.milliseconds, \
(unsigned int)GetProcessId(), (unsigned int)GetThreadId(), \
LogLevelString(level), \
__func__ __VA_OPT__(, ) __VA_ARGS__); \
} \
} \
} while (0)
#define ERR(fmt, ...) LOG(LogLevel_Error, fmt, __VA_ARGS__)
#define WARN(fmt, ...) LOG(LogLevel_Warn, fmt, __VA_ARGS__)
#define INFO(fmt, ...) LOG(LogLevel_Info, fmt, __VA_ARGS__)
#define TRACE(fmt, ...) LOG(LogLevel_Trace, fmt, __VA_ARGS__)
#define DBG(fmt, ...) LOG(LogLevel_Debug, fmt, __VA_ARGS__)
static void InitLogger(const char* logLevelEnvName) {
const char* logLevelEnv = getenv(logLevelEnvName);
if (logLevelEnv) {
switch (*logLevelEnv) {
case 'n':
logLevel = LogLevel_None;
break;
case 'e':
logLevel = LogLevel_Error;
break;
case 'w':
logLevel = LogLevel_Warn;
break;
case 'i':
logLevel = LogLevel_Info;
break;
case 't':
logLevel = LogLevel_Trace;
break;
case 'd':
logLevel = LogLevel_Debug;
break;
default:
logLevel = (LogLevel)(atoi(logLevelEnv));
break;
}
}
#if defined(_WIN32)
QueryPerformanceFrequency(&freq);
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
if (ntdll)
*(void**)(&wineDbgLog) = (void*)GetProcAddress(ntdll, "__wine_dbg_output");
#endif
}