From 8c53adf4abe764b425da2f785508d518ae4f9ad9 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 13 Jan 2022 20:23:08 -0800 Subject: [PATCH] swaglog: add daemon name to context (#23518) * cleanup first * add daemon name * add test * fix * daemon name --- selfdrive/common/swaglog.cc | 24 +++++++++--------------- selfdrive/common/swaglog.h | 2 -- selfdrive/common/tests/test_swaglog.cc | 5 ++++- selfdrive/manager/process.py | 6 ++++-- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/selfdrive/common/swaglog.cc b/selfdrive/common/swaglog.cc index d8c7d036d36b14..aae4fb6e708677 100644 --- a/selfdrive/common/swaglog.cc +++ b/selfdrive/common/swaglog.cc @@ -35,10 +35,6 @@ LogState::~LogState() { static LogState s = {}; -static void cloudlog_bind_locked(const char* k, const char* v) { - s.ctx_j[k] = v; -} - static void cloudlog_init() { if (s.inited) return; s.ctx_j = json11::Json::object {}; @@ -65,18 +61,22 @@ static void cloudlog_init() { // openpilot bindings char* dongle_id = getenv("DONGLE_ID"); if (dongle_id) { - cloudlog_bind_locked("dongle_id", dongle_id); + s.ctx_j["dongle_id"] = dongle_id; } - cloudlog_bind_locked("version", COMMA_VERSION); + char* daemon_name = getenv("MANAGER_DAEMON"); + if (daemon_name) { + s.ctx_j["daemon"] = daemon_name; + } + s.ctx_j["version"] = COMMA_VERSION; s.ctx_j["dirty"] = !getenv("CLEAN"); // device type if (Hardware::EON()) { - cloudlog_bind_locked("device", "eon"); + s.ctx_j["device"] = "eon"; } else if (Hardware::TICI()) { - cloudlog_bind_locked("device", "tici"); + s.ctx_j["device"] = "tici"; } else { - cloudlog_bind_locked("device", "pc"); + s.ctx_j["device"] = "pc"; } s.inited = true; @@ -116,9 +116,3 @@ void cloudlog_e(int levelnum, const char* filename, int lineno, const char* func log(levelnum, filename, lineno, func, msg_buf, log_s); free(msg_buf); } - -void cloudlog_bind(const char* k, const char* v) { - std::lock_guard lk(s.lock); - cloudlog_init(); - cloudlog_bind_locked(k, v); -} diff --git a/selfdrive/common/swaglog.h b/selfdrive/common/swaglog.h index 9a1d3c0a6758a1..a508bcac1aeb46 100644 --- a/selfdrive/common/swaglog.h +++ b/selfdrive/common/swaglog.h @@ -11,8 +11,6 @@ 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); - #define cloudlog(lvl, fmt, ...) cloudlog_e(lvl, __FILE__, __LINE__, \ __func__, \ fmt, ## __VA_ARGS__) diff --git a/selfdrive/common/tests/test_swaglog.cc b/selfdrive/common/tests/test_swaglog.cc index e4d333fd442746..025ee51dbfefe6 100644 --- a/selfdrive/common/tests/test_swaglog.cc +++ b/selfdrive/common/tests/test_swaglog.cc @@ -10,6 +10,7 @@ #include "selfdrive/hardware/hw.h" const char *SWAGLOG_ADDR = "ipc:///tmp/logmessage"; +std::string daemon_name = "testy"; std::string dongle_id = "test_dongle_id"; void log_thread(int msg, int msg_cnt) { @@ -43,9 +44,10 @@ void recv_log(void *zctx, int thread_cnt, int thread_msg_cnt) { REQUIRE(msg["levelnum"].int_value() == CLOUDLOG_DEBUG); REQUIRE_THAT(msg["filename"].string_value(), Catch::Contains("test_swaglog.cc")); REQUIRE(msg["funcname"].string_value() == "log_thread"); - REQUIRE(msg["lineno"].int_value() == 17); + REQUIRE(msg["lineno"].int_value() == 18); // TODO: do this automatically auto ctx = msg["ctx"]; + REQUIRE(ctx["daemon"].string_value() == daemon_name); REQUIRE(ctx["dongle_id"].string_value() == dongle_id); REQUIRE(ctx["version"].string_value() == COMMA_VERSION); REQUIRE(ctx["dirty"].bool_value() == true); @@ -68,6 +70,7 @@ void recv_log(void *zctx, int thread_cnt, int thread_msg_cnt) { } TEST_CASE("swaglog") { + setenv("MANAGER_DAEMON", daemon_name.c_str(), 1); setenv("DONGLE_ID", dongle_id.c_str(), 1); setenv("dirty", "1", 1); const int thread_cnt = 5; diff --git a/selfdrive/manager/process.py b/selfdrive/manager/process.py index 2dcb42f339c4e2..42dbab05ea142f 100644 --- a/selfdrive/manager/process.py +++ b/selfdrive/manager/process.py @@ -48,7 +48,9 @@ def launcher(proc: str, name: str) -> None: raise -def nativelauncher(pargs: List[str], cwd: str) -> None: +def nativelauncher(pargs: List[str], cwd: str, name: str) -> None: + os.environ['MANAGER_DAEMON'] = name + # exec the process os.chdir(cwd) os.execvp(pargs[0], pargs) @@ -203,7 +205,7 @@ def start(self) -> None: cwd = os.path.join(BASEDIR, self.cwd) cloudlog.info(f"starting process {self.name}") - self.proc = Process(name=self.name, target=nativelauncher, args=(self.cmdline, cwd)) + self.proc = Process(name=self.name, target=nativelauncher, args=(self.cmdline, cwd, self.name)) self.proc.start() self.watchdog_seen = False self.shutting_down = False