-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.hpp
More file actions
54 lines (51 loc) · 1.64 KB
/
logger.hpp
File metadata and controls
54 lines (51 loc) · 1.64 KB
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
#pragma once
#include <mutex>
#include <chrono>
#include <ctime>
#include <cstdarg>
#include <cstdio>
#ifdef _WIN32
#include <windows.h>
#endif
// Ensure common short names are not pre-defined by platform headers
#ifdef DBG
#undef DBG
#endif
#ifdef INF
#undef INF
#endif
#ifdef WARN
#undef WARN
#endif
#ifdef ERR
#undef ERR
#endif
// Simple log level constants used across the codebase
enum { DBG = 0, INF = 1, WARN = 2, ERR = 3 };
inline std::mutex log_mtx;
inline void logf(int level, const char* fmt, ...){
std::lock_guard<std::mutex> lk(log_mtx);
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); std::tm tm;
#ifdef _WIN32
localtime_s(&tm, &t);
#else
localtime_r(&t, &tm);
#endif
char tb[20]; std::strftime(tb, sizeof(tb), "%F %T", &tm);
va_list ap; va_start(ap, fmt); char b[2048]; vsnprintf(b, sizeof(b), fmt, ap); va_end(ap);
// Enable ANSI escape processing on Windows consoles when possible
#ifdef _WIN32
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); DWORD m = 0; if (h && GetConsoleMode(h, &m)) SetConsoleMode(h, m | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
const char* level_name = "";
const char* color = "0"; // default
switch (level) {
case 0: level_name = "DBG"; color = "90"; break; // bright black / gray
case 1: level_name = "INF"; color = "32"; break; // green
case 2: level_name = "WARN"; color = "33"; break; // yellow
case 3: level_name = "ERR"; color = "31"; break; // red
default: level_name = "LOG"; color = "0"; break;
}
// Print timestamp, colored level tag, then message
std::printf("[%s] \x1b[%sm%s\x1b[0m %s\n", tb, color, level_name, b);
}