blob: 80ec8e7d7ca6b1180d207df8c117c6aeeeab45c0 [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();
36
37 bool found_channel = false;
38 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
39 reader.configuration()->channels();
40 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
41 const aos::Channel *channel = channels->Get(i);
42 const flatbuffers::string_view name = channel->name()->string_view();
43 const flatbuffers::string_view type = channel->type()->string_view();
44 if (name.find(FLAGS_name) != std::string::npos &&
45 type.find(FLAGS_type) != std::string::npos) {
Austin Schuh15649d62019-12-28 16:36:38 -080046 if (!aos::configuration::ChannelIsReadableOnNode(
47 channel, printer_event_loop->node())) {
48 continue;
49 }
James Kuszmaul38735e82019-12-07 16:42:06 -080050 LOG(INFO) << "Listening on " << name << " " << type;
51
52 CHECK_NOTNULL(channel->schema());
53 printer_event_loop->MakeRawWatcher(
54 channel, [channel](const aos::Context &context, const void *message) {
55 // Print the flatbuffer out to stdout, both to remove the
56 // unnecessary cruft from glog and to allow the user to readily
57 // redirect just the logged output independent of any debugging
58 // information on stderr.
Austin Schuh15649d62019-12-28 16:36:38 -080059 if (context.monotonic_remote_time != context.monotonic_event_time) {
60 std::cout << context.realtime_remote_time << " ("
61 << context.monotonic_remote_time << ") delivered "
62 << context.realtime_event_time << " ("
63 << context.monotonic_event_time << ") "
64 << channel->name()->c_str() << ' '
65 << channel->type()->c_str() << ": "
66 << aos::FlatbufferToJson(
67 channel->schema(),
68 static_cast<const uint8_t *>(message))
Austin Schuhac0771c2020-01-07 18:36:30 -080069 << std::endl;
Austin Schuh15649d62019-12-28 16:36:38 -080070 } else {
71 std::cout << context.realtime_event_time << " ("
72 << context.monotonic_event_time << ") "
73 << channel->name()->c_str() << ' '
74 << channel->type()->c_str() << ": "
75 << aos::FlatbufferToJson(
76 channel->schema(),
77 static_cast<const uint8_t *>(message))
Austin Schuhac0771c2020-01-07 18:36:30 -080078 << std::endl;
Austin Schuh15649d62019-12-28 16:36:38 -080079 }
James Kuszmaul38735e82019-12-07 16:42:06 -080080 });
81 found_channel = true;
82 }
83 }
84
85 if (!found_channel) {
86 LOG(FATAL) << "Could not find any channels";
87 }
88
James Kuszmaul84ff3e52020-01-03 19:48:53 -080089 reader.event_loop_factory()->Run();
James Kuszmaul38735e82019-12-07 16:42:06 -080090
91 aos::Cleanup();
92 return 0;
93}