forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_logger.cc
More file actions
175 lines (149 loc) · 4.46 KB
/
file_logger.cc
File metadata and controls
175 lines (149 loc) · 4.46 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
#include "file_logger.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#ifdef _WIN32
#include <io.h>
#define ftruncate _chsize
#else
#include <unistd.h>
#endif
#include <string.h>
using namespace trantor;
FileLogger::FileLogger() : AsyncFileLogger() {}
FileLogger::~FileLogger() = default;
void FileLogger::output_(const char* msg, const uint64_t len) {
if (!circular_log_file_ptr_) {
circular_log_file_ptr_ =
std::make_unique<CircularLogFile>(fileBaseName_, max_lines_);
}
circular_log_file_ptr_->writeLog(msg, len);
}
FileLogger::CircularLogFile::CircularLogFile(const std::string& fileName,
uint64_t maxLines)
: max_lines_(maxLines), file_name_(fileName) {
std::lock_guard<std::mutex> lock(mutex_);
OpenFile();
LoadExistingLines();
TruncateFileIfNeeded();
}
FileLogger::CircularLogFile::~CircularLogFile() {
std::lock_guard<std::mutex> lock(mutex_);
CloseFile();
}
void FileLogger::CircularLogFile::writeLog(const char* logLine,
const uint64_t len) {
std::lock_guard<std::mutex> lock(mutex_);
if (!fp_)
return;
std::string logString(logLine, len);
std::istringstream iss(logString);
std::string line;
while (std::getline(iss, line)) {
if (lineBuffer_.size() >= max_lines_) {
lineBuffer_.pop_front();
}
lineBuffer_.push_back(line);
AppendToFile(line + "\n");
++linesWrittenSinceLastTruncate_;
if (static_cast<uint64_t>(linesWrittenSinceLastTruncate_.load()) >= TRUNCATE_CHECK_INTERVAL) {
TruncateFileIfNeeded();
}
}
}
void FileLogger::CircularLogFile::flush() {
std::lock_guard<std::mutex> lock(mutex_);
if (fp_) {
fflush(fp_);
}
}
void FileLogger::CircularLogFile::TruncateFileIfNeeded() {
// std::cout<<"Truncating file "<< totalLines_ <<std::endl;
if (!fp_ || lineBuffer_.size() < max_lines_)
return;
// Close the current file
fclose(fp_);
fp_ = nullptr;
// Open a temporary file for writing
std::string tempFileName = file_name_ + ".temp";
FILE* tempFile = fopen(tempFileName.c_str(), "w");
if (!tempFile) {
std::cout << "Error opening temporary file for truncation: "
<< strerror(errno) << std::endl;
OpenFile(); // Reopen the original file
return;
}
// Write only the last max_lines_ lines to the temporary file
size_t startIndex =
lineBuffer_.size() > max_lines_ ? lineBuffer_.size() - max_lines_ : 0;
for (size_t i = startIndex; i < lineBuffer_.size(); ++i) {
fprintf(tempFile, "%s\n", lineBuffer_[i].c_str());
}
fclose(tempFile);
// Replace the original file with the temporary file
if (std::rename(tempFileName.c_str(), file_name_.c_str()) != 0) {
std::cout << "Error replacing original file with truncated file: "
<< strerror(errno) << std::endl;
std::remove(tempFileName.c_str()); // Clean up the temporary file
}
// else {
// totalLines_.store(lineBuffer_.size() > max_lines_ ? max_lines_
// : lineBuffer_.size());
// }
// Reopen the file
OpenFile();
// LoadExistingLines();
linesWrittenSinceLastTruncate_.store(0);
}
void FileLogger::CircularLogFile::OpenFile() {
#ifdef _WIN32
auto wFileName = utils::toNativePath(file_name_);
fp_ = _wfopen(wFileName.c_str(), L"a+");
#else
fp_ = fopen(file_name_.c_str(), "a+");
#endif
if (!fp_) {
// If file doesn't exist, create it
#ifdef _WIN32
fp_ = _wfopen(wFileName.c_str(), L"w+");
#else
fp_ = fopen(file_name_.c_str(), "w+");
#endif
if (!fp_) {
std::cerr << "Error opening file: " << strerror(errno) << std::endl;
}
}
}
void FileLogger::CircularLogFile::LoadExistingLines() {
if (!fp_)
return;
// Move to the beginning of the file
fseek(fp_, 0, SEEK_SET);
lineBuffer_.clear();
std::string line;
char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp_) != nullptr) {
line = buffer;
if (!line.empty() && line.back() == '\n') {
line.pop_back(); // Remove trailing newline
}
if (lineBuffer_.size() >= max_lines_) {
lineBuffer_.pop_front();
}
lineBuffer_.push_back(line);
}
// Move back to the end of the file for appending
fseek(fp_, 0, SEEK_END);
}
void FileLogger::CircularLogFile::AppendToFile(const std::string& line) {
if (fp_) {
fwrite(line.c_str(), 1, line.length(), fp_);
fflush(fp_);
}
}
void FileLogger::CircularLogFile::CloseFile() {
if (fp_) {
fclose(fp_);
fp_ = nullptr;
}
}