-
Notifications
You must be signed in to change notification settings - Fork 10
/
Common.hpp
85 lines (67 loc) · 2.29 KB
/
Common.hpp
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
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "Common/BasicMacros.hpp"
#include "Common/Defer.hpp"
#include "Common/NtBase.hpp"
#include "Common/NtHandle.hpp"
#include "Common/NtEvent.hpp"
#include "Common/NtCriticalSection.hpp"
#include "Common/NtUtils.hpp"
#include "Common/StrUtils.hpp"
#include <algorithm> // std::min and std::max.
#include <math.h>
#ifdef _CONSOLE
inline void DebugLogImpl(const char * funcname, const char * type, const char * format, ...)
{
static CriticalSection mutex;
ScopedLock lock(mutex);
static bool is_inited = false;
static bool show_trace = false;
if (!is_inited)
{
// It also looks in path to exe, but it's fine for a debug build.
char buf[MAX_PATH];
strcpy_s(buf, GetCommandLineA());
_strlwr(buf);
if (StringContains<AsciiToLower>(buf, "trace")) { show_trace = true; }
is_inited = true;
}
if (!show_trace && type && StringEquals<AsciiToLower>(type, "TRACE")) { return; }
static uint64_t prev_date = 0;
SYSTEMTIME now = {0};
GetSystemTime(&now);
// Output current date once. First 8 bytes are current date, so we can compare it as a 64-bit integer.
if (prev_date != *((uint64_t*)&now))
{
prev_date = *((uint64_t*)&now);
printf("%04d/%02d/%02d ", now.wYear, now.wMonth, now.wDay);
}
printf("%02d:%02d:%02d.%03d ", now.wHour, now.wMinute, now.wSecond, now.wMilliseconds);
printf("[%5d] ", GetThreadId(GetCurrentThread()));
if (show_trace && funcname)
{
printf("[%s] ", funcname);
}
if (type && !StringEquals<AsciiToLower>(type, "TRACE") && !StringEquals<AsciiToLower>(type, "INFO"))
{
printf("[%s] ", type);
}
va_list argptr;
va_start(argptr, format);
vprintf(format, argptr);
printf("\n");
fflush(stdout);
}
#define DebugLog(...) DebugLogImpl(__FUNCTION__, "INFO", __VA_ARGS__)
#define DebugLogWarning(...) DebugLogImpl(__FUNCTION__, "WARNING", __VA_ARGS__)
#define DebugLogError(...) DebugLogImpl(__FUNCTION__, "ERROR", __VA_ARGS__)
#define TraceLog(...) DebugLogImpl(__FUNCTION__, "TRACE", __VA_ARGS__)
#define DebugThreadName(...) SetCurrentThreadDescriptionW(L ## __VA_ARGS__)
#else
#define DebugLog(...)
#define DebugLogWarning(...)
#define DebugLogError(...)
#define TraceLog(...)
#define DebugThreadName(...)
#endif