mirror of https://github.com/PentHertz/srsLTE.git
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
This commit is contained in:
parent
4869509c7b
commit
5189bbad69
|
@ -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);
|
||||
|
||||
|
|
|
@ -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<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);
|
||||
if(logfile)
|
||||
if (logfile) {
|
||||
fprintf(logfile, "%s", s->msg);
|
||||
}
|
||||
}
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
} // namespace srslte
|
||||
|
|
Loading…
Reference in New Issue