blob: 969d59cb75ba6522ea9afa005076087ace64f0f9 [file] [log] [blame]
Austin Schuhde031b72020-01-10 19:34:41 -08001#include <iostream>
2
3#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08004#include "aos/events/logging/log_reader.h"
Austin Schuhde031b72020-01-10 19:34:41 -08005#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
Austin Schuh48d10d62022-10-16 22:19:23 -070021DEFINE_int32(
22 max_message_size, 128 * 1024 * 1024,
23 "Max size of a message to be written. This sets the buffers inside "
24 "the encoders.");
25
Austin Schuhde031b72020-01-10 19:34:41 -080026int main(int argc, char **argv) {
Ravago Jonescf453ab2020-05-06 21:14:53 -070027 gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)");
Austin Schuhde031b72020-01-10 19:34:41 -080028 aos::InitGoogle(&argc, &argv);
29
Austin Schuh046b1ed2022-10-16 16:22:23 -070030 std::string header_json_path =
Austin Schuhc29a7902020-09-23 21:18:26 -070031 FLAGS_header.empty() ? (FLAGS_logfile + "_header.json") : FLAGS_header;
Austin Schuhde031b72020-01-10 19:34:41 -080032
Austin Schuhc29a7902020-09-23 21:18:26 -070033 if (FLAGS_replace) {
Austin Schuh046b1ed2022-10-16 16:22:23 -070034 const ::std::string header_json =
35 aos::util::ReadFileToStringOrDie(header_json_path);
Austin Schuhc29a7902020-09-23 21:18:26 -070036 flatbuffers::FlatBufferBuilder fbb;
37 fbb.ForceDefaults(true);
Austin Schuh046b1ed2022-10-16 16:22:23 -070038 flatbuffers::Offset<aos::logger::LogFileHeader> header_offset =
Austin Schuhc29a7902020-09-23 21:18:26 -070039 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
Austin Schuhde031b72020-01-10 19:34:41 -080040
Austin Schuh046b1ed2022-10-16 16:22:23 -070041 fbb.FinishSizePrefixed(header_offset);
42 aos::SizePrefixedFlatbufferDetachedBuffer<aos::logger::LogFileHeader>
43 header(fbb.Release());
Austin Schuhde031b72020-01-10 19:34:41 -080044
Austin Schuhc29a7902020-09-23 21:18:26 -070045 const std::string orig_path = FLAGS_logfile + ".orig";
46 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
Austin Schuhde031b72020-01-10 19:34:41 -080047
Austin Schuhc29a7902020-09-23 21:18:26 -070048 aos::logger::SpanReader span_reader(orig_path);
49 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080050
Austin Schuhc29a7902020-09-23 21:18:26 -070051 aos::logger::DetachedBufferWriter buffer_writer(
Austin Schuh48d10d62022-10-16 22:19:23 -070052 FLAGS_logfile,
53 std::make_unique<aos::logger::DummyEncoder>(FLAGS_max_message_size));
Austin Schuh046b1ed2022-10-16 16:22:23 -070054 buffer_writer.QueueSpan(header.span());
Austin Schuhde031b72020-01-10 19:34:41 -080055
Austin Schuhc29a7902020-09-23 21:18:26 -070056 while (true) {
57 absl::Span<const uint8_t> msg_data = span_reader.ReadMessage();
58 if (msg_data == absl::Span<const uint8_t>()) {
59 break;
Austin Schuhde031b72020-01-10 19:34:41 -080060 }
Austin Schuhc29a7902020-09-23 21:18:26 -070061
62 buffer_writer.QueueSpan(msg_data);
Austin Schuhde031b72020-01-10 19:34:41 -080063 }
Austin Schuhc29a7902020-09-23 21:18:26 -070064 } else {
65 aos::logger::MessageReader reader(FLAGS_logfile);
66 aos::util::WriteStringToFileOrDie(
Austin Schuh046b1ed2022-10-16 16:22:23 -070067 header_json_path,
Austin Schuhc29a7902020-09-23 21:18:26 -070068 aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080069 }
70
Austin Schuhde031b72020-01-10 19:34:41 -080071 return 0;
72}