blob: 4585ab7bee9430981ad9c1b090e15e18302d77c7 [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
colleen61276dc2023-06-01 09:23:29 -070027DEFINE_bool(direct, false,
28 "If true, write using O_DIRECT and write 512 byte aligned blocks "
29 "whenever possible.");
Austin Schuhde031b72020-01-10 19:34:41 -080030int main(int argc, char **argv) {
Ravago Jonescf453ab2020-05-06 21:14:53 -070031 gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)");
Austin Schuhde031b72020-01-10 19:34:41 -080032 aos::InitGoogle(&argc, &argv);
33
Austin Schuh046b1ed2022-10-16 16:22:23 -070034 std::string header_json_path =
Austin Schuhc29a7902020-09-23 21:18:26 -070035 FLAGS_header.empty() ? (FLAGS_logfile + "_header.json") : FLAGS_header;
Austin Schuhde031b72020-01-10 19:34:41 -080036
Austin Schuhc29a7902020-09-23 21:18:26 -070037 if (FLAGS_replace) {
Austin Schuh046b1ed2022-10-16 16:22:23 -070038 const ::std::string header_json =
39 aos::util::ReadFileToStringOrDie(header_json_path);
Austin Schuhc29a7902020-09-23 21:18:26 -070040 flatbuffers::FlatBufferBuilder fbb;
41 fbb.ForceDefaults(true);
Austin Schuh046b1ed2022-10-16 16:22:23 -070042 flatbuffers::Offset<aos::logger::LogFileHeader> header_offset =
Austin Schuhc29a7902020-09-23 21:18:26 -070043 aos::JsonToFlatbuffer<aos::logger::LogFileHeader>(header_json, &fbb);
Austin Schuhde031b72020-01-10 19:34:41 -080044
Austin Schuh046b1ed2022-10-16 16:22:23 -070045 fbb.FinishSizePrefixed(header_offset);
46 aos::SizePrefixedFlatbufferDetachedBuffer<aos::logger::LogFileHeader>
47 header(fbb.Release());
Austin Schuhde031b72020-01-10 19:34:41 -080048
Austin Schuhc29a7902020-09-23 21:18:26 -070049 const std::string orig_path = FLAGS_logfile + ".orig";
50 PCHECK(rename(FLAGS_logfile.c_str(), orig_path.c_str()) == 0);
Austin Schuhde031b72020-01-10 19:34:41 -080051
Austin Schuhc29a7902020-09-23 21:18:26 -070052 aos::logger::SpanReader span_reader(orig_path);
53 CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
Austin Schuhde031b72020-01-10 19:34:41 -080054
colleen61276dc2023-06-01 09:23:29 -070055 aos::logger::FileBackend file_backend("/", FLAGS_direct);
Alexei Strots15c22b12023-04-04 16:27:17 -070056 aos::logger::DetachedBufferWriter buffer_writer(
57 file_backend.RequestFile(FLAGS_logfile),
Austin Schuh48d10d62022-10-16 22:19:23 -070058 std::make_unique<aos::logger::DummyEncoder>(FLAGS_max_message_size));
Austin Schuh7ef11a42023-02-04 17:15:12 -080059 {
Alexei Strots15c22b12023-04-04 16:27:17 -070060 aos::logger::DataEncoder::SpanCopier copier(header.span());
61 buffer_writer.CopyMessage(&copier, aos::monotonic_clock::min_time);
Austin Schuh7ef11a42023-02-04 17:15:12 -080062 }
Austin Schuhde031b72020-01-10 19:34:41 -080063
Austin Schuhc29a7902020-09-23 21:18:26 -070064 while (true) {
65 absl::Span<const uint8_t> msg_data = span_reader.ReadMessage();
James Kuszmaul9776b392023-01-14 14:08:08 -080066 if (msg_data.empty()) {
Austin Schuhc29a7902020-09-23 21:18:26 -070067 break;
Austin Schuhde031b72020-01-10 19:34:41 -080068 }
Austin Schuhc29a7902020-09-23 21:18:26 -070069
Austin Schuh7ef11a42023-02-04 17:15:12 -080070 {
Alexei Strots15c22b12023-04-04 16:27:17 -070071 aos::logger::DataEncoder::SpanCopier copier(msg_data);
72 buffer_writer.CopyMessage(&copier, aos::monotonic_clock::min_time);
Austin Schuh7ef11a42023-02-04 17:15:12 -080073 }
Austin Schuhde031b72020-01-10 19:34:41 -080074 }
Austin Schuhc29a7902020-09-23 21:18:26 -070075 } else {
76 aos::logger::MessageReader reader(FLAGS_logfile);
77 aos::util::WriteStringToFileOrDie(
Austin Schuh046b1ed2022-10-16 16:22:23 -070078 header_json_path,
Austin Schuhc29a7902020-09-23 21:18:26 -070079 aos::FlatbufferToJson(reader.log_file_header(), {.multi_line = true}));
Austin Schuhde031b72020-01-10 19:34:41 -080080 }
81
Austin Schuhde031b72020-01-10 19:34:41 -080082 return 0;
83}