-
Notifications
You must be signed in to change notification settings - Fork 4
/
pivid_inspect_kmsg.cpp
109 lines (88 loc) · 3.58 KB
/
pivid_inspect_kmsg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Command line tool like 'dmesg' but with better timestamps.
#include <cinttypes>
#include <regex>
#include <string>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <fmt/core.h>
#include "logging_policy.h"
#include "unix_system.h"
namespace pivid {
std::string const levels[8] = {
"💥", "🔥", "🚨", "🛑", "⚠️", "🪧", "ℹ️", "🕸️"
};
extern "C" int main(int const argc, char const* const* const argv) {
std::string log_arg;
bool watch_arg;
CLI::App app("Read and print kernel log buffer");
app.add_option("--log", log_arg, "Log level/configuration");
app.add_flag("--watch", watch_arg, "Monitor ongoing logs");
CLI11_PARSE(app, argc, argv);
configure_logging(log_arg);
auto const logger = make_logger("pivid_inspect_kmsg");
try {
DEBUG(logger, "Opening /dev/kmsg");
auto const sys = global_system();
auto open_ret = sys->open("/dev/kmsg", O_RDWR | O_NONBLOCK);
if (open_ret.err == EACCES)
open_ret = sys->open("/dev/kmsg", O_RDONLY | O_NONBLOCK);
auto const kmsg = std::move(open_ret).ex("/dev/kmsg");
auto const now_rt = sys->clock(CLOCK_REALTIME);
auto const now_mt = sys->clock(CLOCK_MONOTONIC_RAW);
int64_t last_seq = -1;
auto const banner = fmt::format(
"=== pivid_inspect_kmsg {} (mt={:.3f}) ===",
format_realtime(now_rt), now_mt
);
auto const inject = fmt::format("<5> {}\n", banner);
(void) kmsg->write(inject.data(), inject.size());
fmt::print("{}\n", banner);
auto const mono = sys->make_flag(CLOCK_MONOTONIC);
for (;;) {
char record[8192];
TRACE(logger, "Reading log record");
auto const ret = kmsg->read(record, sizeof(record));
if (ret.err == EPIPE) continue; // kmsg skipped records
if (ret.err == EAGAIN) {
if (!watch_arg) break;
mono->sleep_until(sys->clock(CLOCK_MONOTONIC) + 0.1);
continue;
}
auto const len = ret.ex("read /dev/kmsg");
CHECK_RUNTIME(len > 0, "Bad /dev/kmsg read: {} bytes", len);
ASSERT(len <= int(sizeof(record)));
int tag = 0, prefix = 0;
int64_t seq = 0, micros = 0;
char flag = '\0';
int const scanned = sscanf(
record, "%d,%" SCNd64 ",%" SCNd64 ",%c;%n",
&tag, &seq, µs, &flag, &prefix
);
CHECK_RUNTIME(scanned == 4, "Bad kmsg record \"{}\"", record);
CHECK_RUNTIME(prefix > 0, "Bad kmsg prefix \"{}\"", record);
ASSERT(prefix < len);
auto const newline = std::find(record + prefix, record + len, '\n');
CHECK_RUNTIME(newline != record + len, "No newline \"{}\"", record);
*newline = '\0';
if (last_seq >= 0 && seq != last_seq + 1)
fmt::print("*** skipped {} records ***\n", seq - last_seq - 1);
last_seq = seq;
double const mt = micros * 1e-6;
double const rt = now_rt - now_mt + mt;
static std::regex const tab{"\\\\x09"};
fmt::print(
"{} {} {}\n",
(mt < now_mt - 43200 ? format_realtime : abbrev_realtime)(rt),
levels[tag % 8],
std::regex_replace(record + prefix, tab, "» ")
);
}
fmt::print("\n");
} catch (std::exception const& e) {
logger->critical("{}", e.what());
return 1;
}
return 0;
}
} // namespace pivid