当前位置:网站首页>Implementation of Muduo asynchronous logging
Implementation of Muduo asynchronous logging
2022-07-03 13:53:00 【yolo_ yyh】
Catalog
Two 、 The destination of the log
3、 ... and 、 Multithreaded asynchronous log implementation
muduo It's for Chen Shuo's personal use C++ Developed a network library , The code is of great learning value , The summary comes from books 《Linux Multithreaded server-side programming 》, Also written by Chen Shuo , Can cooperate with github Use code together .
muduo github website :https://github.com/chenshuo/muduo
One 、 Log level and format
TRACE、DEBUG、INFO、WARN、ERROR、FATAL, The default level is obtained from the environment variable , To adjust the level, you only need to call Logger::setLogLevel() that will do ;
muduo The format of the log message is as follows :

The date and time in the timestamp string are cached , Multiple logs within one second only need to reformat the microsecond part .
Two 、 The destination of the log
Write to a local file .
Rolling condition : File size and time , For program crash , The log cannot flush To hard disk ,muduo On a regular basis ( every other 3 second ) Log the buffer flush To hard disk , And every log message in memory has cookie( The address of a function ), This can be done in core dump Find in the file cookie You can find messages that haven't been written to the disk yet .
3、 ... and 、 Multithreaded asynchronous log implementation
(1) characteristic
Each process writes only one log file , Use a background thread to collect log information , And write the log file , Other business threads just send log messages to this background thread .
(2) Concrete realization
Double buffering technology : Prepare two pieces buffer,A、B, The front end is responsible for buffer A Fill in the data , The back end is responsible for buffer B The data for is written to the file , When buffer A After full , In exchange for A and B, Let the back end bufferA The data for is written to the file , The front end continues bufferB Fill in the new message , So back and forth .
In order to write the log to the file in time , Even if buffer A Not full , Also every 3 Seconds for swap write operation .
When the front-end write frequency is not high :

When the front-end write frequency is high :

Front end write frequency is too high , When messages are dense :

边栏推荐
- Flutter动态化 | Fair 2.5.0 新版本特性
- IBEM mathematical formula detection data set
- Father and basketball
- Mycms we media mall v3.4.1 release, user manual update
- SQL Injection (AJAX/JSON/jQuery)
- 编程内功之编程语言众多的原因
- Golang — template
- [redis] cache warm-up, cache avalanche and cache breakdown
- Error running 'application' in idea running: the solution of command line is too long
- [how to earn a million passive income]
猜你喜欢

Internet of things completion -- (stm32f407 connects to cloud platform detection data)

Unity EmbeddedBrowser浏览器插件事件通讯

Qt学习23 布局管理器(二)

Kivy tutorial how to automatically load kV files

KEIL5出现中文字体乱码的解决方法

GoLand 2021.2 configure go (go1.17.6)

User and group command exercises

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

8 Queen question

SQL Injection (GET/Select)
随机推荐
Qt学习25 布局管理器(四)
Golang — template
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
Common network state detection and analysis tools
Qt学习17 对话框及其类型
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
记录关于银行回调post请求405 问题
[how to earn a million passive income]
项目协作的进度如何推进| 社区征文
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Use and design of Muduo buffer class
HALCON联合C#检测表面缺陷——HALCON例程autobahn
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
Sequence table (implemented in C language)
编程内功之编程语言众多的原因
RichView TRVStyle ListStyle 列表样式(项目符号编号)
物联网毕设 --(STM32f407连接云平台检测数据)
Unity EmbeddedBrowser浏览器插件事件通讯