diff --git a/lib/include/srslte/common/logger_file.h b/lib/include/srslte/common/logger_file.h index f0bd5a15b..956753f7a 100644 --- a/lib/include/srslte/common/logger_file.h +++ b/lib/include/srslte/common/logger_file.h @@ -64,11 +64,9 @@ private: int64_t max_length; int64_t cur_length; FILE* logfile; - bool inited; - bool not_done; + bool is_running; std::string filename; pthread_cond_t not_empty; - pthread_cond_t not_full; pthread_mutex_t mutex; pthread_t thread; std::deque buffer; diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index abbea8f93..d89dfac0c 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -34,38 +34,41 @@ using namespace std; namespace srslte{ logger_file::logger_file() - :inited(false) - ,logfile(NULL) - ,not_done(true) + :logfile(NULL) + ,is_running(false) ,cur_length(0) ,max_length(0) {} logger_file::~logger_file() { - not_done = false; - if(inited) { + if(is_running) { log(new std::string("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); } + 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); - pthread_cond_init(¬_full, NULL); 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); - inited = true; } void logger_file::log(const char *msg) { @@ -80,13 +83,13 @@ void logger_file::log(str_ptr msg) { } void logger_file::run_thread() { - while(not_done) { + while(is_running) { pthread_mutex_lock(&mutex); while(buffer.empty()) { pthread_cond_wait(¬_empty, &mutex); + if(!is_running) return; // Thread done. Messages in buffer will be handled in flush. } str_ptr s = buffer.front(); - pthread_cond_signal(¬_full); int n = 0; if(logfile) n = fprintf(logfile, "%s", s->c_str());