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

边栏推荐
- Unity Render Streaming通过Js与Unity自定义通讯
- 解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- 软件测试工作那么难找,只有外包offer,我该去么?
- Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
- 常见的几种最优化方法Matlab原理和深度分析
- SQL Injection (POST/Search)
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- 静态链表(数组的下标代替指针)
- Field problems in MySQL
- [développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
猜你喜欢

Richview trvstyle liststyle list style (bullet number)

CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team

Error running 'application' in idea running: the solution of command line is too long

解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)

Universal dividend source code, supports the dividend of any B on the BSC

SQL Injection (GET/Select)

Ocean CMS vulnerability - search php

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

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

How to use lxml to judge whether the website announcement is updated
随机推荐
Leetcode-1175. Prime Arrangements
Go 1.16.4: purpose of go mod tidy
[how to earn a million passive income]
Sequence table (implemented in C language)
User and group command exercises
Dynamic programming 01 knapsack and complete knapsack
Summary of common error reporting problems and positioning methods of thrift
Flutter dynamic | fair 2.5.0 new version features
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
The reasons why there are so many programming languages in programming internal skills
Qt学习25 布局管理器(四)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
Leetcode-1175.Prime Arrangements
Stack application (balancer)
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
GoLand 2021.1.1: configure the multi line display of the tab of the open file
Mycms we media mall v3.4.1 release, user manual update
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu