blob: 0dca904a1d3a0006730123d8a6a0a983f25754a5 [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
41 aos::logger::DetachedBufferWriter buffer_writer(FLAGS_logfile);
42 buffer_writer.QueueSizedFlatbuffer(&fbb);
43
44 while (true) {
45 absl::Span<const uint8_t> msg_data = span_reader.ReadMessage();
46 if (msg_data == absl::Span<const uint8_t>()) {
47 break;
48 }
49
50 buffer_writer.WriteSizedFlatbuffer(msg_data);
51 }
52 } else {
53 aos::logger::MessageReader reader(FLAGS_logfile);
54 aos::util::WriteStringToFileOrDie(
Ravago Jonescf453ab2020-05-06 21:14:53 -070055 FLAGS_header, aos::FlatbufferToJson(reader.log_file_header(),
56 {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080057 }
58 }
59
60 aos::Cleanup();
61 return 0;
62}