blob: 7d3ba2fe09779ece8af1c2f5efc584fad41151d7 [file] [log] [blame]
Austin Schuhde031b72020-01-10 19:34:41 -08001#include <iostream>
2
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "gflags/gflags.h"
4
Austin Schuhde031b72020-01-10 19:34:41 -08005#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08006#include "aos/events/logging/log_reader.h"
Austin Schuhde031b72020-01-10 19:34:41 -08007#include "aos/events/simulated_event_loop.h"
8#include "aos/init.h"
9#include "aos/json_to_flatbuffer.h"
10#include "aos/util/file.h"
Austin Schuhde031b72020-01-10 19:34:41 -080011
12DEFINE_string(logfile, "/tmp/logfile.bfbs",
13 "Name of the logfile to read from.");
14DEFINE_bool(
15 replace, false,
16 "If true, replace the header on the log file with the JSON header.");
17DEFINE_string(
18 header, "",
Austin Schuhc29a7902020-09-23 21:18:26 -070019 "If provided, this is the path to the JSON with the log file header. If "
20 "not provided, _header.json will be appended to --logfile.");
Austin Schuhde031b72020-01-10 19:34:41 -080021
Austin Schuh48d10d62022-10-16 22:19:23 -070022DEFINE_int32(
23 max_message_size, 128 * 1024 * 1024,
24 "Max size of a message to be written. This sets the buffers inside "
25 "the encoders.");
26
Austin Schuhde031b72020-01-10 19:34:41 -080027int main(int argc, char **argv) {
Ravago Jonescf453ab2020-05-06 21:14:53 -070028 gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)");
Austin Schuhde031b72020-01-10 19:34:41 -080029 aos::InitGoogle(&argc, &argv);
30
Austin Schuh046b1ed2022-10-16 16:22:23 -070031 std::string header_json_path =
Austin Schuhc29a7902020-09-23 21:18:26 -070032 FLAGS_header.empty() ? (FLAGS_logfile + "_header.json") : FLAGS_header;
Austin Schuhde031b72020-01-10 19:34:41 -080033
Austin Schuhc29a7902020-09-23 21:18:26 -070034 if (FLAGS_replace) {
Austin Schuh046b1ed2022-10-16 16:22:23 -070035 const ::std::string header_json =
36 aos::util::ReadFileToStringOrDie(header_json_path);
Austin Schuhc29a7902020-09-23 21:18:26 -070037 flatbuffers::FlatBufferBuilder fbb;
38 fbb.ForceDefaults(true);
Austin Schuh046b1ed2022-10-16 16:22:23 -070039 flatbuffers::Offset<aos::logger::LogFileHeader> header_offset =
Austin Schuhc29a7902020-09-23 21:18:26 -070040 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
Austin Schuhde031b72020-01-10 19:34:41 -080041
Austin Schuh046b1ed2022-10-16 16:22:23 -070042 fbb.FinishSizePrefixed(header_offset);
43 aos::SizePrefixedFlatbufferDetachedBuffer<aos::logger::LogFileHeader>
44 header(fbb.Release());
Austin Schuhde031b72020-01-10 19:34:41 -080045
Austin Schuhc29a7902020-09-23 21:18:26 -070046 const std::string orig_path = FLAGS_logfile + ".orig";
47 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
Austin Schuhde031b72020-01-10 19:34:41 -080048
Austin Schuhc29a7902020-09-23 21:18:26 -070049 aos::logger::SpanReader span_reader(orig_path);
50 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080051
Alexei Strots15c22b12023-04-04 16:27:17 -070052 aos::logger::FileBackend file_backend("/");
53 aos::logger::DetachedBufferWriter buffer_writer(
54 file_backend.RequestFile(FLAGS_logfile),
Austin Schuh48d10d62022-10-16 22:19:23 -070055 std::make_unique<aos::logger::DummyEncoder>(FLAGS_max_message_size));
Austin Schuh7ef11a42023-02-04 17:15:12 -080056 {
Alexei Strots15c22b12023-04-04 16:27:17 -070057 aos::logger::DataEncoder::SpanCopier copier(header.span());
58 buffer_writer.CopyMessage(&copier, aos::monotonic_clock::min_time);
Austin Schuh7ef11a42023-02-04 17:15:12 -080059 }
Austin Schuhde031b72020-01-10 19:34:41 -080060
Austin Schuhc29a7902020-09-23 21:18:26 -070061 while (true) {
62 absl::Span<const uint8_t> msg_data = span_reader.ReadMessage();
James Kuszmaul9776b392023-01-14 14:08:08 -080063 if (msg_data.empty()) {
Austin Schuhc29a7902020-09-23 21:18:26 -070064 break;
Austin Schuhde031b72020-01-10 19:34:41 -080065 }
Austin Schuhc29a7902020-09-23 21:18:26 -070066
Austin Schuh7ef11a42023-02-04 17:15:12 -080067 {
Alexei Strots15c22b12023-04-04 16:27:17 -070068 aos::logger::DataEncoder::SpanCopier copier(msg_data);
69 buffer_writer.CopyMessage(&copier, aos::monotonic_clock::min_time);
Austin Schuh7ef11a42023-02-04 17:15:12 -080070 }
Austin Schuhde031b72020-01-10 19:34:41 -080071 }
Austin Schuhc29a7902020-09-23 21:18:26 -070072 } else {
73 aos::logger::MessageReader reader(FLAGS_logfile);
74 aos::util::WriteStringToFileOrDie(
Austin Schuh046b1ed2022-10-16 16:22:23 -070075 header_json_path,
Austin Schuhc29a7902020-09-23 21:18:26 -070076 aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080077 }
78
Austin Schuhde031b72020-01-10 19:34:41 -080079 return 0;
80}