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 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<str_ptr> buffer;

View File

@ -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(&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_cond_destroy(&not_empty);
}
}
void logger_file::init(std::string file, int max_length_) {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&not_empty, NULL);
pthread_cond_init(&not_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(&not_empty, &mutex);
if(!is_running) return; // Thread done. Messages in buffer will be handled in flush.
}
str_ptr s = buffer.front();
pthread_cond_signal(&not_full);
int n = 0;
if(logfile)
n = fprintf(logfile, "%s", s->c_str());