blob: ff263f8e76b684422386e98e9d8b5b2b71d31bb4 [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) {
21 gflags::SetUsageMessage(
22 R"(This tool lets us manipulate log files.)");
23 aos::InitGoogle(&argc, &argv);
24
25 if (!FLAGS_header.empty()) {
26 if (FLAGS_replace) {
27 const ::std::string header_json =
28 aos::util::ReadFileToStringOrDie(FLAGS_header);
29 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -080030 fbb.ForceDefaults(true);
Austin Schuhde031b72020-01-10 19:34:41 -080031 flatbuffers::Offset<aos::logger::LogFileHeader> header =
32 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
33
34 fbb.FinishSizePrefixed(header);
35
36 const std::string orig_path = FLAGS_logfile + ".orig";
37 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
38
39 aos::logger::SpanReader span_reader(orig_path);
Austin Schuhf15e9372020-02-26 18:54:58 -080040 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080041
42 aos::logger::DetachedBufferWriter buffer_writer(FLAGS_logfile);
43 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
51 buffer_writer.WriteSizedFlatbuffer(msg_data);
52 }
53 } else {
54 aos::logger::MessageReader reader(FLAGS_logfile);
55 aos::util::WriteStringToFileOrDie(
56 FLAGS_header, aos::FlatbufferToJson(reader.log_file_header(), true));
57 }
58 }
59
60 aos::Cleanup();
61 return 0;
62}