|
|
|
@ -6,7 +6,7 @@ |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
|
|
#include <pthread.h> |
|
|
|
|
#include <mutex> |
|
|
|
|
#include <zmq.h> |
|
|
|
|
|
|
|
|
|
#include "json11.hpp" |
|
|
|
@ -17,7 +17,7 @@ |
|
|
|
|
#include "swaglog.h" |
|
|
|
|
|
|
|
|
|
typedef struct LogState { |
|
|
|
|
pthread_mutex_t lock; |
|
|
|
|
std::mutex lock; |
|
|
|
|
bool inited; |
|
|
|
|
json11::Json::object ctx_j; |
|
|
|
|
void *zctx; |
|
|
|
@ -25,9 +25,7 @@ typedef struct LogState { |
|
|
|
|
int print_level; |
|
|
|
|
} LogState; |
|
|
|
|
|
|
|
|
|
static LogState s = { |
|
|
|
|
.lock = PTHREAD_MUTEX_INITIALIZER, |
|
|
|
|
}; |
|
|
|
|
static LogState s = {}; |
|
|
|
|
|
|
|
|
|
static void cloudlog_bind_locked(const char* k, const char* v) { |
|
|
|
|
s.ctx_j[k] = v; |
|
|
|
@ -65,7 +63,7 @@ static void cloudlog_init() { |
|
|
|
|
|
|
|
|
|
void cloudlog_e(int levelnum, const char* filename, int lineno, const char* func, |
|
|
|
|
const char* fmt, ...) { |
|
|
|
|
pthread_mutex_lock(&s.lock); |
|
|
|
|
std::lock_guard lk(s.lock); |
|
|
|
|
cloudlog_init(); |
|
|
|
|
|
|
|
|
|
char* msg_buf = NULL; |
|
|
|
@ -75,7 +73,6 @@ void cloudlog_e(int levelnum, const char* filename, int lineno, const char* func |
|
|
|
|
va_end(args); |
|
|
|
|
|
|
|
|
|
if (!msg_buf) { |
|
|
|
|
pthread_mutex_unlock(&s.lock); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -101,12 +98,10 @@ void cloudlog_e(int levelnum, const char* filename, int lineno, const char* func |
|
|
|
|
zmq_send(s.sock, &levelnum_c, 1, ZMQ_NOBLOCK | ZMQ_SNDMORE); |
|
|
|
|
zmq_send(s.sock, log_s.c_str(), log_s.length(), ZMQ_NOBLOCK); |
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&s.lock); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void cloudlog_bind(const char* k, const char* v) { |
|
|
|
|
pthread_mutex_lock(&s.lock); |
|
|
|
|
std::lock_guard lk(s.lock); |
|
|
|
|
cloudlog_init(); |
|
|
|
|
cloudlog_bind_locked(k, v); |
|
|
|
|
pthread_mutex_unlock(&s.lock); |
|
|
|
|
} |
|
|
|
|