mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-06-22 06:22:06 +08:00
swaglog: use std::mutex & remove extern C (#19817)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#ifndef SWAGLOG_H
|
||||
#define SWAGLOG_H
|
||||
#pragma once
|
||||
|
||||
#include "selfdrive/common/timing.h"
|
||||
|
||||
@@ -9,19 +8,11 @@
|
||||
#define CLOUDLOG_ERROR 40
|
||||
#define CLOUDLOG_CRITICAL 50
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void cloudlog_e(int levelnum, const char* filename, int lineno, const char* func,
|
||||
const char* fmt, ...) /*__attribute__ ((format (printf, 6, 7)))*/;
|
||||
|
||||
void cloudlog_bind(const char* k, const char* v);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#define cloudlog(lvl, fmt, ...) cloudlog_e(lvl, __FILE__, __LINE__, \
|
||||
__func__, \
|
||||
fmt, ## __VA_ARGS__)
|
||||
@@ -64,5 +55,3 @@ void cloudlog_bind(const char* k, const char* v);
|
||||
#define LOG_100(fmt, ...) cloudlog_rl(2, 100, CLOUDLOG_INFO, fmt, ## __VA_ARGS__)
|
||||
#define LOGW_100(fmt, ...) cloudlog_rl(2, 100, CLOUDLOG_WARNING, fmt, ## __VA_ARGS__)
|
||||
#define LOGE_100(fmt, ...) cloudlog_rl(2, 100, CLOUDLOG_ERROR, fmt, ## __VA_ARGS__)
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user