当前位置:网站首页>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 :

边栏推荐
- 【556. 下一个更大元素 III】
- Field problems in MySQL
- IBEM mathematical formula detection data set
- Go: send the get request and parse the return JSON (go1.16.4)
- Using registered classes to realize specific type matching function template
- ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
- 又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
- 【被动收入如何挣个一百万】
- Golang — template
- 双向链表(我们只需要关注插入和删除函数)
猜你喜欢

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

JVM系列——概述,程序计数器day1-1

Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)

Logback log sorting

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

Libuv Library - Design Overview (Chinese version)

Go language unit test 4: go language uses gomonkey to test functions or methods

Brief analysis of tensorboard visual processing cases

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

Using registered classes to realize specific type matching function template
随机推荐
JVM系列——概述,程序计数器day1-1
Golang — template
Swiftui development experience: the five most powerful principles that a programmer needs to master
Flutter动态化 | Fair 2.5.0 新版本特性
双向链表(我们只需要关注插入和删除函数)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Unity EmbeddedBrowser浏览器插件事件通讯
There is nothing new under the sun. Can the meta universe go higher?
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
Go: send the get request and parse the return JSON (go1.16.4)
Dynamic programming 01 knapsack and complete knapsack
Windos creates Cordova prompt because running scripts is prohibited on this system
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
Several common optimization methods matlab principle and depth analysis
Go 1.16.4: manage third-party libraries with Mod
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
【被动收入如何挣个一百万】
Students who do not understand the code can also send their own token, which is easy to learn BSC
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example