当前位置:网站首页>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 新版本特性
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- Richview trvstyle liststyle list style (bullet number)
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
- Software testing is so hard to find, only outsourcing offers, should I go?
- Libuv库 - 设计概述(中文版)
- There is nothing new under the sun. Can the meta universe go higher?
- RichView TRVStyle ListStyle 列表样式(项目符号编号)
- Qt学习20 Qt 中的标准对话框(中)
- Unity EmbeddedBrowser浏览器插件事件通讯
猜你喜欢

Kivy tutorial how to automatically load kV files

双向链表(我们只需要关注插入和删除函数)

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Brief analysis of tensorboard visual processing cases

Halcon combined with C # to detect surface defects -- Halcon routine autobahn

Qt学习17 对话框及其类型

GoLand 2021.1.1: configure the multi line display of the tab of the open file

Flutter动态化 | Fair 2.5.0 新版本特性

全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场

GoLand 2021.2 configure go (go1.17.6)
随机推荐
Go language unit test 3: go language uses gocovey library to do unit test
Qt学习19 Qt 中的标准对话框(上)
MySQL installation, uninstallation, initial password setting and general commands of Linux
Qt学习23 布局管理器(二)
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
RichView TRVStyle ListStyle 列表样式(项目符号编号)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
PHP maze game
Qt学习20 Qt 中的标准对话框(中)
User and group command exercises
GoLand 2021.1: rename the go project
GoLand 2021.1.1: configure the multi line display of the tab of the open file
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
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
SQL Injection (GET/Select)
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
JS convert pseudo array to array
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
Resource Cost Optimization Practice of R & D team