Skip to content

Commit 3824e06

Browse files
author
Gin
committed
fix file loger on command line
1 parent 78274ce commit 3824e06

5 files changed

Lines changed: 22 additions & 6 deletions

File tree

Common/Cpp/Logging/FileLogger.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ void FileLogger::thread_loop(){
133133
return m_stopping || !m_queue.empty();
134134
});
135135
if (m_stopping){
136+
if (m_config.flush_when_exit && m_file.is_open()){
137+
while(m_queue.size() > 0){
138+
std::string msg = std::move(m_queue.front().first);
139+
// Write to file
140+
std::string line = normalize_newlines(msg);
141+
std::string file_str = to_file_str(line);
142+
m_file.write(file_str.c_str(), file_str.size());
143+
m_queue.pop_front();
144+
}
145+
m_file.flush();
146+
}
136147
break;
137148
}
138149
auto& item = m_queue.front();

Common/Cpp/Logging/FileLogger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct FileLoggerConfig{
2828
size_t max_queue_size = 10000; // Max pending log entries before blocking
2929
size_t max_file_size_bytes = 50 * 1024 * 1024; // Max file size before rotation (50MB default)
3030
size_t last_log_max_lines = 10000; // Max lines to keep in memory for get_last()
31+
bool flush_when_exit = false; // Whether to save remaining logs to file when program exists
3132
};
3233

3334

Common/Cpp/Logging/GlobalLogger.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ static FileLoggerConfig _global_logger_config;
1313

1414
void initialize_global_logger(FileLoggerConfig config){
1515
_global_logger_config = std::move(config);
16-
// auto& logger = global_logger_raw();
17-
// logger.log("Initialized logger with path " + config.file_path);
1816
}
1917

2018
Logger& global_logger_raw(){
2119
auto get_config = [&](){
2220
if (_global_logger_config.file_path.size() == 0){
23-
_global_logger_config.file_path = ".";
21+
_global_logger_config.file_path = "./SerialPrograms.log";
2422
}
2523
return _global_logger_config;
2624
};

Common/Cpp/Logging/GlobalLogger.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ namespace PokemonAutomation{
1616

1717
// Call this function at very beginning of the main function to initialize the global file logger.
1818
// Call `global_logger_raw()` to retrieve the global file logger afterwards.
19-
// If not called, a default logger with default file path of "." will be created.
19+
// If not called, a default logger with default file path of "./SerialPrograms.log" will be created.
2020
void initialize_global_logger(FileLoggerConfig config);
2121

2222

2323
// Return a global raw `FileLogger`. `FileLogger` is defined in FileLogger.h.
2424
// "raw" here means the logger does not add any timestamp or tags to the incoming log lines.
2525
// Suggest calling `initialize_global_logger()` at very beginning of the main function to
26-
// initialize the logger first. Otherwise a default logger with default file path of "."
27-
// will be created.
26+
// initialize the logger first. Otherwise a default logger with default file path of
27+
// "./SerialPrograms.log" will be created.
2828
Logger& global_logger_raw();
2929

3030

SerialPrograms/Source/CommandLine/CommandLine_Main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ using namespace PokemonAutomation;
1717
using namespace PokemonAutomation::NintendoSwitch;
1818

1919
int main(int argc, char* argv[]){
20+
initialize_global_logger(
21+
FileLoggerConfig{
22+
.file_path = "./SerialProgramsCommandLine.log",
23+
.flush_when_exit = true
24+
}
25+
);
2026
// // Set up output redirection for logging
2127
// OutputRedirector redirect_stdout(std::cout, "stdout", Color());
2228
// OutputRedirector redirect_stderr(std::cerr, "stderr", COLOR_RED);

0 commit comments

Comments
 (0)