forked from mawei/dp
1
0
Fork 0
dp/selfdrive/logcatd/logcatd_systemd.cc

72 lines
1.9 KiB
C++
Raw Permalink Normal View History

2021-05-15 09:20:48 +08:00
#include <systemd/sd-journal.h>
2020-10-02 09:45:44 +08:00
#include <cassert>
2021-02-24 08:46:22 +08:00
#include <csignal>
2020-10-02 09:45:44 +08:00
#include <map>
2021-05-15 09:20:48 +08:00
#include <string>
2020-10-02 09:45:44 +08:00
#include "json11.hpp"
2021-05-15 09:20:48 +08:00
#include "cereal/messaging/messaging.h"
#include "selfdrive/common/timing.h"
#include "selfdrive/common/util.h"
2020-10-02 09:45:44 +08:00
2021-02-24 08:46:22 +08:00
ExitHandler do_exit;
2020-10-02 09:45:44 +08:00
int main(int argc, char *argv[]) {
2021-02-24 08:46:22 +08:00
2020-10-02 09:45:44 +08:00
PubMaster pm({"androidLog"});
sd_journal *journal;
2021-02-24 08:46:22 +08:00
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);
2020-10-02 09:45:44 +08:00
2021-02-24 08:46:22 +08:00
while (!do_exit) {
err = sd_journal_next(journal);
assert(err >= 0);
2020-10-02 09:45:44 +08:00
// Wait for new message if we didn't receive anything
2021-07-15 08:34:36 +08:00
if (err == 0) {
2021-02-24 08:46:22 +08:00
err = sd_journal_wait(journal, 1000 * 1000);
assert (err >= 0);
continue; // Try again
2020-10-02 09:45:44 +08:00
}
uint64_t timestamp = 0;
2021-02-24 08:46:22 +08:00
err = sd_journal_get_realtime_usec(journal, &timestamp);
assert(err >= 0);
2020-10-02 09:45:44 +08:00
const void *data;
size_t length;
std::map<std::string, std::string> kv;
2021-07-15 08:34:36 +08:00
SD_JOURNAL_FOREACH_DATA(journal, data, length) {
2020-10-02 09:45:44 +08:00
std::string str((char*)data, length);
// Split "KEY=VALUE"" on "=" and put in map
std::size_t found = str.find("=");
2021-07-15 08:34:36 +08:00
if (found != std::string::npos) {
2020-10-02 09:45:44 +08:00
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;
}