blob: b0f8ba50828712d3fd43b3172390cfbb7156c169 [file] [log] [blame]
Austin Schuhde031b72020-01-10 19:34:41 -08001#include <iostream>
2
3#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08004#include "aos/events/logging/log_reader.h"
Austin Schuhde031b72020-01-10 19:34:41 -08005#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
11DEFINE_string(logfile, "/tmp/logfile.bfbs",
12 "Name of the logfile to read from.");
13DEFINE_bool(
14 replace, false,
15 "If true, replace the header on the log file with the JSON header.");
16DEFINE_string(
17 header, "",
Austin Schuhc29a7902020-09-23 21:18:26 -070018 "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 Schuhde031b72020-01-10 19:34:41 -080020
21int main(int argc, char **argv) {
Ravago Jonescf453ab2020-05-06 21:14:53 -070022 gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)");
Austin Schuhde031b72020-01-10 19:34:41 -080023 aos::InitGoogle(&argc, &argv);
24
Austin Schuh046b1ed2022-10-16 16:22:23 -070025 std::string header_json_path =
Austin Schuhc29a7902020-09-23 21:18:26 -070026 FLAGS_header.empty() ? (FLAGS_logfile + "_header.json") : FLAGS_header;
Austin Schuhde031b72020-01-10 19:34:41 -080027
Austin Schuhc29a7902020-09-23 21:18:26 -070028 if (FLAGS_replace) {
Austin Schuh046b1ed2022-10-16 16:22:23 -070029 const ::std::string header_json =
30 aos::util::ReadFileToStringOrDie(header_json_path);
Austin Schuhc29a7902020-09-23 21:18:26 -070031 flatbuffers::FlatBufferBuilder fbb;
32 fbb.ForceDefaults(true);
Austin Schuh046b1ed2022-10-16 16:22:23 -070033 flatbuffers::Offset<aos::logger::LogFileHeader> header_offset =
Austin Schuhc29a7902020-09-23 21:18:26 -070034 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
Austin Schuhde031b72020-01-10 19:34:41 -080035
Austin Schuh046b1ed2022-10-16 16:22:23 -070036 fbb.FinishSizePrefixed(header_offset);
37 aos::SizePrefixedFlatbufferDetachedBuffer<aos::logger::LogFileHeader>
38 header(fbb.Release());
Austin Schuhde031b72020-01-10 19:34:41 -080039
Austin Schuhc29a7902020-09-23 21:18:26 -070040 const std::string orig_path = FLAGS_logfile + ".orig";
41 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
Austin Schuhde031b72020-01-10 19:34:41 -080042
Austin Schuhc29a7902020-09-23 21:18:26 -070043 aos::logger::SpanReader span_reader(orig_path);
44 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080045
Austin Schuhc29a7902020-09-23 21:18:26 -070046 aos::logger::DetachedBufferWriter buffer_writer(
47 FLAGS_logfile, std::make_unique<aos::logger::DummyEncoder>());
Austin Schuh046b1ed2022-10-16 16:22:23 -070048 buffer_writer.QueueSpan(header.span());
Austin Schuhde031b72020-01-10 19:34:41 -080049
Austin Schuhc29a7902020-09-23 21:18:26 -070050 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 Schuhde031b72020-01-10 19:34:41 -080054 }
Austin Schuhc29a7902020-09-23 21:18:26 -070055
56 buffer_writer.QueueSpan(msg_data);
Austin Schuhde031b72020-01-10 19:34:41 -080057 }
Austin Schuhc29a7902020-09-23 21:18:26 -070058 } else {
59 aos::logger::MessageReader reader(FLAGS_logfile);
60 aos::util::WriteStringToFileOrDie(
Austin Schuh046b1ed2022-10-16 16:22:23 -070061 header_json_path,
Austin Schuhc29a7902020-09-23 21:18:26 -070062 aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080063 }
64
Austin Schuhde031b72020-01-10 19:34:41 -080065 return 0;
66}