@ -33,35 +33,52 @@ logger_file::logger_file() : logfile(NULL), is_running(false), cur_length(0), ma
pthread_cond_init ( & not_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 ( & not_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 ( & not_empty ) ;
}
void logger_file : : init ( std : : string file , int max_length_ ) {
pthread_mutex_init ( & mutex , NULL ) ;
pthread_cond_init ( & not_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 ( & 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 )
@ -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