-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogfile_manager.cpp
326 lines (274 loc) · 11.7 KB
/
logfile_manager.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <chrono>
#include <iostream>
#include "logfile_manager.h"
#include "packet_handler.h"
#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
// 로그 레벨을 순차적으로 설정하고 로그 메시지를 출력하는 함수
int CLoggingManager::ManageLogLevel(){
try {
auto logger = spdlog::default_logger();
spdlog::set_level(spdlog::level::trace);
spdlog::trace("This is a trace message - visible at trace level");
spdlog::set_level(spdlog::level::debug);
spdlog::info("This is an info message - visible at debug level");
spdlog::debug("This is a debug message - visible at debug level");
spdlog::set_level(spdlog::level::info);
spdlog::warn("This is a warning message - visible at info level");
spdlog::debug("This debug message will not be visible at info level");
spdlog::set_level(spdlog::level::err);
spdlog::error("This is an error message - visible at error level");
spdlog::info("This info message will not be visible at error level");
spdlog::set_level(spdlog::level::critical);
spdlog::critical("This is a critical message - visible at critical level");
spdlog::warn("This warning message will not be visible at critical level");
SPDLOG_TRACE("Some trace message with param {}", 42);
SPDLOG_DEBUG("Some debug message");
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in ManageLogLevel: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 크기와 개수가 제한된 회전 로그 파일을 설정하고 로거 패턴을 정의하는 함수
int CLoggingManager::RotateLogs() {
try {
size_t siMaxSize = 1048576; // 로그 파일 최대 크기를 1MB로 설정하여 빠른 로테이션 유도
size_t siMaxFiles = 1; // 로그 파일 개수는 2(초기파일+1)개로 설정
auto create_rotating_logger = [&](const std::string& loggerName, const std::string& fileName) {
auto logger = spdlog::get(loggerName);
if (!logger) {
logger = spdlog::rotating_logger_mt(loggerName, "logs/" + fileName, siMaxSize, siMaxFiles);
logger->set_level(spdlog::level::trace);
logger->set_pattern("[%Y-%m-%d %H:%M:%S] [%l] %v");
}
};
create_rotating_logger("packetLogger", "packet_transmission.log");
create_rotating_logger("maliciousLogger", "malicious_ips.log");
create_rotating_logger("detailedLogger", "detailed_logs.log");
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in RotateLogs: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 로그 메시지를 생성하여 로그 파일의 크기를 빠르게 증가시키는 함수
int CLoggingManager::GenerateLogs(const std::string& strLoggerName) {
try {
auto logger = spdlog::get(strLoggerName);
if (!logger) {
spdlog::error("Logger with name {} does not exist.", strLoggerName);
return ERROR_UNKNOWN;
}
for (int i = 0; i < 1; ++i) {
logger->info("Logged message.");
}
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in GenerateLogs: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 멀티 싱크 로거를 설정하고 로그 레벨에 맞는 메시지 출력을 확인하는 함수
int CLoggingManager::MultiSinkLogger() {
try {
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
consoleSink->set_level(spdlog::level::info);
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multi_sink.log", true);
fileSink->set_level(spdlog::level::trace);
std::vector<spdlog::sink_ptr> sinks {consoleSink, fileSink};
auto logger = std::make_shared<spdlog::logger>("multiSinkLogger", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::trace);
spdlog::register_logger(logger);
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%L%$] [pid %P] [thread %t] [%s:%#] %v");
std::cout << " \n";
for (int i = 0; i < 2; ++i) {
logger->trace("Trace level message {}", i);
logger->debug("Debug level message {}", i);
logger->info("Info level message {}", i);
logger->warn("Warning level message {}", i);
logger->error("Error level message {}", i);
logger->critical("Critical level message {}", i);
}
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in MultiSinkLogger: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 비동기 로깅을 설정하는 함수
int CLoggingManager::SetupAsyncLogger() {
try {
spdlog::init_thread_pool(8192, 1);
if (!spdlog::get("asyncFileLogger")) {
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/async.log", true);
auto logger = std::make_shared<spdlog::async_logger>("async_file_logger", fileSink, spdlog::thread_pool(), spdlog::async_overflow_policy::block);
spdlog::register_logger(logger);
}
return SUCCESS_CODE;
} catch (const spdlog::spdlog_ex& ex) {
return ERROR_UNKNOWN;
}
}
// 멀티스레드 로깅을 테스트 함수
int CLoggingManager::TestMultiThreadedLogging() {
try {
std::vector<std::thread> vecThreads;
const int nThreads = 4;
const int nLogsPerThread = 625;
for (int i = 1; i <= nThreads; ++i) {
vecThreads.emplace_back([i, nLogsPerThread]() {
auto logger = spdlog::get("async_file_logger");
if (!logger) return;
for (int j = 0; j < nLogsPerThread; ++j) {
logger->trace("Thread {} - Trace message {}", i, j);
logger->debug("Thread {} - Debug message {}", i, j);
logger->info("Thread {} - Log message {}", i, j);
logger->warn("Thread {} - Warning message {}", i, j);
logger->error("Thread {} - Error message {}", i, j);
logger->critical("Thread {} - Critical message {}", i, j);
}
});
}
for (auto& thread : vecThreads) {
thread.join();
}
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in MultiThreadedLoggingTest: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 비동기 로딩 시스템의 성능을 측정하기 위한 함수
int CLoggingManager::MeasureAsyncLogPerformance() {
try {
auto logger = spdlog::get("async_file_logger");
if (!logger) {
spdlog::error("Logger not found");
return ERROR_UNKNOWN;
}
const int nLogs = 10000;
auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << " " << "\n";
std::cout << "Logging " << nLogs << " messages took " << elapsed.count() << " seconds using asynchronous logging." << std::endl;
logger->flush();
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in MeasureAsyncLoggingPerformance: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 동기 로딩을 설정하는 함수
int CLoggingManager::SetupSyncLogger() {
try {
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/sync.log", true);
auto logger = std::make_shared<spdlog::logger>("syncFileLogger", fileSink);
spdlog::register_logger(logger);
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in SetupSyncLogger: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 동기 로딩 시스템의 성능을 측정하기 위한 함수
int CLoggingManager::MeasureSyncLogPerformance() {
try {
auto logger = spdlog::get("syncFileLogger");
if (!logger) {
spdlog::error("Logger not found");
return ERROR_UNKNOWN;
}
const int nLogs = 10000;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < nLogs; ++i) {
logger->info("This is a synchronous log message number {}", i);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Logging " << nLogs << " messages took " << elapsed.count() << " seconds using synchronous logging." << std::endl;
logger->flush();
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in MeasureSyncLoggingPerformance: {}", ex.what());
return ERROR_UNKNOWN;
}
}
int CLoggingManager::StartRotation(){
try {
int nResult = RotateLogs();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to rotate logs.");
return nResult;
}
nResult = GenerateLogs("packetLogger");
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to generate logs for packetLogger.");
return nResult;
}
nResult = GenerateLogs("maliciousLogger");
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to generate logs for maliciousLogger.");
return nResult;
}
nResult = GenerateLogs("detailedLogger");
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to generate logs for detailedLogger.");
return nResult;
}
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in TestLogging: {}", ex.what());
return ERROR_UNKNOWN;
}
}
// 전체 로깅 시스템을 설정하고 다양한 로깅 테스트를 수행하는 함수
int CLoggingManager::TestLogging() {
try{
int nRotationResult = CLoggingManager::StartRotation();
CLoggingManager instance;
int nResult = instance.ManageLogLevel();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to manage log level.");
return nResult;
}
nResult = instance.MultiSinkLogger();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to create multi-sink logger.");
return nResult;
}
nResult = instance.SetupAsyncLogger();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to setup async logger.");
return nResult;
}
nResult = instance.TestMultiThreadedLogging();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to test multi-threaded logging.");
return nResult;
}
nResult = instance.MeasureAsyncLogPerformance();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to measure async log performance.");
return nResult;
}
nResult = instance.SetupSyncLogger();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to setup sync logger.");
return nResult;
}
nResult = instance.MeasureSyncLogPerformance();
if (nResult != SUCCESS_CODE) {
spdlog::error("Failed to measure sync log performance.");
return nResult;
}
return SUCCESS_CODE;
} catch (const std::exception& ex) {
spdlog::error("Exception in TestLogging: {}", ex.what());
return ERROR_UNKNOWN;
}
}