-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmesg.cpp
More file actions
79 lines (69 loc) · 2.42 KB
/
dmesg.cpp
File metadata and controls
79 lines (69 loc) · 2.42 KB
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
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include <cfbox/applet.hpp>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "dmesg",
.version = CFBOX_VERSION_STRING,
.one_line = "print kernel ring buffer",
.usage = "dmesg [-T]",
.options = " -T show timestamps in human-readable format",
.extra = "",
};
auto read_kmsg() -> std::vector<std::string> {
std::vector<std::string> lines;
cfbox::io::unique_file f(std::fopen("/var/log/dmesg", "r"));
if (!f) f.reset(std::fopen("/var/log/kern.log", "r"));
if (!f) {
CFBOX_ERR("dmesg", "cannot open kernel log");
return lines;
}
char buf[4096];
while (std::fgets(buf, sizeof(buf), f.get())) {
auto len = std::strlen(buf);
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) {
buf[--len] = '\0';
}
lines.emplace_back(buf);
}
return lines;
}
} // anonymous namespace
auto dmesg_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'T', false, "time"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool timestamps = parsed.has('T') || parsed.has_long("time");
auto lines = read_kmsg();
for (const auto& line : lines) {
if (timestamps) {
// Try to extract timestamp from [ 1234.567] format
auto bracket = line.find('[');
auto close_bracket = line.find(']');
if (bracket != std::string::npos && close_bracket != std::string::npos &&
close_bracket > bracket) {
auto ts_str = line.substr(bracket + 1, close_bracket - bracket - 1);
auto ts = std::stod(ts_str);
auto secs = static_cast<time_t>(ts);
auto tm = std::localtime(&secs);
char time_buf[32];
std::strftime(time_buf, sizeof(time_buf), "%b %d %H:%M:%S", tm);
std::printf("%s %s\n", time_buf, line.c_str());
} else {
std::printf("%s\n", line.c_str());
}
} else {
std::printf("%s\n", line.c_str());
}
}
return 0;
}