Repository navigation
Expand file tree
/
Copy pathLogger.cpp
More file actions
119 lines (104 loc) · 3.36 KB
/
Copy pathLogger.cpp
File metadata and controls
119 lines (104 loc) · 3.36 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
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
#include "Logger.h"
#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <ctime>
#include <mutex>
namespace AntiCheatLogger
{
// 线程安全的日志输出
static std::mutex g_logMutex;
// 临时实现:输出到标准输出,后续可替换为正式日志库
static const char* GetLevelString(LogLevel level)
{
switch (level)
{
case LogLevel::LOG_LEVEL_DEBUG:
return "DEBUG";
case LogLevel::LOG_LEVEL_INFO:
return "INFO";
case LogLevel::LOG_LEVEL_WARNING:
return "WARNING";
case LogLevel::LOG_LEVEL_ERROR:
return "ERROR";
case LogLevel::LOG_LEVEL_CRITICAL:
return "CRITICAL";
default:
return "UNKNOWN";
}
}
static const char* GetCategoryString(LogCategory category)
{
switch (category)
{
case LogCategory::GENERAL:
return "GENERAL";
case LogCategory::SENSOR:
return "SENSOR";
case LogCategory::PERFORMANCE:
return "PERFORMANCE";
case LogCategory::SECURITY:
return "SECURITY";
case LogCategory::SYSTEM:
return "SYSTEM";
default:
return "UNKNOWN";
}
}
static std::string GetTimestamp()
{
auto now = std::time(nullptr);
auto localTime = *std::localtime(&now);
char buffer[64];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
return std::string(buffer);
}
void Log(LogLevel level, LogCategory category, const std::string& message)
{
std::lock_guard<std::mutex> lock(g_logMutex);
// 临时实现:格式化输出到标准输出
// 格式:[时间戳] [级别] [类别] 消息
std::cout << "[" << GetTimestamp() << "] "
<< "[" << GetLevelString(level) << "] "
<< "[" << GetCategoryString(category) << "] " << message << std::endl;
// TODO: 后续替换为正式日志库接口
// 例如:spdlog::get("anticheat")->log(spdlog::level::info, message);
}
void Log(LogLevel level, LogCategory category, const char* format, ...)
{
char buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
Log(level, category, std::string(buffer));
}
void LogPerformance(const std::string& operation, double durationMs)
{
std::ostringstream oss;
oss << "Performance: " << operation << " took " << durationMs << "ms";
Log(LogLevel::LOG_LEVEL_INFO, LogCategory::PERFORMANCE, oss.str());
}
void LogSensorExecution(const std::string& sensorName, double durationMs, bool timeout)
{
std::ostringstream oss;
oss << "Sensor: " << sensorName << " took " << durationMs << "ms";
if (timeout)
{
oss << " (TIMEOUT)";
}
Log(timeout ? LogLevel::LOG_LEVEL_WARNING : LogLevel::LOG_LEVEL_DEBUG, LogCategory::SENSOR, oss.str());
}
void LogSecurityEvent(const std::string& eventType, const std::string& details)
{
std::ostringstream oss;
oss << "Security Event: " << eventType << " - " << details;
Log(LogLevel::LOG_LEVEL_WARNING, LogCategory::SECURITY, oss.str());
}
void LogSystemInfo(const std::string& osVersion, const std::string& architecture)
{
std::ostringstream oss;
oss << "System Info: OS=" << osVersion << ", Arch=" << architecture;
Log(LogLevel::LOG_LEVEL_INFO, LogCategory::SYSTEM, oss.str());
}
} // namespace AntiCheatLogger