blob: 02a1402a61bc1cb0b005bb6de545b1f6885a3169 [file] [log] [blame]
Austin Schuhde031b72020-01-10 19:34:41 -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 "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 Schuhc29a7902020-09-23 21:18:26 -070025 std::string header =
26 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) {
29 const ::std::string header_json = aos::util::ReadFileToStringOrDie(header);
30 flatbuffers::FlatBufferBuilder fbb;
31 fbb.ForceDefaults(true);
32 flatbuffers::Offset<aos::logger::LogFileHeader> header =
33 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
Austin Schuhde031b72020-01-10 19:34:41 -080034
Austin Schuhc29a7902020-09-23 21:18:26 -070035 fbb.FinishSizePrefixed(header);
Austin Schuhde031b72020-01-10 19:34:41 -080036
Austin Schuhc29a7902020-09-23 21:18:26 -070037 const std::string orig_path = FLAGS_logfile + ".orig";
38 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
Austin Schuhde031b72020-01-10 19:34:41 -080039
Austin Schuhc29a7902020-09-23 21:18:26 -070040 aos::logger::SpanReader span_reader(orig_path);
41 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080042
Austin Schuhc29a7902020-09-23 21:18:26 -070043 aos::logger::DetachedBufferWriter buffer_writer(
44 FLAGS_logfile, std::make_unique<aos::logger::DummyEncoder>());
45 buffer_writer.QueueSizedFlatbuffer(&fbb);
Austin Schuhde031b72020-01-10 19:34:41 -080046
Austin Schuhc29a7902020-09-23 21:18:26 -070047 while (true) {
48 absl::Span<const uint8_t> msg_data = span_reader.ReadMessage();
49 if (msg_data == absl::Span<const uint8_t>()) {
50 break;
Austin Schuhde031b72020-01-10 19:34:41 -080051 }
Austin Schuhc29a7902020-09-23 21:18:26 -070052
53 buffer_writer.QueueSpan(msg_data);
Austin Schuhde031b72020-01-10 19:34:41 -080054 }
Austin Schuhc29a7902020-09-23 21:18:26 -070055 } else {
56 aos::logger::MessageReader reader(FLAGS_logfile);
57 aos::util::WriteStringToFileOrDie(
58 header,
59 aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080060 }
61
Austin Schuhde031b72020-01-10 19:34:41 -080062 return 0;
63}