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 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 21 | DEFINE_int32( |
| 22 | max_message_size, 128 * 1024 * 1024, |
| 23 | "Max size of a message to be written. This sets the buffers inside " |
| 24 | "the encoders."); |
| 25 | |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 26 | int main(int argc, char **argv) { |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 27 | gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)"); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 28 | aos::InitGoogle(&argc, &argv); |
| 29 | |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame] | 30 | std::string header_json_path = |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 31 | FLAGS_header.empty() ? (FLAGS_logfile + "_header.json") : FLAGS_header; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 32 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 33 | if (FLAGS_replace) { |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame] | 34 | const ::std::string header_json = |
| 35 | aos::util::ReadFileToStringOrDie(header_json_path); |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 36 | flatbuffers::FlatBufferBuilder fbb; |
| 37 | fbb.ForceDefaults(true); |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame] | 38 | flatbuffers::Offset<aos::logger::LogFileHeader> header_offset = |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 39 | aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame] | 41 | fbb.FinishSizePrefixed(header_offset); |
| 42 | aos::SizePrefixedFlatbufferDetachedBuffer<aos::logger::LogFileHeader> |
| 43 | header(fbb.Release()); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 44 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 45 | const std::string orig_path = FLAGS_logfile + ".orig"; |
| 46 | PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 47 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 48 | aos::logger::SpanReader span_reader(orig_path); |
| 49 | CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting"; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 50 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 51 | aos::logger::DetachedBufferWriter buffer_writer( |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 52 | FLAGS_logfile, |
| 53 | std::make_unique<aos::logger::DummyEncoder>(FLAGS_max_message_size)); |
Austin Schuh | 7ef11a4 | 2023-02-04 17:15:12 -0800 | [diff] [blame^] | 54 | { |
| 55 | aos::logger::DataEncoder::SpanCopier coppier(header.span()); |
| 56 | buffer_writer.CopyMessage(&coppier, aos::monotonic_clock::min_time); |
| 57 | } |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 58 | |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 59 | while (true) { |
| 60 | absl::Span<const uint8_t> msg_data = span_reader.ReadMessage(); |
James Kuszmaul | 9776b39 | 2023-01-14 14:08:08 -0800 | [diff] [blame] | 61 | if (msg_data.empty()) { |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 62 | break; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 63 | } |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 64 | |
Austin Schuh | 7ef11a4 | 2023-02-04 17:15:12 -0800 | [diff] [blame^] | 65 | { |
| 66 | aos::logger::DataEncoder::SpanCopier coppier(msg_data); |
| 67 | buffer_writer.CopyMessage(&coppier, aos::monotonic_clock::min_time); |
| 68 | } |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 69 | } |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 70 | } else { |
| 71 | aos::logger::MessageReader reader(FLAGS_logfile); |
| 72 | aos::util::WriteStringToFileOrDie( |
Austin Schuh | 046b1ed | 2022-10-16 16:22:23 -0700 | [diff] [blame] | 73 | header_json_path, |
Austin Schuh | c29a790 | 2020-09-23 21:18:26 -0700 | [diff] [blame] | 74 | aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true})); |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |