-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_logs.cpp
More file actions
79 lines (73 loc) · 2.74 KB
/
print_logs.cpp
File metadata and controls
79 lines (73 loc) · 2.74 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
/// @file
///
/// A command line executable which connects to a Horus notification service and prints all
/// received logs to the standard output.
///
/// Note that the executable will continuously try to silently reconnect to the notification
/// service, so even if it is not running no error message will be printed.
#include <cstddef>
#include "examples/helpers.h"
#include "horus/pb/logs/message_pb.h"
#include "horus/rpc/services.h"
#include "horus/sdk.h"
#include "horus/sdk/logs.h"
#include "horus/strings/ansi.h"
#include "horus/strings/chrono.h" // IWYU pragma: keep
#include "horus/strings/pad.h"
#include "horus/strings/stdio.h"
#include "horus/strings/stringify.h"
#include "horus/types/span.h"
int main(int argc, const char* argv[]) {
const horus::Span<const char*> args{argv, static_cast<std::size_t>(argc)};
horus::RpcServices::ServiceResolutionMap service_map;
if (!horus::ParseArgs(service_map.notification, args)) {
return 1;
}
horus::Sdk sdk{service_map};
horus::SdkSubscription const subscription{
sdk.SubscribeToLogs({/*on_log_message=*/[](const horus::pb::LogMessage& log_message) {
const horus::sdk::Log log{log_message};
horus::ColoredFormat<const char*> (*format)(const char* const&){
&horus::MagentaColored<const char*>};
char chr{'?'};
switch (log.severity) {
case horus::sdk::Log::Severity::kDebug: {
format = &horus::BlackColored;
chr = 'D';
break;
}
case horus::sdk::Log::Severity::kInfo: {
format = &horus::GreenColored;
chr = 'I';
break;
}
case horus::sdk::Log::Severity::kWarning: {
format = &horus::YellowColored;
chr = 'W';
break;
}
case horus::sdk::Log::Severity::kError: {
format = &horus::RedColored;
chr = 'E';
break;
}
case horus::sdk::Log::Severity::kFatal: {
format = &horus::RedColored;
chr = 'F';
break;
}
case horus::sdk::Log::Severity::kUnknownWireValue:
case horus::sdk::Log::Severity::kUnspecified:
default:
break;
}
constexpr auto kSep = " | ";
const horus::ColoredFormat<const char*> sep{format(kSep)};
horus::StringifyTo(horus::StdoutSink(), horus::Iso8601{log.time}, sep, "#",
horus::PadLeftBy(3, log.id, '0'), sep, log.node_id, sep,
horus::Char{chr}, sep, log.message, "\n");
}})
.Wait()};
horus::WaitForTermination();
return 0;
}