-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlogger.cpp
More file actions
97 lines (75 loc) · 2.98 KB
/
logger.cpp
File metadata and controls
97 lines (75 loc) · 2.98 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
#include "logger.h"
#include <fstream>
#include <ctime>
#include <iostream>
#include <chrono>
#include <filesystem>
void Logger::init(std::filesystem::path outputDir) {
std::filesystem::create_directories(outputDir);
outputDirectory = std::move(outputDir);
deleteOldLogs(10);
refreshLogfiles();
}
void Logger::logQuery(intercept::types::r_string message) {
std::unique_lock lock(queryLog_lock);
if (!queryLog) return;
pushTimestamp(*queryLog);
*queryLog << message.c_str() << "\n";
}
void Logger::logThread(intercept::types::r_string message) {
std::unique_lock lock(threadLog_lock);
if (!threadLogEnabled) return;
pushTimestamp(*threadLog);
*threadLog << message.c_str() << "\n";
}
void Logger::pushTimestamp(std::ostream& str) const {
auto currentTime = std::chrono::system_clock::now();
auto millisSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime.time_since_epoch()).count();
auto timeT = std::chrono::system_clock::to_time_t(currentTime);
char buffer [84];
auto leng = strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&timeT));
sprintf(&buffer[leng-1], ".%03lld", millisSinceEpoch - timeT*1000);
str << "["sv << buffer << "] "sv;
}
void Logger::refreshLogfiles() {
// Get the current timestamp
auto currentTime = std::chrono::system_clock::now();
auto timeT = std::chrono::system_clock::to_time_t(currentTime);
char timestampBuffer[20];
strftime(timestampBuffer, sizeof(timestampBuffer), "%Y%m%d%H%M%S", localtime(&timeT));
std::string currentTimeString = timestampBuffer;
// Add the timestamp to the log file names
std::string queryLogFileName = "query_" + currentTimeString + ".log";
std::string threadLogFileName = "thread_" + currentTimeString + ".log";
{
std::unique_lock lock(queryLog_lock);
if (queryLog) queryLog.reset();
if (queryLogEnabled)
queryLog = std::make_unique<std::ofstream>(outputDirectory / queryLogFileName, std::ofstream::app);
}
{
std::unique_lock lock(threadLog_lock);
if (threadLog) threadLog.reset();
if (threadLogEnabled)
threadLog = std::make_unique<std::ofstream>(outputDirectory / threadLogFileName, std::ofstream::app);
}
}
void Logger::deleteOldLogs(int days) {
std::filesystem::path& path = outputDirectory;
try {
auto now = std::filesystem::file_time_type::clock::now();
auto duration = std::chrono::hours(24 * days);
for (auto& p : std::filesystem::directory_iterator(path)) {
if (p.is_regular_file() && p.path().extension() == ".log") {
auto last_modified = std::filesystem::last_write_time(p);
if (now - last_modified > duration) {
std::filesystem::remove(p);
std::cout << "Deleted: " << p.path() << '\n';
}
}
}
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
}
}