blob: a53d3372227982dfac20affda719eeb7119e744f [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);
31 aos::SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuh92547522019-12-28 14:33:43 -080032 reader.Register(&log_reader_factory);
James Kuszmaul38735e82019-12-07 16:42:06 -080033
34 std::unique_ptr<aos::EventLoop> printer_event_loop =
35 log_reader_factory.MakeEventLoop("printer");
36 printer_event_loop->SkipTimingReport();
37
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) {
47 LOG(INFO) << "Listening on " << name << " " << type;
48
49 CHECK_NOTNULL(channel->schema());
50 printer_event_loop->MakeRawWatcher(
51 channel, [channel](const aos::Context &context, const void *message) {
52 // Print the flatbuffer out to stdout, both to remove the
53 // unnecessary cruft from glog and to allow the user to readily
54 // redirect just the logged output independent of any debugging
55 // information on stderr.
Austin Schuh92547522019-12-28 14:33:43 -080056 std::cout << context.realtime_event_time << " ("
57 << context.monotonic_event_time << ") "
58 << channel->name()->c_str() << ' '
59 << channel->type()->c_str() << ": "
James Kuszmaul38735e82019-12-07 16:42:06 -080060 << aos::FlatbufferToJson(
61 channel->schema(),
62 static_cast<const uint8_t *>(message))
63 << '\n';
64 });
65 found_channel = true;
66 }
67 }
68
69 if (!found_channel) {
70 LOG(FATAL) << "Could not find any channels";
71 }
72
73 log_reader_factory.Run();
74
75 reader.Deregister();
76
77 aos::Cleanup();
78 return 0;
79}