blob: 45f0811c8545476cf93c0d6e6df9fe08446934c0 [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);
Austin Schuh15649d62019-12-28 16:36:38 -080031 aos::SimulatedEventLoopFactory log_reader_factory(reader.configuration(),
32 reader.node());
Austin Schuh92547522019-12-28 14:33:43 -080033 reader.Register(&log_reader_factory);
James Kuszmaul38735e82019-12-07 16:42:06 -080034
35 std::unique_ptr<aos::EventLoop> printer_event_loop =
36 log_reader_factory.MakeEventLoop("printer");
37 printer_event_loop->SkipTimingReport();
38
39 bool found_channel = false;
40 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
41 reader.configuration()->channels();
42 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
43 const aos::Channel *channel = channels->Get(i);
44 const flatbuffers::string_view name = channel->name()->string_view();
45 const flatbuffers::string_view type = channel->type()->string_view();
46 if (name.find(FLAGS_name) != std::string::npos &&
47 type.find(FLAGS_type) != std::string::npos) {
Austin Schuh15649d62019-12-28 16:36:38 -080048 if (!aos::configuration::ChannelIsReadableOnNode(
49 channel, printer_event_loop->node())) {
50 continue;
51 }
James Kuszmaul38735e82019-12-07 16:42:06 -080052 LOG(INFO) << "Listening on " << name << " " << type;
53
54 CHECK_NOTNULL(channel->schema());
55 printer_event_loop->MakeRawWatcher(
56 channel, [channel](const aos::Context &context, const void *message) {
57 // Print the flatbuffer out to stdout, both to remove the
58 // unnecessary cruft from glog and to allow the user to readily
59 // redirect just the logged output independent of any debugging
60 // information on stderr.
Austin Schuh15649d62019-12-28 16:36:38 -080061 if (context.monotonic_remote_time != context.monotonic_event_time) {
62 std::cout << context.realtime_remote_time << " ("
63 << context.monotonic_remote_time << ") delivered "
64 << context.realtime_event_time << " ("
65 << context.monotonic_event_time << ") "
66 << channel->name()->c_str() << ' '
67 << channel->type()->c_str() << ": "
68 << aos::FlatbufferToJson(
69 channel->schema(),
70 static_cast<const uint8_t *>(message))
71 << '\n';
72 } else {
73 std::cout << context.realtime_event_time << " ("
74 << context.monotonic_event_time << ") "
75 << channel->name()->c_str() << ' '
76 << channel->type()->c_str() << ": "
77 << aos::FlatbufferToJson(
78 channel->schema(),
79 static_cast<const uint8_t *>(message))
80 << '\n';
81 }
James Kuszmaul38735e82019-12-07 16:42:06 -080082 });
83 found_channel = true;
84 }
85 }
86
87 if (!found_channel) {
88 LOG(FATAL) << "Could not find any channels";
89 }
90
91 log_reader_factory.Run();
92
93 reader.Deregister();
94
95 aos::Cleanup();
96 return 0;
97}