Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 1 | #include <iostream> |
| 2 | |
| 3 | #include "aos/configuration.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 4 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 5 | #include "aos/events/simulated_event_loop.h" |
| 6 | #include "aos/init.h" |
| 7 | #include "aos/json_to_flatbuffer.h" |
| 8 | #include "aos/util/file.h" |
| 9 | #include "gflags/gflags.h" |
| 10 | |
| 11 | DEFINE_string(logfile, "/tmp/logfile.bfbs", |
| 12 | "Name of the logfile to read from."); |
| 13 | DEFINE_bool( |
| 14 | replace, false, |
| 15 | "If true, replace the header on the log file with the JSON header."); |
| 16 | DEFINE_string( |
| 17 | header, "", |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 18 | "If provided, this is the path to the JSON with the log file header. If " |
| 19 | "not provided, _header.json will be appended to --logfile."); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 20 | |
| 21 | int main(int argc, char **argv) { |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 22 | gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)"); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 23 | aos::InitGoogle(&argc, &argv); |
| 24 | |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame^] | 25 | std::string header_json_path = |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 26 | FLAGS_header.empty() ? (FLAGS_logfile + "_header.json") : FLAGS_header; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 27 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 28 | if (FLAGS_replace) { |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame^] | 29 | const ::std::string header_json = |
| 30 | aos::util::ReadFileToStringOrDie(header_json_path); |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 31 | flatbuffers::FlatBufferBuilder fbb; |
| 32 | fbb.ForceDefaults(true); |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame^] | 33 | flatbuffers::Offset<aos::logger::LogFileHeader> header_offset = |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 34 | aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame^] | 36 | fbb.FinishSizePrefixed(header_offset); |
| 37 | aos::SizePrefixedFlatbufferDetachedBuffer<aos::logger::LogFileHeader> |
| 38 | header(fbb.Release()); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 39 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 40 | const std::string orig_path = FLAGS_logfile + ".orig"; |
| 41 | PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 42 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 43 | aos::logger::SpanReader span_reader(orig_path); |
| 44 | CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting"; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 45 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 46 | aos::logger::DetachedBufferWriter buffer_writer( |
| 47 | FLAGS_logfile, std::make_unique<aos::logger::DummyEncoder>()); |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame^] | 48 | buffer_writer.QueueSpan(header.span()); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 49 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 50 | while (true) { |
| 51 | absl::Span<const uint8_t> msg_data = span_reader.ReadMessage(); |
| 52 | if (msg_data == absl::Span<const uint8_t>()) { |
| 53 | break; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 54 | } |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 55 | |
| 56 | buffer_writer.QueueSpan(msg_data); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 57 | } |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 58 | } else { |
| 59 | aos::logger::MessageReader reader(FLAGS_logfile); |
| 60 | aos::util::WriteStringToFileOrDie( |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame^] | 61 | header_json_path, |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 62 | aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true})); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | } |