refactor swaglog (#30795)

pull/30804/head
Dean Lee 1 year ago committed by GitHub
parent adca970ca9
commit 3646ca0a14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 65
      common/swaglog.cc
  2. 34
      common/util.h

@ -5,30 +5,28 @@
#include "common/swaglog.h" #include "common/swaglog.h"
#include <cassert> #include <cassert>
#include <cstdarg>
#include <cstring>
#include <limits> #include <limits>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <zmq.h> #include <zmq.h>
#include "third_party/json11/json11.hpp" #include "third_party/json11/json11.hpp"
#include "common/util.h"
#include "common/version.h" #include "common/version.h"
#include "system/hardware/hw.h" #include "system/hardware/hw.h"
class SwaglogState : public LogState { class SwaglogState {
public: public:
SwaglogState() : LogState(Path::swaglog_ipc().c_str()) {} SwaglogState() {
zctx = zmq_ctx_new();
sock = zmq_socket(zctx, ZMQ_PUSH);
json11::Json::object ctx_j; // Timeout on shutdown for messages to be received by the logging process
int timeout = 100;
zmq_setsockopt(sock, ZMQ_LINGER, &timeout, sizeof(timeout));
zmq_connect(sock, Path::swaglog_ipc().c_str());
inline void initialize() {
ctx_j = json11::Json::object {};
print_level = CLOUDLOG_WARNING; print_level = CLOUDLOG_WARNING;
const char* print_lvl = getenv("LOGPRINT"); if (const char* print_lvl = getenv("LOGPRINT")) {
if (print_lvl) {
if (strcmp(print_lvl, "debug") == 0) { if (strcmp(print_lvl, "debug") == 0) {
print_level = CLOUDLOG_DEBUG; print_level = CLOUDLOG_DEBUG;
} else if (strcmp(print_lvl, "info") == 0) { } else if (strcmp(print_lvl, "info") == 0) {
@ -38,39 +36,44 @@ class SwaglogState : public LogState {
} }
} }
// openpilot bindings ctx_j = json11::Json::object{};
char* dongle_id = getenv("DONGLE_ID"); if (char* dongle_id = getenv("DONGLE_ID")) {
if (dongle_id) {
ctx_j["dongle_id"] = dongle_id; ctx_j["dongle_id"] = dongle_id;
} }
char* daemon_name = getenv("MANAGER_DAEMON"); if (char* daemon_name = getenv("MANAGER_DAEMON")) {
if (daemon_name) {
ctx_j["daemon"] = daemon_name; ctx_j["daemon"] = daemon_name;
} }
ctx_j["version"] = COMMA_VERSION; ctx_j["version"] = COMMA_VERSION;
ctx_j["dirty"] = !getenv("CLEAN"); ctx_j["dirty"] = !getenv("CLEAN");
// device type
ctx_j["device"] = Hardware::get_name(); ctx_j["device"] = Hardware::get_name();
LogState::initialize();
} }
~SwaglogState() {
zmq_close(sock);
zmq_ctx_destroy(zctx);
}
void log(int levelnum, const char* filename, int lineno, const char* func, const char* msg, const std::string& log_s) {
std::lock_guard lk(lock);
if (levelnum >= print_level) {
printf("%s: %s\n", filename, msg);
}
zmq_send(sock, log_s.data(), log_s.length(), ZMQ_NOBLOCK);
}
std::mutex lock;
void* zctx = nullptr;
void* sock = nullptr;
int print_level;
json11::Json::object ctx_j;
}; };
static SwaglogState s = {};
bool LOG_TIMESTAMPS = getenv("LOG_TIMESTAMPS"); bool LOG_TIMESTAMPS = getenv("LOG_TIMESTAMPS");
uint32_t NO_FRAME_ID = std::numeric_limits<uint32_t>::max(); uint32_t NO_FRAME_ID = std::numeric_limits<uint32_t>::max();
static void log(int levelnum, const char* filename, int lineno, const char* func, const char* msg, const std::string& log_s) {
if (levelnum >= s.print_level) {
printf("%s: %s\n", filename, msg);
}
zmq_send(s.sock, log_s.data(), log_s.length(), ZMQ_NOBLOCK);
}
static void cloudlog_common(int levelnum, const char* filename, int lineno, const char* func, static void cloudlog_common(int levelnum, const char* filename, int lineno, const char* func,
char* msg_buf, const json11::Json::object &msg_j={}) { char* msg_buf, const json11::Json::object &msg_j={}) {
std::lock_guard lk(s.lock); static SwaglogState s;
if (!s.initialized) s.initialize();
json11::Json::object log_j = json11::Json::object { json11::Json::object log_j = json11::Json::object {
{"ctx", s.ctx_j}, {"ctx", s.ctx_j},
@ -89,7 +92,7 @@ static void cloudlog_common(int levelnum, const char* filename, int lineno, cons
std::string log_s; std::string log_s;
log_s += (char)levelnum; log_s += (char)levelnum;
((json11::Json)log_j).dump(log_s); ((json11::Json)log_j).dump(log_s);
log(levelnum, filename, lineno, func, msg_buf, log_s); s.log(levelnum, filename, lineno, func, msg_buf, log_s);
free(msg_buf); free(msg_buf);
} }

@ -3,7 +3,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <zmq.h>
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
@ -180,36 +179,3 @@ void update_max_atomic(std::atomic<T>& max, T const& value) {
T prev = max; T prev = max;
while (prev < value && !max.compare_exchange_weak(prev, value)) {} while (prev < value && !max.compare_exchange_weak(prev, value)) {}
} }
class LogState {
public:
bool initialized = false;
std::mutex lock;
void *zctx = nullptr;
void *sock = nullptr;
int print_level;
std::string endpoint;
LogState(std::string _endpoint) {
endpoint = _endpoint;
}
inline void initialize() {
zctx = zmq_ctx_new();
sock = zmq_socket(zctx, ZMQ_PUSH);
// Timeout on shutdown for messages to be received by the logging process
int timeout = 100;
zmq_setsockopt(sock, ZMQ_LINGER, &timeout, sizeof(timeout));
zmq_connect(sock, endpoint.c_str());
initialized = true;
}
~LogState() {
if (initialized) {
zmq_close(sock);
zmq_ctx_destroy(zctx);
}
}
};

Loading…
Cancel
Save