-
Notifications
You must be signed in to change notification settings - Fork 177
feat-logger-update #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat-logger-update #1173
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ede36e
feat-logger-update
nguyenhoangthuan99 b6dd78f
feat-update-logger move max line to .cortexrc
nguyenhoangthuan99 d7d3bf0
Merge branch 'dev' into feat-logger-update
nguyenhoangthuan99 8408543
feat-update-logger add mutex for thread safe
nguyenhoangthuan99 6bfa2e7
Merge branch 'feat-logger-update' of github.com:janhq/cortex.cpp into…
nguyenhoangthuan99 4f3b0b0
Fix comment
nguyenhoangthuan99 53ff237
Fix comment
nguyenhoangthuan99 3ab763c
fix-comment
nguyenhoangthuan99 82db904
fix CI build
nguyenhoangthuan99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) { | ||
nguyenhoangthuan99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 (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"r+"); | ||
#else | ||
fp_ = fopen(file_name_.c_str(), "r+"); | ||
#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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#pragma once | ||
|
||
#include <trantor/utils/AsyncFileLogger.h> | ||
#include <trantor/utils/Utilities.h> | ||
#include <atomic> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <deque> | ||
#include <mutex> | ||
|
||
#ifdef _WIN32 | ||
#include <windows.h> | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
|
||
namespace trantor { | ||
|
||
class TRANTOR_EXPORT FileLogger : public AsyncFileLogger { | ||
nguyenhoangthuan99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
FileLogger(); | ||
~FileLogger(); | ||
|
||
/** | ||
* @brief Set the maximum number of lines to keep in the log file. | ||
* | ||
* @param maxLines | ||
*/ | ||
void setMaxLines(uint64_t maxLines) { max_lines_ = maxLines; } | ||
|
||
/** | ||
* @brief Set the log file name. | ||
* | ||
* @param fileName The full name of the log file. | ||
*/ | ||
void setFileName(const std::string& fileName) { | ||
filePath_ = "./"; | ||
fileBaseName_ = fileName; | ||
fileExtName_ = ""; | ||
} | ||
void output_(const char* msg, const uint64_t len); | ||
|
||
protected: | ||
class CircularLogFile { | ||
public: | ||
CircularLogFile(const std::string& fileName, uint64_t maxLines); | ||
~CircularLogFile(); | ||
|
||
void writeLog(const char* logLine, const uint64_t len); | ||
void flush(); | ||
uint64_t getLength() const { return lineBuffer_.size(); } | ||
|
||
private: | ||
FILE* fp_{nullptr}; | ||
uint64_t max_lines_; | ||
std::string file_name_; | ||
std::deque<std::string> lineBuffer_; | ||
std::atomic<int> linesWrittenSinceLastTruncate_{0}; | ||
static const uint64_t TRUNCATE_CHECK_INTERVAL = 1000; | ||
mutable std::mutex mutex_; | ||
|
||
void LoadExistingLines(); | ||
void TruncateFileIfNeeded(); | ||
void AppendToFile(const std::string& line); | ||
void OpenFile(); | ||
void CloseFile(); | ||
}; | ||
std::unique_ptr<CircularLogFile> circular_log_file_ptr_; | ||
uint64_t max_lines_{100000}; // Default to 100000 lines | ||
}; | ||
|
||
} // namespace trantor |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.