Apollo  6.0
Open source self driving car software
Classes | Public Member Functions | List of all members
apollo::cyber::logger::AsyncLogger Class Reference

Wrapper for a glog Logger which asynchronously writes log messages. This class starts a new thread responsible for forwarding the messages to the logger, and performs double buffering. Writers append to the current buffer and then wake up the logger thread. The logger swaps in a new buffer and writes any accumulated messages to the wrapped Logger. More...

#include <async_logger.h>

Inheritance diagram for apollo::cyber::logger::AsyncLogger:
Inheritance graph
Collaboration diagram for apollo::cyber::logger::AsyncLogger:
Collaboration graph

Public Member Functions

 AsyncLogger (google::base::Logger *wrapped)
 
 ~AsyncLogger ()
 
void Start ()
 start the async logger More...
 
void Stop ()
 Stop the thread. Flush() and Write() must not be called after this. NOTE: this is currently only used in tests: in real life, we enable async logging once when the program starts and then never disable it. REQUIRES: Start() must have been called. More...
 
void Write (bool force_flush, time_t timestamp, const char *message, int message_len) override
 Write a message to the log. Start() must have been called. More...
 
void Flush () override
 Flush any buffered messages. More...
 
uint32_t LogSize () override
 Get the current LOG file size. The return value is an approximate value since some logged data may not have been flushed to disk yet. More...
 
std::thread * LogThread ()
 get the log thead More...
 

Detailed Description

Wrapper for a glog Logger which asynchronously writes log messages. This class starts a new thread responsible for forwarding the messages to the logger, and performs double buffering. Writers append to the current buffer and then wake up the logger thread. The logger swaps in a new buffer and writes any accumulated messages to the wrapped Logger.

This double-buffering design dramatically improves performance, especially for logging messages which require flushing the underlying file (i.e WARNING and above for default). The flush can take a couple of milliseconds, and in some cases can even block for hundreds of milliseconds or more. With the double-buffered approach, threads can proceed with useful work while the IO thread blocks.

The semantics provided by this wrapper are slightly weaker than the default glog semantics. By default, glog will immediately (synchronously) flush WARNING and above to the underlying file, whereas here we are deferring that flush to a separate thread. This means that a crash just after a 'LOG_WARN' would may be missing the message in the logs, but the perf benefit is probably worth it. We do take care that a glog FATAL message flushes all buffered log messages before exiting.

Warning
The logger limits the total amount of buffer space, so if the underlying log blocks for too long, eventually the threads generating the log messages will block as well. This prevents runaway memory usage.

Constructor & Destructor Documentation

◆ AsyncLogger()

apollo::cyber::logger::AsyncLogger::AsyncLogger ( google::base::Logger *  wrapped)
explicit

◆ ~AsyncLogger()

apollo::cyber::logger::AsyncLogger::~AsyncLogger ( )

Member Function Documentation

◆ Flush()

void apollo::cyber::logger::AsyncLogger::Flush ( )
override

Flush any buffered messages.

◆ LogSize()

uint32_t apollo::cyber::logger::AsyncLogger::LogSize ( )
override

Get the current LOG file size. The return value is an approximate value since some logged data may not have been flushed to disk yet.

Returns
the log file size

◆ LogThread()

std::thread* apollo::cyber::logger::AsyncLogger::LogThread ( )
inline

get the log thead

Returns
the pointer of log thread

◆ Start()

void apollo::cyber::logger::AsyncLogger::Start ( )

start the async logger

◆ Stop()

void apollo::cyber::logger::AsyncLogger::Stop ( )

Stop the thread. Flush() and Write() must not be called after this. NOTE: this is currently only used in tests: in real life, we enable async logging once when the program starts and then never disable it. REQUIRES: Start() must have been called.

◆ Write()

void apollo::cyber::logger::AsyncLogger::Write ( bool  force_flush,
time_t  timestamp,
const char *  message,
int  message_len 
)
override

Write a message to the log. Start() must have been called.

Parameters
force_flushis set by the GLog library based on the configured '–logbuflevel' flag. Any messages logged at the configured level or higher result in 'force_flush' being set to true, indicating that the message should be immediately written to the log rather than buffered in memory.
timestampis the time of write a message
messageis the info to be written
message_lenis the length of message

The documentation for this class was generated from the following file: