c ++ Based on log files

All commercial software or online system has logging, because the log information provides an important record since the operation or state transition system startup, is to track the various exception error firsthand. Most of the logging module system automatically maintains a history log file, namely: the log file size reaches the agreed limit, automatically dump into a new history log file, the current file emptied and continue to record new log information, for example: assume that the current test.log log file name, size when it reaches the upper limit (e.g., 10MB), put the contents of the dump file to the new file test.log.1, test.log then emptied and continues to record new information. Depending on the configuration, we can keep 1 to N copies of the history log file. When the number of log file history to reach the upper limit, we can use round-robin strategy (or other policy) based on document generation time in order to cover the old file. This paper attempts to achieve these log files function with C ++, its features are summarized as follows:

1) log files provide an interface allowing users to configure the log file name, the maximum size of the log, the maximum number of historical log files;

2) log files provide Append () interface that allows users to append log messages to a file;

3) execution log file in the Append () interfaces during the current log file size automatically detected: If added before the current message, the file size exceeds the agreed limit, then the current message record, the message will have to ensure that the dump to the history file and history log files does not exceed the agreed limit; no person, directly recording the current message;

4) log files provide an interface allowing users to configure whether the history log file compression;

LogFile class implements the above functions, look at its interface definition:

 1 #ifndef _LOGFILE_H
 2 #define _LOGFILE_H
 3 
 4 #include <fstream>
 5 #include <iostream>
 6 
 7 
 8 class LogFile {
 9 public:
10     LogFile(const LogFile &) = delete;
11     LogFile& operator=(const LogFile &) = delete;
12     LogFile(const std::string&, double, unsigned int);
13 
14     ~LogFile();
15     void Append(std::string &&msg);
16 
17 private:
18     void Rotate();
19     double GetFileSize();
20     std::string NextHistoryFile();
21 
22 private:
23     std::ofstream ofs_;
24     std::string file_name_;
25     double cur_size_;
26     double max_size_;
27     unsigned int max_file_num_;
28 };
29 
30 #endif //_ROTATEFILE_H

LogFile member variable declarations:

ofs_: c ++ std :: ofstream types of objects, "<<" message to the log file by the operator thereof

file_name_: log file name;

cur_size_: real-time record of the current log file size, to avoid every Append execute () call the system function to obtain the file size of the operation;

max_size_: the upper limit of the log file specified by the user;

max_file_num_: a user-specified maximum number of historical documents;

LogFile member function Description:

LogFile (const std :: string &, double, unsigned int):  The only constructor LogFile class, you can log file name, specify the maximum file size limit, the number of historical documents, its implementation is very simple:

 

 

 

 

Guess you like

Origin www.cnblogs.com/wangwenzhi2019/p/11100331.html