Apollo 10.0
自动驾驶开放平台
apollo::cyber::logger::AsyncLogger类 参考

更多...

#include <async_logger.h>

类 apollo::cyber::logger::AsyncLogger 继承关系图:
apollo::cyber::logger::AsyncLogger 的协作图:

Public 成员函数

 AsyncLogger (google::base::Logger *wrapped)
 
 ~AsyncLogger ()
 
void Start ()
 start the async logger
 
void Stop ()
 Stop the thread.
 
void Write (bool force_flush, time_t timestamp, const char *message, int message_len) override
 Write a message to the log.
 
void Flush () override
 Flush any buffered messages.
 
uint32_t LogSize () override
 Get the current LOG file size.
 
std::thread * LogThread ()
 get the log thead
 

详细描述

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.

警告
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.

在文件 async_logger.h73 行定义.

构造及析构函数说明

◆ AsyncLogger()

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

在文件 async_logger.cc34 行定义.

34 : wrapped_(wrapped) {
35 active_buf_.reset(new std::deque<Msg>());
36 flushing_buf_.reset(new std::deque<Msg>());
37}

◆ ~AsyncLogger()

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

在文件 async_logger.cc39 行定义.

39{ Stop(); }

成员函数说明

◆ Flush()

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

Flush any buffered messages.

在文件 async_logger.cc81 行定义.

81 {
82 for (auto& module_logger : module_logger_map_) {
83 module_logger.second->Flush();
84 }
85}

◆ 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.

返回
the log file size

在文件 async_logger.cc87 行定义.

87{ return wrapped_->LogSize(); }

◆ LogThread()

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

get the log thead

返回
the pointer of log thread

在文件 async_logger.h126 行定义.

126{ return &log_thread_; }

◆ Start()

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

start the async logger

在文件 async_logger.cc41 行定义.

41 {
42 CHECK_EQ(state_.load(std::memory_order_acquire), INITTED);
43 state_.store(RUNNING, std::memory_order_release);
44 log_thread_ = std::thread(&AsyncLogger::RunThread, this);
45 // std::cout << "Async Logger Start!" << std::endl;
46}

◆ 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.

在文件 async_logger.cc48 行定义.

48 {
49 state_.store(STOPPED, std::memory_order_release);
50 if (log_thread_.joinable()) {
51 log_thread_.join();
52 }
53
54 FlushBuffer(active_buf_);
55 ACHECK(active_buf_->empty());
56 ACHECK(flushing_buf_->empty());
57 // std::cout << "Async Logger Stop!" << std::endl;
58}
#define ACHECK(cond)
Definition log.h:80

◆ 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.

参数
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

在文件 async_logger.cc60 行定义.

61 {
62 if (cyber_unlikely(state_.load(std::memory_order_acquire) != RUNNING)) {
63 // std::cout << "Async Logger not running!" << std::endl;
64 return;
65 }
66 if (message_len > 0) {
67 auto msg_str = std::string(message, message_len);
68 while (flag_.test_and_set(std::memory_order_acquire)) {
69 cpu_relax();
70 }
71 active_buf_->emplace_back(timestamp, std::move(msg_str),
72 log_level_map.at(message[0]));
73 flag_.clear(std::memory_order_release);
74 }
75
76 if (force_flush && timestamp == 0 && message && message_len == 0) {
77 Stop();
78 }
79}
void cpu_relax()
Definition macros.h:53
#define cyber_unlikely(x)
Definition macros.h:30

该类的文档由以下文件生成: