Improve logger behavior when out of disk space
I actually tried it, and found a few places that didn't fully work.
Might still be more, but it's definitely closer now.
It'd be nice to test this stuff, but it's hard to set up a valid test.
Change-Id: If2ad1b9d91a116515215eaa49140b1aa0230fc93
diff --git a/aos/events/logging/logfile_utils.h b/aos/events/logging/logfile_utils.h
index 0b0c8f5..fddda72 100644
--- a/aos/events/logging/logfile_utils.h
+++ b/aos/events/logging/logfile_utils.h
@@ -42,8 +42,14 @@
// file. It encodes them, queues them up, and batches the write operation.
class DetachedBufferWriter {
public:
+ // Marker struct for one of our constructor overloads.
+ struct already_out_of_space_t {};
+
DetachedBufferWriter(std::string_view filename,
std::unique_ptr<DetachedBufferEncoder> encoder);
+ // Creates a dummy instance which won't even open a file. It will act as if
+ // opening the file ran out of space immediately.
+ DetachedBufferWriter(already_out_of_space_t) : ran_out_of_space_(true) {}
DetachedBufferWriter(DetachedBufferWriter &&other);
DetachedBufferWriter(const DetachedBufferWriter &) = delete;
@@ -54,6 +60,10 @@
std::string_view filename() const { return filename_; }
+ // This will be true until Close() is called, unless the file couldn't be
+ // created due to running out of space.
+ bool is_open() const { return fd_ != -1; }
+
// Queues up a finished FlatBufferBuilder to be encoded and written.
//
// Triggers a flush if there's enough data queued up.
@@ -64,6 +74,9 @@
}
// May steal the backing storage of buffer, or may leave it alone.
void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) {
+ if (ran_out_of_space_) {
+ return;
+ }
encoder_->Encode(std::move(buffer));
FlushAtThreshold();
}