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