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
master
Andre Puschmann 5 years ago
parent 4869509c7b
commit 5189bbad69

@ -47,6 +47,7 @@ public:
logger_file(std::string file); logger_file(std::string file);
~logger_file(); ~logger_file();
void init(std::string file, int max_length = -1); void init(std::string file, int max_length = -1);
void stop();
// Implementation of log_out // Implementation of log_out
void log(unique_log_str_t msg); void log(unique_log_str_t msg);

@ -33,35 +33,52 @@ logger_file::logger_file() : logfile(NULL), is_running(false), cur_length(0), ma
pthread_cond_init(&not_empty, NULL); pthread_cond_init(&not_empty, NULL);
} }
logger_file::~logger_file() { logger_file::~logger_file()
if(is_running) { {
logger::log_char("Closing log\n"); stop();
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();
flush();
if (logfile) {
fclose(logfile);
}
}
pthread_mutex_destroy(&mutex); pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&not_empty); 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); if (is_running) {
pthread_cond_init(&not_empty, NULL); fprintf(stderr, "Error: logger thread is already running.\n");
return;
}
pthread_mutex_lock(&mutex);
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; is_running = true;
start(-2); 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(&not_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) 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()); unique_log_str_t s = std::move(buffer.front());
int n = 0; int n = 0;
if(logfile) if (logfile) {
n = fprintf(logfile, "%s", s->msg); n = fprintf(logfile, "%s", s->msg);
}
buffer.pop_front(); buffer.pop_front();
pthread_mutex_unlock(&mutex);
if (n > 0) { if (n > 0) {
cur_length += (int64_t) n; cur_length += (int64_t) n;
if (cur_length >= max_length && max_length > 0) { if (cur_length >= max_length && max_length > 0) {
@ -104,17 +123,20 @@ void logger_file::run_thread() {
cur_length = 0; cur_length = 0;
} }
} }
pthread_mutex_unlock(&mutex);
} }
} }
void logger_file::flush() { void logger_file::flush()
{
std::deque<unique_log_str_t>::iterator it; std::deque<unique_log_str_t>::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); unique_log_str_t s = std::move(*it);
if(logfile) if (logfile) {
fprintf(logfile, "%s", s->msg); fprintf(logfile, "%s", s->msg);
}
} }
buffer.clear();
} }
} // namespace srslte } // namespace srslte

Loading…
Cancel
Save