blob: 9961bd82d8ed59fbd98d5d3f1b3e380f432aa44d [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, "",
18 "If provided, this is the path to the JSON with the log file header.");
19
20int main(int argc, char **argv) {
Ravago Jonescf453ab2020-05-06 21:14:53 -070021 gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)");
Austin Schuhde031b72020-01-10 19:34:41 -080022 aos::InitGoogle(&argc, &argv);
23
24 if (!FLAGS_header.empty()) {
25 if (FLAGS_replace) {
26 const ::std::string header_json =
27 aos::util::ReadFileToStringOrDie(FLAGS_header);
28 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -080029 fbb.ForceDefaults(true);
Austin Schuhde031b72020-01-10 19:34:41 -080030 flatbuffers::Offset<aos::logger::LogFileHeader> header =
31 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
32
33 fbb.FinishSizePrefixed(header);
34
35 const std::string orig_path = FLAGS_logfile + ".orig";
36 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
37
38 aos::logger::SpanReader span_reader(orig_path);
Austin Schuhf15e9372020-02-26 18:54:58 -080039 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080040
Brian Silvermanf51499a2020-09-21 12:49:08 -070041 aos::logger::DetachedBufferWriter buffer_writer(
42 FLAGS_logfile, std::make_unique<aos::logger::DummyEncoder>());
Austin Schuhde031b72020-01-10 19:34:41 -080043 buffer_writer.QueueSizedFlatbuffer(&fbb);
44
45 while (true) {
46 absl::Span<const uint8_t> msg_data = span_reader.ReadMessage();
47 if (msg_data == absl::Span<const uint8_t>()) {
48 break;
49 }
50
Brian Silvermanf51499a2020-09-21 12:49:08 -070051 buffer_writer.QueueSpan(msg_data);
Austin Schuhde031b72020-01-10 19:34:41 -080052 }
53 } else {
54 aos::logger::MessageReader reader(FLAGS_logfile);
55 aos::util::WriteStringToFileOrDie(
Ravago Jonescf453ab2020-05-06 21:14:53 -070056 FLAGS_header, aos::FlatbufferToJson(reader.log_file_header(),
57 {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080058 }
59 }
60
Austin Schuhde031b72020-01-10 19:34:41 -080061 return 0;
62}