core/log: Remove insane mutex trylocks
parent
e5533371e7
commit
1786c8b02a
|
@ -1,6 +1,8 @@
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "interface/mutex.h"
|
||||||
|
//#include "interface/thread.h"
|
||||||
#include "c55/os.h"
|
#include "c55/os.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
@ -13,9 +15,6 @@
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
#include <errno.h> // EBUSY
|
|
||||||
|
|
||||||
pthread_mutex_t log_mutex;
|
|
||||||
|
|
||||||
const int CORE_FATAL = 0;
|
const int CORE_FATAL = 0;
|
||||||
const int CORE_ERROR = 1;
|
const int CORE_ERROR = 1;
|
||||||
|
@ -31,6 +30,9 @@ static const bool use_colors = false;
|
||||||
static const bool use_colors = true;
|
static const bool use_colors = true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static interface::Mutex log_mutex;
|
||||||
|
//static interface::Thread *log_active_thread = nullptr;
|
||||||
|
|
||||||
static std::atomic_bool line_begin(true);
|
static std::atomic_bool line_begin(true);
|
||||||
static std::atomic_int current_level(0);
|
static std::atomic_int current_level(0);
|
||||||
static std::atomic_int max_level(CORE_INFO);
|
static std::atomic_int max_level(CORE_INFO);
|
||||||
|
@ -39,7 +41,6 @@ static FILE *file = NULL;
|
||||||
|
|
||||||
void log_init()
|
void log_init()
|
||||||
{
|
{
|
||||||
pthread_mutex_init(&log_mutex, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_set_max_level(int level)
|
void log_set_max_level(int level)
|
||||||
|
@ -54,23 +55,23 @@ int log_get_max_level()
|
||||||
|
|
||||||
void log_set_file(const char *path)
|
void log_set_file(const char *path)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&log_mutex);
|
log_mutex.lock();
|
||||||
file = fopen(path, "a");
|
file = fopen(path, "a");
|
||||||
if(file)
|
if(file)
|
||||||
fprintf(stderr, "Opened log file \"%s\"\n", path);
|
fprintf(stderr, "Opened log file \"%s\"\n", path);
|
||||||
else
|
else
|
||||||
log_w("__log", "Failed to open log file \"%s\"", path);
|
log_w("__log", "Failed to open log file \"%s\"", path);
|
||||||
pthread_mutex_unlock(&log_mutex);
|
log_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_close()
|
void log_close()
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&log_mutex);
|
log_mutex.lock();
|
||||||
if(file){
|
if(file){
|
||||||
fclose(file);
|
fclose(file);
|
||||||
file = NULL;
|
file = NULL;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&log_mutex);
|
log_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_nl_nolock()
|
void log_nl_nolock()
|
||||||
|
@ -94,17 +95,8 @@ void log_nl()
|
||||||
line_begin = true;
|
line_begin = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int r = pthread_mutex_trylock(&log_mutex);
|
interface::MutexScope ms(log_mutex);
|
||||||
if(r == EBUSY){
|
|
||||||
// Yo dawg, I heard you like logging so I'm launching a signal handler
|
|
||||||
// in the middle of your logging routine!
|
|
||||||
// Just call the damn function; it's fine enough for the rare occasions
|
|
||||||
// this happens in
|
|
||||||
log_nl_nolock();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
log_nl_nolock();
|
log_nl_nolock();
|
||||||
pthread_mutex_unlock(&log_mutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print(int level, const char *sys, const char *fmt, va_list va_args)
|
static void print(int level, const char *sys, const char *fmt, va_list va_args)
|
||||||
|
@ -157,7 +149,7 @@ static void print(int level, const char *sys, const char *fmt, va_list va_args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does not require any locking
|
// Does not require any locking
|
||||||
static void fallback_print(int level, const char *sys, const char *fmt,
|
/*static void fallback_print(int level, const char *sys, const char *fmt,
|
||||||
va_list va_args)
|
va_list va_args)
|
||||||
{
|
{
|
||||||
FILE *f = file;
|
FILE *f = file;
|
||||||
|
@ -167,30 +159,19 @@ static void fallback_print(int level, const char *sys, const char *fmt,
|
||||||
fprintf(f, "\033[0m"); // reset
|
fprintf(f, "\033[0m"); // reset
|
||||||
vfprintf(f, fmt, va_args);
|
vfprintf(f, fmt, va_args);
|
||||||
fprintf(f, "\n");
|
fprintf(f, "\n");
|
||||||
}
|
}*/
|
||||||
|
|
||||||
void log_(int level, const char *sys, const char *fmt, ...)
|
void log_(int level, const char *sys, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
if(level > max_level){ // Fast path
|
if(level > max_level){ // Fast path
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int r = pthread_mutex_trylock(&log_mutex);
|
interface::MutexScope ms(log_mutex);
|
||||||
if(r == EBUSY){
|
|
||||||
// Yo dawg, I heard you like logging so I'm launching a signal handler
|
|
||||||
// in the middle of your logging routine so you can synchronize your
|
|
||||||
// threads while you are synchronizing your threads
|
|
||||||
va_list va_args;
|
|
||||||
va_start(va_args, fmt);
|
|
||||||
fallback_print(level, sys, fmt, va_args);
|
|
||||||
va_end(va_args);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
va_list va_args;
|
va_list va_args;
|
||||||
va_start(va_args, fmt);
|
va_start(va_args, fmt);
|
||||||
print(level, sys, fmt, va_args);
|
print(level, sys, fmt, va_args);
|
||||||
log_nl_nolock();
|
log_nl_nolock();
|
||||||
va_end(va_args);
|
va_end(va_args);
|
||||||
pthread_mutex_unlock(&log_mutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_no_nl(int level, const char *sys, const char *fmt, ...)
|
void log_no_nl(int level, const char *sys, const char *fmt, ...)
|
||||||
|
@ -198,21 +179,10 @@ void log_no_nl(int level, const char *sys, const char *fmt, ...)
|
||||||
if(level > max_level){ // Fast path
|
if(level > max_level){ // Fast path
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int r = pthread_mutex_trylock(&log_mutex);
|
interface::MutexScope ms(log_mutex);
|
||||||
if(r == EBUSY){
|
|
||||||
// Yo dawg, I heard you like logging so I'm launching a signal handler
|
|
||||||
// in the middle of your logging routine so you can synchronize your
|
|
||||||
// threads while you are synchronizing your threads
|
|
||||||
va_list va_args;
|
|
||||||
va_start(va_args, fmt);
|
|
||||||
fallback_print(level, sys, fmt, va_args);
|
|
||||||
va_end(va_args);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
va_list va_args;
|
va_list va_args;
|
||||||
va_start(va_args, fmt);
|
va_start(va_args, fmt);
|
||||||
print(level, sys, fmt, va_args);
|
print(level, sys, fmt, va_args);
|
||||||
va_end(va_args);
|
va_end(va_args);
|
||||||
pthread_mutex_unlock(&log_mutex);
|
|
||||||
}
|
}
|
||||||
// vim: set noet ts=4 sw=4:
|
// vim: set noet ts=4 sw=4:
|
||||||
|
|
Loading…
Reference in New Issue