Encode flatbuffers directly into the encoder when logging

We were running out of memory when running for many hours.  Initial
debugging looked like it was a heap fragmentation issue.  Tracking the
allocated memory using the malloc hooks wasn't showing any growth of
memory.  The heap was growing though.

Instead of allocating a FlatBufferBuilder/DetachedBuffer for each
message to be logged, we can instead have the BufferEncoder provide
memory to write to, and have it only alloate that buffer space once, and
allocate it to the maximum size that a writer might see.

Change-Id: I046bd2422aea368867b0c63cee7d04c6033fe724
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/events/logging/log_edit.cc b/aos/events/logging/log_edit.cc
index b0f8ba5..969d59c 100644
--- a/aos/events/logging/log_edit.cc
+++ b/aos/events/logging/log_edit.cc
@@ -18,6 +18,11 @@
     "If provided, this is the path to the JSON with the log file header.  If "
     "not provided, _header.json will be appended to --logfile.");
 
+DEFINE_int32(
+    max_message_size, 128 * 1024 * 1024,
+    "Max size of a message to be written.  This sets the buffers inside "
+    "the encoders.");
+
 int main(int argc, char **argv) {
   gflags::SetUsageMessage(R"(This tool lets us manipulate log files.)");
   aos::InitGoogle(&argc, &argv);
@@ -44,7 +49,8 @@
     CHECK(!span_reader.ReadMessage().empty()) << ": Empty header, aborting";
 
     aos::logger::DetachedBufferWriter buffer_writer(
-        FLAGS_logfile, std::make_unique<aos::logger::DummyEncoder>());
+        FLAGS_logfile,
+        std::make_unique<aos::logger::DummyEncoder>(FLAGS_max_message_size));
     buffer_writer.QueueSpan(header.span());
 
     while (true) {