mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-08-03 08:41:32 +08:00
Rewrite logcatd in Python (#36111)
* Add Python logcatd implementation * lil more
This commit is contained in:
@@ -338,10 +338,6 @@ SConscript([
|
||||
'system/ubloxd/SConscript',
|
||||
'system/loggerd/SConscript',
|
||||
])
|
||||
if arch != "Darwin":
|
||||
SConscript([
|
||||
'system/logcatd/SConscript',
|
||||
])
|
||||
|
||||
if arch == "larch64":
|
||||
SConscript(['system/camerad/SConscript'])
|
||||
|
||||
@@ -59,7 +59,7 @@ PROCS = {
|
||||
"system.proclogd": 3.0,
|
||||
"system.logmessaged": 1.0,
|
||||
"system.tombstoned": 0,
|
||||
"./logcatd": 1.0,
|
||||
"system.journald": 1.0,
|
||||
"system.micd": 5.0,
|
||||
"system.timed": 0,
|
||||
"selfdrive.pandad.pandad": 0,
|
||||
|
||||
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
import cereal.messaging as messaging
|
||||
from openpilot.common.swaglog import cloudlog
|
||||
|
||||
|
||||
def main():
|
||||
pm = messaging.PubMaster(['androidLog'])
|
||||
cmd = ['journalctl', '-f', '-o', 'json']
|
||||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, text=True)
|
||||
assert proc.stdout is not None
|
||||
try:
|
||||
for line in proc.stdout:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
kv = json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
cloudlog.exception("failed to parse journalctl output")
|
||||
continue
|
||||
|
||||
msg = messaging.new_message('androidLog')
|
||||
entry = msg.androidLog
|
||||
entry.ts = int(kv.get('__REALTIME_TIMESTAMP', 0))
|
||||
entry.message = json.dumps(kv)
|
||||
if '_PID' in kv:
|
||||
entry.pid = int(kv['_PID'])
|
||||
if 'PRIORITY' in kv:
|
||||
entry.priority = int(kv['PRIORITY'])
|
||||
if 'SYSLOG_IDENTIFIER' in kv:
|
||||
entry.tag = kv['SYSLOG_IDENTIFIER']
|
||||
|
||||
pm.send('androidLog', msg)
|
||||
finally:
|
||||
proc.terminate()
|
||||
proc.wait()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1 +0,0 @@
|
||||
logcatd
|
||||
@@ -1,3 +0,0 @@
|
||||
Import('env', 'messaging', 'common')
|
||||
|
||||
env.Program('logcatd', 'logcatd_systemd.cc', LIBS=[messaging, common, 'systemd'])
|
||||
@@ -1,75 +0,0 @@
|
||||
#include <systemd/sd-journal.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <csignal>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "third_party/json11/json11.hpp"
|
||||
|
||||
#include "cereal/messaging/messaging.h"
|
||||
#include "common/timing.h"
|
||||
#include "common/util.h"
|
||||
|
||||
ExitHandler do_exit;
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
PubMaster pm({"androidLog"});
|
||||
|
||||
sd_journal *journal;
|
||||
int err = sd_journal_open(&journal, 0);
|
||||
assert(err >= 0);
|
||||
err = sd_journal_get_fd(journal); // needed so sd_journal_wait() works properly if files rotate
|
||||
assert(err >= 0);
|
||||
err = sd_journal_seek_tail(journal);
|
||||
assert(err >= 0);
|
||||
|
||||
// workaround for bug https://github.com/systemd/systemd/issues/9934
|
||||
// call sd_journal_previous_skip after sd_journal_seek_tail (like journalctl -f does) to makes things work.
|
||||
sd_journal_previous_skip(journal, 1);
|
||||
|
||||
while (!do_exit) {
|
||||
err = sd_journal_next(journal);
|
||||
assert(err >= 0);
|
||||
|
||||
// Wait for new message if we didn't receive anything
|
||||
if (err == 0) {
|
||||
err = sd_journal_wait(journal, 1000 * 1000);
|
||||
assert(err >= 0);
|
||||
continue; // Try again
|
||||
}
|
||||
|
||||
uint64_t timestamp = 0;
|
||||
err = sd_journal_get_realtime_usec(journal, ×tamp);
|
||||
assert(err >= 0);
|
||||
|
||||
const void *data;
|
||||
size_t length;
|
||||
std::map<std::string, std::string> kv;
|
||||
|
||||
SD_JOURNAL_FOREACH_DATA(journal, data, length) {
|
||||
std::string str((char*)data, length);
|
||||
|
||||
// Split "KEY=VALUE"" on "=" and put in map
|
||||
std::size_t found = str.find("=");
|
||||
if (found != std::string::npos) {
|
||||
kv[str.substr(0, found)] = str.substr(found + 1, std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
MessageBuilder msg;
|
||||
|
||||
// Build message
|
||||
auto androidEntry = msg.initEvent().initAndroidLog();
|
||||
androidEntry.setTs(timestamp);
|
||||
androidEntry.setMessage(json11::Json(kv).dump());
|
||||
if (kv.count("_PID")) androidEntry.setPid(std::atoi(kv["_PID"].c_str()));
|
||||
if (kv.count("PRIORITY")) androidEntry.setPriority(std::atoi(kv["PRIORITY"].c_str()));
|
||||
if (kv.count("SYSLOG_IDENTIFIER")) androidEntry.setTag(kv["SYSLOG_IDENTIFIER"]);
|
||||
|
||||
pm.send("androidLog", msg);
|
||||
}
|
||||
|
||||
sd_journal_close(journal);
|
||||
return 0;
|
||||
}
|
||||
@@ -71,8 +71,8 @@ procs = [
|
||||
|
||||
NativeProcess("camerad", "system/camerad", ["./camerad"], driverview, enabled=not WEBCAM),
|
||||
PythonProcess("webcamerad", "tools.webcam.camerad", driverview, enabled=WEBCAM),
|
||||
NativeProcess("logcatd", "system/logcatd", ["./logcatd"], only_onroad, platform.system() != "Darwin"),
|
||||
PythonProcess("proclogd", "system.proclogd", only_onroad, enabled=platform.system() != "Darwin"),
|
||||
PythonProcess("journald", "system.journald", only_onroad, platform.system() != "Darwin"),
|
||||
PythonProcess("micd", "system.micd", iscar),
|
||||
PythonProcess("timed", "system.timed", always_run, enabled=not PC),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user