From 5189bbad69962e2fd45202b266b58d6396d3443c Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Fri, 13 Sep 2019 15:33:12 +0200 Subject: [PATCH] add stop() to logger_file this allows to close and currently running log process, flush the content to the file, continue logging during that time, open a new file and write the new log entries to that new file basically it allows to use the logger_file multiple times without needing to destroy the object this is used in the UE tester to write each testcase into a separate log file --- lib/include/srslte/common/logger_file.h | 1 + lib/src/common/logger_file.cc | 66 ++++++++++++++++--------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/lib/include/srslte/common/logger_file.h b/lib/include/srslte/common/logger_file.h index 543fa4a1e..ed4457659 100644 --- a/lib/include/srslte/common/logger_file.h +++ b/lib/include/srslte/common/logger_file.h @@ -47,6 +47,7 @@ public: logger_file(std::string file); ~logger_file(); void init(std::string file, int max_length = -1); + void stop(); // Implementation of log_out void log(unique_log_str_t msg); diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index 7f0f63093..1ce449741 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -33,35 +33,52 @@ logger_file::logger_file() : logfile(NULL), is_running(false), cur_length(0), ma pthread_cond_init(¬_empty, NULL); } -logger_file::~logger_file() { - if(is_running) { - logger::log_char("Closing log\n"); - pthread_mutex_lock(&mutex); - is_running = false; - pthread_cond_signal(¬_empty); // wakeup thread and let it terminate - pthread_mutex_unlock(&mutex); - wait_thread_finish(); - flush(); - if (logfile) { - fclose(logfile); - } - } +logger_file::~logger_file() +{ + stop(); pthread_mutex_destroy(&mutex); pthread_cond_destroy(¬_empty); } void logger_file::init(std::string file, int max_length_) { - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(¬_empty, NULL); + if (is_running) { + fprintf(stderr, "Error: logger thread is already running.\n"); + return; + } + pthread_mutex_lock(&mutex); max_length = (int64_t)max_length_*1024; name_idx = 0; filename = file; logfile = fopen(filename.c_str(), "w"); - if(logfile == NULL) { + if (logfile == NULL) { printf("Error: could not create log file, no messages will be logged!\n"); } is_running = true; start(-2); + pthread_mutex_unlock(&mutex); +} + +void logger_file::stop() +{ + if (is_running) { + logger::log_char("Closing log\n"); + pthread_mutex_lock(&mutex); + is_running = false; + pthread_cond_signal(¬_empty); // wakeup thread and let it terminate + pthread_mutex_unlock(&mutex); + wait_thread_finish(); + pthread_mutex_lock(&mutex); + flush(); + if (logfile) { + fclose(logfile); + logfile = NULL; + } + pthread_mutex_unlock(&mutex); + } else { + pthread_mutex_lock(&mutex); + flush(); // flush even if thread isn't running anymore + pthread_mutex_unlock(&mutex); + } } void logger_file::log(unique_log_str_t msg) @@ -85,10 +102,12 @@ void logger_file::run_thread() { unique_log_str_t s = std::move(buffer.front()); int n = 0; - if(logfile) + if (logfile) { n = fprintf(logfile, "%s", s->msg); + } + buffer.pop_front(); - pthread_mutex_unlock(&mutex); + if (n > 0) { cur_length += (int64_t) n; if (cur_length >= max_length && max_length > 0) { @@ -104,17 +123,20 @@ void logger_file::run_thread() { cur_length = 0; } } + pthread_mutex_unlock(&mutex); } } -void logger_file::flush() { +void logger_file::flush() +{ std::deque::iterator it; - for(it=buffer.begin();it!=buffer.end();it++) - { + for (it = buffer.begin(); it != buffer.end(); it++) { unique_log_str_t s = std::move(*it); - if(logfile) + if (logfile) { fprintf(logfile, "%s", s->msg); + } } + buffer.clear(); } } // namespace srslte