blob: f2427b828a0f87d7cfd53c5e14ae4b60ae0d30d9 [file] [log] [blame]
James Kuszmaul38735e82019-12-07 16:42:06 -08001#include <iostream>
2
3#include "aos/configuration.h"
4#include "aos/events/logging/logger.h"
5#include "aos/events/simulated_event_loop.h"
6#include "aos/init.h"
7#include "aos/json_to_flatbuffer.h"
8#include "gflags/gflags.h"
9
10DEFINE_string(logfile, "/tmp/logfile.bfbs",
11 "Name of the logfile to read from.");
12DEFINE_string(
13 name, "",
14 "Name to match for printing out channels. Empty means no name filter.");
15DEFINE_string(type, "",
16 "Channel type to match for printing out channels. Empty means no "
17 "type filter.");
18int main(int argc, char **argv) {
19 gflags::SetUsageMessage(
20 "This program provides a basic interface to dump data from a logfile to "
21 "stdout. Given a logfile, channel name filter, and type filter, it will "
22 "print all the messages in the logfile matching the filters. The message "
23 "filters work by taking the values of --name and --type and printing any "
24 "channel whose name contains --name as a substr and whose type contains "
25 "--type as a substr. Not specifying --name or --type leaves them free. "
26 "Calling this program without --name or --type specified prints out all "
27 "the logged data.");
28 aos::InitGoogle(&argc, &argv);
29
30 aos::logger::LogReader reader(FLAGS_logfile);
James Kuszmaul84ff3e52020-01-03 19:48:53 -080031 reader.Register();
James Kuszmaul38735e82019-12-07 16:42:06 -080032
33 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuhac0771c2020-01-07 18:36:30 -080034 reader.event_loop_factory()->MakeEventLoop("printer", reader.node());
James Kuszmaul38735e82019-12-07 16:42:06 -080035 printer_event_loop->SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080036 printer_event_loop->SkipAosLog();
James Kuszmaul38735e82019-12-07 16:42:06 -080037
38 bool found_channel = false;
39 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
40 reader.configuration()->channels();
41 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
42 const aos::Channel *channel = channels->Get(i);
43 const flatbuffers::string_view name = channel->name()->string_view();
44 const flatbuffers::string_view type = channel->type()->string_view();
45 if (name.find(FLAGS_name) != std::string::npos &&
46 type.find(FLAGS_type) != std::string::npos) {
Austin Schuh15649d62019-12-28 16:36:38 -080047 if (!aos::configuration::ChannelIsReadableOnNode(
48 channel, printer_event_loop->node())) {
49 continue;
50 }
James Kuszmaul38735e82019-12-07 16:42:06 -080051 LOG(INFO) << "Listening on " << name << " " << type;
52
53 CHECK_NOTNULL(channel->schema());
54 printer_event_loop->MakeRawWatcher(
55 channel, [channel](const aos::Context &context, const void *message) {
56 // Print the flatbuffer out to stdout, both to remove the
57 // unnecessary cruft from glog and to allow the user to readily
58 // redirect just the logged output independent of any debugging
59 // information on stderr.
Austin Schuh15649d62019-12-28 16:36:38 -080060 if (context.monotonic_remote_time != context.monotonic_event_time) {
61 std::cout << context.realtime_remote_time << " ("
62 << context.monotonic_remote_time << ") delivered "
63 << context.realtime_event_time << " ("
64 << context.monotonic_event_time << ") "
65 << channel->name()->c_str() << ' '
66 << channel->type()->c_str() << ": "
67 << aos::FlatbufferToJson(
68 channel->schema(),
69 static_cast<const uint8_t *>(message))
Austin Schuhac0771c2020-01-07 18:36:30 -080070 << std::endl;
Austin Schuh15649d62019-12-28 16:36:38 -080071 } else {
72 std::cout << context.realtime_event_time << " ("
73 << context.monotonic_event_time << ") "
74 << channel->name()->c_str() << ' '
75 << channel->type()->c_str() << ": "
76 << aos::FlatbufferToJson(
77 channel->schema(),
78 static_cast<const uint8_t *>(message))
Austin Schuhac0771c2020-01-07 18:36:30 -080079 << std::endl;
Austin Schuh15649d62019-12-28 16:36:38 -080080 }
James Kuszmaul38735e82019-12-07 16:42:06 -080081 });
82 found_channel = true;
83 }
84 }
85
86 if (!found_channel) {
87 LOG(FATAL) << "Could not find any channels";
88 }
89
James Kuszmaul84ff3e52020-01-03 19:48:53 -080090 reader.event_loop_factory()->Run();
James Kuszmaul38735e82019-12-07 16:42:06 -080091
92 aos::Cleanup();
93 return 0;
94}