mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-06-27 03:02:05 +08:00
swaglog: delay creating zmq socket to first use - fix modeld crash in sim (#24271)
* Initialize zmq socket later with initialize method
* empty lines
* Add lock and move initialize function
* Check for initialized
old-commit-hash: 6ab7b2f325
This commit is contained in:
@@ -17,6 +17,9 @@ class StatlogState : public LogState {
|
||||
static StatlogState s = {};
|
||||
|
||||
static void log(const char* metric_type, const char* metric, const char* fmt, ...) {
|
||||
std::lock_guard lk(s.lock);
|
||||
if (!s.initialized) s.initialize();
|
||||
|
||||
char* value_buf = nullptr;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
@@ -21,7 +21,6 @@ class SwaglogState : public LogState {
|
||||
public:
|
||||
SwaglogState() : LogState("ipc:///tmp/logmessage") {}
|
||||
|
||||
bool initialized = false;
|
||||
json11::Json::object ctx_j;
|
||||
|
||||
inline void initialize() {
|
||||
@@ -56,8 +55,7 @@ class SwaglogState : public LogState {
|
||||
} else {
|
||||
ctx_j["device"] = "pc";
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
LogState::initialize();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -113,7 +111,7 @@ void cloudlog_t_common(int levelnum, const char* filename, int lineno, const cha
|
||||
char* msg_buf = nullptr;
|
||||
int ret = vasprintf(&msg_buf, fmt, args);
|
||||
if (ret <= 0 || !msg_buf) return;
|
||||
json11::Json::object tspt_j = json11::Json::object{
|
||||
json11::Json::object tspt_j = json11::Json::object{
|
||||
{"event", msg_buf},
|
||||
{"time", std::to_string(nanos_since_boot())}
|
||||
};
|
||||
|
||||
+16
-6
@@ -168,12 +168,18 @@ void update_max_atomic(std::atomic<T>& max, T const& value) {
|
||||
|
||||
class LogState {
|
||||
public:
|
||||
bool initialized = false;
|
||||
std::mutex lock;
|
||||
void *zctx;
|
||||
void *sock;
|
||||
void *zctx = nullptr;
|
||||
void *sock = nullptr;
|
||||
int print_level;
|
||||
const char* endpoint;
|
||||
|
||||
LogState(const char* endpoint) {
|
||||
LogState(const char* _endpoint) {
|
||||
endpoint = _endpoint;
|
||||
}
|
||||
|
||||
inline void initialize() {
|
||||
zctx = zmq_ctx_new();
|
||||
sock = zmq_socket(zctx, ZMQ_PUSH);
|
||||
|
||||
@@ -182,9 +188,13 @@ class LogState {
|
||||
zmq_setsockopt(sock, ZMQ_LINGER, &timeout, sizeof(timeout));
|
||||
|
||||
zmq_connect(sock, endpoint);
|
||||
};
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
~LogState() {
|
||||
zmq_close(sock);
|
||||
zmq_ctx_destroy(zctx);
|
||||
if (initialized) {
|
||||
zmq_close(sock);
|
||||
zmq_ctx_destroy(zctx);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user