File tree Expand file tree Collapse file tree
SerialPrograms/Source/CommandLine Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -13,14 +13,12 @@ static FileLoggerConfig _global_logger_config;
1313
1414void 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
2018Logger& 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 };
Original file line number Diff line number Diff 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.
2020void 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.
2828Logger& global_logger_raw ();
2929
3030
Original file line number Diff line number Diff line change @@ -17,6 +17,12 @@ using namespace PokemonAutomation;
1717using namespace PokemonAutomation ::NintendoSwitch;
1818
1919int 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);
You can’t perform that action at this time.
0 commit comments