proxygen
|
#include <AsyncFileWriter.h>
Classes | |
struct | Data |
Public Member Functions | |
AsyncFileWriter (folly::StringPiece path) | |
AsyncFileWriter (folly::File &&file) | |
~AsyncFileWriter () | |
void | writeMessage (folly::StringPiece buffer, uint32_t flags=0) override |
void | writeMessage (std::string &&buffer, uint32_t flags=0) override |
void | flush () override |
bool | ttyOutput () const override |
void | setMaxBufferSize (size_t size) |
size_t | getMaxBufferSize () const |
const folly::File & | getFile () const |
![]() | |
virtual | ~LogWriter () |
Static Public Attributes | |
static constexpr size_t | kDefaultMaxBufferSize = 1024 * 1024 |
Private Types | |
enum | Flags : uint32_t { FLAG_IO_THREAD_STARTED = 0x01, FLAG_DESTROYING = 0x02, FLAG_STOP = 0x04, FLAG_IO_THREAD_STOPPED = 0x08, FLAG_IO_THREAD_JOINED = 0x10 } |
Private Member Functions | |
void | ioThread () |
void | performIO (std::vector< std::string > *ioQueue, size_t numDiscarded) |
void | onIoError (const std::exception &ex) |
std::string | getNumDiscardedMsg (size_t numDiscarded) |
bool | preFork () |
void | postForkParent () |
void | postForkChild () |
void | stopIoThread (folly::Synchronized< Data, std::mutex >::LockedPtr &data, uint32_t extraFlags) |
void | restartThread () |
Private Attributes | |
folly::File | file_ |
folly::Synchronized< Data, std::mutex > | data_ |
std::condition_variable | messageReady_ |
std::condition_variable | ioCV_ |
folly::Synchronized< Data, std::mutex >::LockedPtr | lockedData_ |
Additional Inherited Members | |
![]() | |
enum | Flags : uint32_t { NO_FLAGS = 0x00, NEVER_DISCARD = 0x01 } |
A LogWriter implementation that asynchronously writes to a file descriptor.
This class performs the log I/O in a separarate thread.
The advantage of this class over ImmediateFileWriter is that logging I/O can never slow down or block your normal program operation. If log messages are generated faster than they can be written, messages will be dropped (and an indication of how many messages were dropped will be written to the log file when we are able to catch up a bit.)
However, one downside is that if your program crashes, not all log messages may have been written, so you may lose messages generated immediately before the crash.
Definition at line 44 of file AsyncFileWriter.h.
|
private |
Enumerator | |
---|---|
FLAG_IO_THREAD_STARTED | |
FLAG_DESTROYING | |
FLAG_STOP | |
FLAG_IO_THREAD_STOPPED | |
FLAG_IO_THREAD_JOINED |
Definition at line 103 of file AsyncFileWriter.h.
|
explicit |
Construct an AsyncFileWriter that appends to the file at the specified path.
Definition at line 31 of file AsyncFileWriter.cpp.
References folly::Range< Iter >::str().
|
explicit |
Construct an AsyncFileWriter that writes to the specified File object.
Definition at line 33 of file AsyncFileWriter.cpp.
References folly::data(), data_, FLAG_IO_THREAD_STARTED, ioThread(), folly::gen::move, postForkChild(), postForkParent(), preFork(), and folly::detail::AtFork::registerHandler().
folly::AsyncFileWriter::~AsyncFileWriter | ( | ) |
Definition at line 51 of file AsyncFileWriter.cpp.
References folly::data(), data_, FLAG_DESTROYING, onIoError(), performIO(), stopIoThread(), and folly::detail::AtFork::unregisterHandler().
|
overridevirtual |
Block until the I/O thread has finished writing all messages that were already enqueued when flush() was called.
Implements folly::LogWriter.
Definition at line 103 of file AsyncFileWriter.cpp.
References folly::data(), data_, ioCV_, messageReady_, and start.
|
inline |
size_t folly::AsyncFileWriter::getMaxBufferSize | ( | ) | const |
Get the maximum buffer size for this AsyncFileWriter, in bytes.
Definition at line 129 of file AsyncFileWriter.cpp.
References folly::data(), and data_.
|
private |
Definition at line 228 of file AsyncFileWriter.cpp.
Referenced by folly::AsyncFileWriter::Data::getCurrentQueue(), and performIO().
|
private |
Definition at line 134 of file AsyncFileWriter.cpp.
References folly::data(), data_, FLAG_IO_THREAD_STOPPED, FLAG_STOP, ioCV_, messageReady_, onIoError(), performIO(), and folly::setThreadName().
Referenced by AsyncFileWriter(), and restartThread().
|
private |
Definition at line 218 of file AsyncFileWriter.cpp.
References folly::exceptionStr(), folly::File::fd(), file_, and folly::LoggerDB::internalWarning().
Referenced by folly::AsyncFileWriter::Data::getCurrentQueue(), ioThread(), and ~AsyncFileWriter().
|
private |
Definition at line 184 of file AsyncFileWriter.cpp.
References folly::checkUnixError(), folly::File::fd(), file_, getNumDiscardedMsg(), folly::size(), folly::writeFull(), and folly::writevFull().
Referenced by folly::AsyncFileWriter::Data::getCurrentQueue(), ioThread(), and ~AsyncFileWriter().
|
private |
Definition at line 264 of file AsyncFileWriter.cpp.
References lockedData_, and restartThread().
Referenced by AsyncFileWriter(), and folly::AsyncFileWriter::Data::getCurrentQueue().
|
private |
Definition at line 259 of file AsyncFileWriter.cpp.
References restartThread().
Referenced by AsyncFileWriter(), and folly::AsyncFileWriter::Data::getCurrentQueue().
|
private |
Definition at line 237 of file AsyncFileWriter.cpp.
References data_, FLAG_IO_THREAD_STARTED, lockedData_, and stopIoThread().
Referenced by AsyncFileWriter(), and folly::AsyncFileWriter::Data::getCurrentQueue().
|
private |
Definition at line 292 of file AsyncFileWriter.cpp.
References folly::data(), FLAG_DESTROYING, FLAG_IO_THREAD_JOINED, FLAG_IO_THREAD_STARTED, FLAG_IO_THREAD_STOPPED, FLAG_STOP, ioThread(), lockedData_, and folly::gen::move.
Referenced by folly::AsyncFileWriter::Data::getCurrentQueue(), postForkChild(), and postForkParent().
void folly::AsyncFileWriter::setMaxBufferSize | ( | size_t | size | ) |
Set the maximum buffer size for this AsyncFileWriter, in bytes.
This controls the upper bound on how much unwritten data will be buffered in memory. If messages are being logged faster than they can be written to output file, new messages will be discarded if they would cause the amount of buffered data to exceed this limit.
Definition at line 124 of file AsyncFileWriter.cpp.
References folly::data(), data_, and folly::size().
|
private |
Definition at line 274 of file AsyncFileWriter.cpp.
References FLAG_IO_THREAD_JOINED, FLAG_IO_THREAD_STOPPED, FLAG_STOP, ioCV_, and messageReady_.
Referenced by folly::AsyncFileWriter::Data::getCurrentQueue(), preFork(), and ~AsyncFileWriter().
|
overridevirtual |
Returns true if the output steam is a tty.
Implements folly::LogWriter.
Definition at line 81 of file AsyncFileWriter.cpp.
References folly::File::fd(), and file_.
|
overridevirtual |
Write a serialized log message.
The flags parameter is a bitwise-ORed set of Flag values defined above.
Implements folly::LogWriter.
Definition at line 85 of file AsyncFileWriter.cpp.
References folly::Range< Iter >::str().
Referenced by writeThread().
|
overridevirtual |
Write a serialized message.
This version of writeMessage() accepts a std::string&&. The default implementation calls the StringPiece version of writeMessage(), but subclasses may override this implementation if desired.
Reimplemented from folly::LogWriter.
Definition at line 89 of file AsyncFileWriter.cpp.
References buffer(), folly::data(), data_, messageReady_, folly::gen::move, and folly::LogWriter::NEVER_DISCARD.
|
private |
Definition at line 158 of file AsyncFileWriter.h.
Referenced by AsyncFileWriter(), flush(), getMaxBufferSize(), ioThread(), preFork(), setMaxBufferSize(), writeMessage(), and ~AsyncFileWriter().
|
private |
Definition at line 156 of file AsyncFileWriter.h.
Referenced by getFile(), onIoError(), performIO(), and ttyOutput().
|
private |
ioCV_ is signaled by the I/O thread each time it increments the ioThreadCounter (once each time around its loop).
Definition at line 168 of file AsyncFileWriter.h.
Referenced by flush(), ioThread(), and stopIoThread().
|
static |
The default maximum buffer size.
The comments for setMaxBufferSize() explain how this parameter is used.
Definition at line 51 of file AsyncFileWriter.h.
Referenced by TEST().
|
private |
lockedData_ exists only to help pass the lock between preFork() and postForkParent()/postForkChild(). We potentially could add some new low-level methods to Synchronized to allow manually locking and unlocking to avoid having to store this object as a member variable.
Definition at line 176 of file AsyncFileWriter.h.
Referenced by postForkChild(), preFork(), and restartThread().
|
private |
messageReady_ is signaled by writer threads whenever they add a new message to the current queue.
Definition at line 163 of file AsyncFileWriter.h.
Referenced by flush(), ioThread(), stopIoThread(), and writeMessage().