Merge pull request #234 from softwareradiosystems/log_fix

Log fix
This commit is contained in:
Andre Puschmann 2018-07-06 13:07:55 +02:00 committed by GitHub
commit 355330dab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 13 deletions

View File

@ -64,11 +64,9 @@ private:
int64_t max_length; int64_t max_length;
int64_t cur_length; int64_t cur_length;
FILE* logfile; FILE* logfile;
bool inited; bool is_running;
bool not_done;
std::string filename; std::string filename;
pthread_cond_t not_empty; pthread_cond_t not_empty;
pthread_cond_t not_full;
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_t thread; pthread_t thread;
std::deque<str_ptr> buffer; std::deque<str_ptr> buffer;

View File

@ -34,38 +34,41 @@ using namespace std;
namespace srslte{ namespace srslte{
logger_file::logger_file() logger_file::logger_file()
:inited(false) :logfile(NULL)
,logfile(NULL) ,is_running(false)
,not_done(true)
,cur_length(0) ,cur_length(0)
,max_length(0) ,max_length(0)
{} {}
logger_file::~logger_file() { logger_file::~logger_file() {
not_done = false; if(is_running) {
if(inited) {
log(new std::string("Closing log\n")); log(new std::string("Closing log\n"));
pthread_mutex_lock(&mutex);
is_running = false;
pthread_cond_signal(&not_empty); // wakeup thread and let it terminate
pthread_mutex_unlock(&mutex);
wait_thread_finish(); wait_thread_finish();
flush(); flush();
if (logfile) { if (logfile) {
fclose(logfile); fclose(logfile);
} }
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&not_empty);
} }
} }
void logger_file::init(std::string file, int max_length_) { void logger_file::init(std::string file, int max_length_) {
pthread_mutex_init(&mutex, NULL); pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&not_empty, NULL); pthread_cond_init(&not_empty, NULL);
pthread_cond_init(&not_full, NULL);
max_length = (int64_t)max_length_*1024; max_length = (int64_t)max_length_*1024;
name_idx = 0; name_idx = 0;
filename = file; filename = file;
logfile = fopen(filename.c_str(), "w"); 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"); printf("Error: could not create log file, no messages will be logged!\n");
} }
is_running = true;
start(-2); start(-2);
inited = true;
} }
void logger_file::log(const char *msg) { void logger_file::log(const char *msg) {
@ -80,13 +83,13 @@ void logger_file::log(str_ptr msg) {
} }
void logger_file::run_thread() { void logger_file::run_thread() {
while(not_done) { while(is_running) {
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
while(buffer.empty()) { while(buffer.empty()) {
pthread_cond_wait(&not_empty, &mutex); pthread_cond_wait(&not_empty, &mutex);
if(!is_running) return; // Thread done. Messages in buffer will be handled in flush.
} }
str_ptr s = buffer.front(); str_ptr s = buffer.front();
pthread_cond_signal(&not_full);
int n = 0; int n = 0;
if(logfile) if(logfile)
n = fprintf(logfile, "%s", s->c_str()); n = fprintf(logfile, "%s", s->c_str());