The role of 1.log logs
During the software development cycle, the system typically uses a persistent log system to record operations, both in the foreground and in the background.
Embed the log code information in your code, primarily recording the following information:
(1) Record the system operation abnormal information.
(2) Record the system operation status information.
(3) record the operating performance index of the system.
Through the analysis and diagnosis of the above information, we can adopt the correct means to improve the system quality and system performance. This shows the importance of log logs in the system and the necessity of existence.
Types and levels of 2.log logs
2.1 Types of logs
Mainly divided into three major categories:
Security class information: Record system boundary interaction behavior and information;
Business class information: Record system internal business processing behavior and information;
Performance class Information: Record the support capability of the system hardware to the business processing.
2.2 Types of logs
General sub-level five:
Error: After this information is output, the principal system core module does not work properly and needs to be repaired to work properly.
WARN (warning): After this information output, the System general module has the problem, does not affect the system to run.
info (Notification): After this information is output, it is mainly to record the system running status and other related information.
Debug (Debug): The most granular output, in spite of the above conditions, you want the output of relevant information, can be output here.
Trace (Trace): The most granular output, in spite of all the above, you want to output the relevant information can be output here.
Debug and trace are not included in the simple log system implemented in this article. Debug is performed during the encoding process, and trace is not required.
2.3 Common open-source log tools
The common open source log implemented by C + + is: C + + version of log4j Log4cplus, fast C + + log library--spdlog, pure C log function library--zlog, C + + log framework--googleglog, etc.
The Open Source Log tool Log4cplus in the project is more common use, specific usage and source code please refer to the resources of the network, no longer repeat.
3. Self-implemented log tool
The following C + + simple log tools are implemented primarily for error (errors), WARN (warning), and info (notification) three types of log. Consists of a source file (logger.cpp) and a header file (logger.h). The source code is as follows.
logger.h File:
/* *\logger.h *\brief Diary module */#ifndef __logger__#define __logger__ #include <iostream> #include <iomanip> #inc Lude <fstream> #include <string> #include <cstdlib> #include <stdint.h>//////\brief The type of log file/// typedef enum Log_rank {INFO, WARNING, ERROR, fatal}log_rank_t; \brief initialization log File///\param info_log_filename Information file name///\param warn_log_filename Warning file name///\param Error_log_ FileName Error file name void Initlogger (const std::string&info_log_filename, const STD::STRING&WARN_LOG_FIL ename, const std::string&error_log_filename); \brief log System class///class Logger {friend void Initlogger (const std::string& Info_log_filename, conststd::string& Warn_log_filename, conststd::string& erro_log_filename); Public://constructor Logger (log_rank_t Log_rank): M_log_rank (Log_rank) {}; ~logger (); \brief write the source code file name, line number, function name before writing the log information \param Log_rank Log Level///\param line log the number of lines that occur///\param function log takes place functions static std::ostream& start (Log_ra nk_t Log_rank, const int32line, conststd::string& function ); Private://////\brief the corresponding log output stream///static std::ostream& GetStream (log_rank_t Log_rank) according to the rank; Static Std::ofstream M_info_log_file; < information on the day of the output stream static Std::ofstream m_warn_log_file; < The output stream of the warning message is static std::ofstream m_error_log_file; < error message output stream log_rank_t M_log_rank; < the level of log information}; \brief is read/write with different output streams according to different levels///#define LOG (Log_rank) Logger (Log_rank). Start (Log_rank, __line__,__function__)// \brief Use the diary to check the various macros///#define CHECK (a) if (!) A) {LOG (ERROR) << "CHECK failed" << Endl << #a << "=" << (a) << Endl; Abort (); } #define CHECK_NOTNULL (a) if ( NULL = = (a)) {LOG (ERROR) << "Check_notnull failed" << #a << "= = NULL" << Endl; Abort (); } #define CHECK_NULL (a) if (NULL! = (a)) { LOG (ERROR) << "Check_null failed" << Endl << #a << "! = NULL" & lt;< Endl; Abort (); } #define CHECK_EQ (A, B) if (! ( (a) = = (b)) {LOG (ERROR) << "CHECk_eq failed "<< Endl << #a <<" = "<< (a) << Endl << #b << "=" << (b) << Endl; Abort (); } #define CHECK_NE (A, B) if (! ( (a)! = (b))) {LOG (ERROR) << "Check_ne failed" << Endl << #a << "=" << (a) << Endl << #b << "=" << (b) << Endl; Abort (); } #define CHECK_LT (A, B) if (! ( (a) < (b)) {LOG (ERROR) << "Check_lt failed" << #a << "=" << (a) << Endl << #b << "= " << (b) << Endl; Abort (); } #define CHECK_GT (A, B) if (! ( (a) > (b)) {LOG (ERROR) << "Check_gt failed" << Endl << #a << "=" << (a) << Endl << #b <&L T "=" << (b) << Endl; Abort (); } #define Check_le (A, B) if (! ( (a) <= (b)) {LOG (ERROR) << "Check_le failed" << Endl << #a << "=" << (a) << Endl << #b <&L T "=" << (b) << Endl; Abort (); } #define CHECK_GE (A, B) if (! ( (a) >= (b)) {LOG (ERROR) << "Check_ge failed" << Endl << #a << "=" << (a) << Endl << #b <& Lt "=" << (b) << Endl; Abort (); } #define CHECK_DOUBLE_EQ (A, b) do { Check_le ((a), (b) +0.000000000000001l); Check_ge ((a), (b) -0.000000000000001l); }while (0) #endif
Logger.cpp File Source:
#include "logger.h" #include <cstdlib> #include <ctime> std::ofstream logger::m_error_log_file;std::o FStream Logger::m_info_log_file;std::ofstream Logger::m_warn_log_file; void Initlogger (const std::string&info_log_filename, const Std::string&warn_log_filename, Const std::string&error_log_filename) {Logger::m_info_log_file.open (Info_log_filename.c_str ()); Logger::m_warn_log_file.open (Warn_log_filename.c_str ()); Logger::m_error_log_file.open (Error_log_filename.c_str ());} std::ostream& Logger::getstream (Log_rank_tlog_rank) {return (INFO = = Log_rank)? (M_info_log_file.is_open () m_info_log_file:std::cout): (WARNING = = Log_rank?) (M_warn_log_file.is_open () m_warn_log_file:std::cerr): (M_error_log_file.is_open ()? m_error_log_fil E:std::cerr));} std::ostream& Logger::start (Log_rank_tlog_rank, const int32 line, Const std::string&function) {time_t TM; Time (&TM); Char time_string[128]; Ctime_r (&tm, time_string); Return GetStream (Log_rank) << time_string << function (<< function < < ") <<" line "<< line <<std::flush;} Logger::~logger () {GetStream (M_log_rank) << Std::endl << Std::flush; if (FATAL = = M_log_rank) {m_info_log_file.close (); M_info_log_file.close (); M_info_log_file.close (); Abort (); }}
Here's how to use it:
The first step is to create a log file by calling the initialization function Initlogger given the path of the three log files.
The second step is to call log (TYPE) << "Yourinfo" where the log is to be inserted; Your info indicates the information you want to enter into the log file.
Take the Warn log for example, the output information is as follows:
Sun Jul 5 09:49:48 2015
function (Getnexttask) line-no task to Berun
Sun Jul 5 09:49:53 2015
function (Getnexttask) line-no task to Berun
Sun Jul 5 09:49:58 2015
function (Getnexttask) line-no task to Berun
Sun Jul 5 09:50:03 2015
function (Getnexttask) line-no task to Berun
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simple log log system for C + + implementation