James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame^] | 1 | #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 | |
| 10 | DEFINE_string(logfile, "/tmp/logfile.bfbs", |
| 11 | "Name of the logfile to read from."); |
| 12 | DEFINE_string( |
| 13 | name, "", |
| 14 | "Name to match for printing out channels. Empty means no name filter."); |
| 15 | DEFINE_string(type, "", |
| 16 | "Channel type to match for printing out channels. Empty means no " |
| 17 | "type filter."); |
| 18 | int 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()); |
| 32 | std::unique_ptr<aos::EventLoop> reader_event_loop = |
| 33 | log_reader_factory.MakeEventLoop("log_reader"); |
| 34 | reader.Register(reader_event_loop.get()); |
| 35 | // We don't run timing reports when trying to print out logged data, because |
| 36 | // otherwise we would end up printing out the timing reports themselves... |
| 37 | reader_event_loop->SkipTimingReport(); |
| 38 | |
| 39 | std::unique_ptr<aos::EventLoop> printer_event_loop = |
| 40 | log_reader_factory.MakeEventLoop("printer"); |
| 41 | printer_event_loop->SkipTimingReport(); |
| 42 | |
| 43 | bool found_channel = false; |
| 44 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 45 | reader.configuration()->channels(); |
| 46 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 47 | const aos::Channel *channel = channels->Get(i); |
| 48 | const flatbuffers::string_view name = channel->name()->string_view(); |
| 49 | const flatbuffers::string_view type = channel->type()->string_view(); |
| 50 | if (name.find(FLAGS_name) != std::string::npos && |
| 51 | type.find(FLAGS_type) != std::string::npos) { |
| 52 | 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. |
| 61 | // TODO(james): Also print out realtime time once we support |
| 62 | // replaying it. |
| 63 | std::cout << channel->name()->c_str() << ' ' |
| 64 | << channel->type()->c_str() << " at " |
| 65 | << context.monotonic_sent_time << ": " |
| 66 | << aos::FlatbufferToJson( |
| 67 | channel->schema(), |
| 68 | static_cast<const uint8_t *>(message)) |
| 69 | << '\n'; |
| 70 | }); |
| 71 | found_channel = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (!found_channel) { |
| 76 | LOG(FATAL) << "Could not find any channels"; |
| 77 | } |
| 78 | |
| 79 | log_reader_factory.Run(); |
| 80 | |
| 81 | reader.Deregister(); |
| 82 | |
| 83 | aos::Cleanup(); |
| 84 | return 0; |
| 85 | } |