-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
180 lines (149 loc) · 5.17 KB
/
logger.cpp
File metadata and controls
180 lines (149 loc) · 5.17 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "logger.h"
#include "utils.h"
#include <ctime>
#include <sstream>
#include <iomanip>
#include <cstring>
Logger& Logger::instance() {
static Logger instance;
return instance;
}
void Logger::init(const std::string& log_file) {
std::lock_guard<std::mutex> lock(mutex_);
if (initialized_) {
return;
}
log_file_ = log_file;
// Ensure log directory exists
if (!log_file_.empty()) {
utils::ensure_log_file(log_file_);
// Open in append mode for live appending
// std::ios::app ensures writes go to end of file
file_stream_.open(log_file_, std::ios::app | std::ios::out);
if (file_stream_.is_open()) {
// Ensure immediate writes by setting unitbuf (flush after each output operation)
file_stream_.setf(std::ios::unitbuf);
initialized_ = true;
} else {
initialized_ = false;
}
}
}
void Logger::log(LogLevel level, const std::string& message) {
std::lock_guard<std::mutex> lock(mutex_);
if (!initialized_ || !file_stream_.is_open()) {
return;
}
uint64_t timestamp = std::time(nullptr);
std::string level_str = level_to_string(level);
std::string time_str = format_timestamp(timestamp);
// Format: timestamp level message
file_stream_ << time_str << " [" << level_str << "] " << message << "\n";
file_stream_.flush(); // Ensure immediate write to disk
}
void Logger::log_connection(const ConnectionLog& conn_log) {
std::lock_guard<std::mutex> lock(mutex_);
if (!initialized_ || !file_stream_.is_open()) {
return;
}
std::stringstream json;
json << std::fixed << std::setprecision(2);
json << format_timestamp(conn_log.timestamp) << " [CONN] {";
json << "\"event\":\"" << escape_json_string(conn_log.event) << "\"";
if (!conn_log.client_ip.empty()) {
json << ",\"client_ip\":\"" << escape_json_string(conn_log.client_ip) << "\"";
json << ",\"client_port\":" << conn_log.client_port;
}
if (!conn_log.target_host.empty()) {
json << ",\"target_host\":\"" << escape_json_string(conn_log.target_host) << "\"";
json << ",\"target_port\":" << conn_log.target_port;
}
if (!conn_log.runway_id.empty()) {
json << ",\"runway_id\":\"" << escape_json_string(conn_log.runway_id) << "\"";
}
if (!conn_log.method.empty()) {
json << ",\"method\":\"" << escape_json_string(conn_log.method) << "\"";
}
if (!conn_log.path.empty()) {
json << ",\"path\":\"" << escape_json_string(conn_log.path) << "\"";
}
if (conn_log.status_code > 0) {
json << ",\"status_code\":" << conn_log.status_code;
}
if (conn_log.bytes_sent > 0 || conn_log.bytes_received > 0) {
json << ",\"bytes_sent\":" << conn_log.bytes_sent;
json << ",\"bytes_received\":" << conn_log.bytes_received;
}
if (conn_log.duration_ms > 0.0) {
json << ",\"duration_ms\":" << conn_log.duration_ms;
}
if (!conn_log.error.empty()) {
json << ",\"error\":\"" << escape_json_string(conn_log.error) << "\"";
}
json << "}\n";
file_stream_ << json.str();
file_stream_.flush(); // Ensure immediate write to disk
}
void Logger::flush() {
std::lock_guard<std::mutex> lock(mutex_);
if (file_stream_.is_open()) {
file_stream_.flush();
}
}
void Logger::close() {
std::lock_guard<std::mutex> lock(mutex_);
if (file_stream_.is_open()) {
file_stream_.close();
}
initialized_ = false;
}
std::string Logger::format_timestamp(uint64_t timestamp) {
std::time_t time_val = static_cast<std::time_t>(timestamp);
std::tm tm_info;
#ifdef _WIN32
// Use localtime_s on Windows (thread-safe)
if (localtime_s(&tm_info, &time_val) != 0) {
return "0000-00-00 00:00:00";
}
#else
// Use localtime_r on POSIX (thread-safe)
if (localtime_r(&time_val, &tm_info) == nullptr) {
return "0000-00-00 00:00:00";
}
#endif
std::stringstream ss;
ss << std::put_time(&tm_info, "%Y-%m-%d %H:%M:%S");
return ss.str();
}
std::string Logger::escape_json_string(const std::string& str) {
std::stringstream escaped;
for (char c : str) {
if (c == '"') {
escaped << "\\\"";
} else if (c == '\\') {
escaped << "\\\\";
} else if (c == '\n') {
escaped << "\\n";
} else if (c == '\r') {
escaped << "\\r";
} else if (c == '\t') {
escaped << "\\t";
} else if (static_cast<unsigned char>(c) < 0x20) {
// Control characters
escaped << "\\u" << std::hex << std::setw(4) << std::setfill('0')
<< static_cast<int>(static_cast<unsigned char>(c));
} else {
escaped << c;
}
}
return escaped.str();
}
std::string Logger::level_to_string(LogLevel level) {
switch (level) {
case LogLevel::DEBUG: return "DEBUG";
case LogLevel::INFO: return "INFO";
case LogLevel::WARN: return "WARN";
case LogLevel::ERROR_LEVEL: return "ERROR";
default: return "INFO";
}
}