Skip to content

Commit

Permalink
swaglog: add daemon name to context (commaai#23518)
Browse files Browse the repository at this point in the history
* cleanup first

* add daemon name

* add test

* fix

* daemon name
  • Loading branch information
adeebshihadeh authored Jan 14, 2022
1 parent feae375 commit 8c53adf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
24 changes: 9 additions & 15 deletions selfdrive/common/swaglog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
2 changes: 0 additions & 2 deletions selfdrive/common/swaglog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
5 changes: 4 additions & 1 deletion selfdrive/common/tests/test_swaglog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions selfdrive/manager/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c53adf

Please sign in to comment.