In the back-end code, the log everywhere, the design of their own set of log management code, provide a good set of log interface will greatly facilitate the development of code.
In the writing of log management code, there are the following difficulties:
1. The number of uncertain parameters of the entry function is written
2. Log permission control
3. Log output form.
Interface design:
1. Provide three types of log printing form: 1) Console printing information, similar to printf interface encapsulation
2) function tracking interface, print the current code of the file name, function name and line, as well as some set of output parameters
3) Log printing function, provide print level control, and print content output to the log file
2. Provide log level control: 1) provide the current log level when printing the log, code to control printing according to level
2) Log Print level control temporarily uses configuration files, which can be modified in real time via communication interface
The following is a code implementation: DMLogManager.h
1 //=============================================================================2 /* 3 * File:dmlogmanager.h4 *5 * author:bing6 *7 * date:2016-09-078 *9 * version:v2.0Ten * One * Github/mail:Https://github.com/binchen-china<[email protected]> A * - * Note: - */ the //============================================================================= - - #pragmaOnce -#include"DMaker.h" + - enumLog_level + { ADm_error =0x0001, atDm_warning =0x0010, -Dm_info =0x0100, -Dm_debug =0x1000 - }; - - classDmlogmanager in { - Public: to Dmlogmanager (); + -~Dmlogmanager (); the * voidPrint_log (Constdm_char*fmt, ...); $ Panax Notoginseng voidTrace_log (stringFilestringFunc, Dm_int Line,Constdm_char*fmt, ...); - the voidWrite_log (Dm_int Log_level,stringFilestringFunc, Dm_int Line,Constdm_char*fmt, ...); + A Private: the voidinit (); + - voidget_log_config (); $ $Inlinevoidopen_log_file (); - -Inlinevoidclose_log_file (); the - voidset_log_level ();Wuyi the Private: -file*_log_file; Wu string_log_name; - string_log_level; About Dm_int _log_mask; $ }; - -typedef Ace_singleton<dmlogmanager, Ace_thread_mutex>dmlogmgr; - A //Console output without code info + #defineDm_print (Log_fmt,args ...) Dmlogmgr::instance ()->print_log (log_fmt,# #args) the //Console output with code info - #defineDm_trace (Log_fmt,args ...) Dmlogmgr::instance ()->trace_log (__file__,__function__,__line__,log_fmt,# #args) $ //write logs into log file the #defineDm_log (Log_level,log_fmt,args ...) Dmlogmgr::instance ()->write_log (log_level,__file__,__function__,__line__,log_fmt,# #args)
DMLogManager.cpp
#include"DMLogManager.h"Dmlogmanager::D mlogmanager (): _log_mask (0) {init ();} Dmlogmanager::~Dmlogmanager () {}voidDmlogmanager::init () {get_log_config (); Set_log_level ();}voidDmlogmanager::get_log_config () {_log_name= Dmjsoncfg::instance ()->getitemstring ("Service_info","service_name"); _log_level= Dmjsoncfg::instance ()->getitemstring ("Service_info","Log_level"); _log_name.append (". Log");} InlinevoidDmlogmanager::open_log_file () {_log_file= fopen (_log_name.c_str (),"a"); if(nullptr = =_log_file) { return; }}inlinevoidDmlogmanager::close_log_file () {fclose (_log_file);}voidDmlogmanager::set_log_level () {if("DEBUG"==_log_level) {_log_mask=0x1111; } Else if("INFO"==_log_level) {_log_mask=0x0111; } Else if("WARNING"==_log_level) {_log_mask=0x0011; } Else if("ERROR"==_log_level) {_log_mask=0x0001; }}voidDmlogmanager::p Rint_log (Constdm_char*FMT, ...) {va_list ap; Va_start (AP, FMT); stringLog_info =FMT; ace_os::vfprintf (stdout, FMT, AP); Va_end (AP);}voidDmlogmanager::trace_log (stringFilestringFunc, Dm_int Line,Constdm_char*FMT, ...) {va_list ap; Va_start (AP, FMT); Ace_os::p rintf ("[dm_trace][%s][%s][%d]:", File.c_str (), Func.c_str (), line); stringLog_info =FMT; ace_os::vfprintf (stdout, FMT, AP); Va_end (AP);}voidDmlogmanager::write_log (Dm_int Log_level,stringFilestringFunc, Dm_int Line,Constdm_char*FMT, ...) {va_list ap; Va_start (AP, FMT); Open_log_file (); Switch(log_level) { CaseDm_debug: {if(Dm_debug &_log_mask) {ace_os::fprintf (_log_file,"[dm_debug][%s][%s][%d]:", File.c_str (), Func.c_str (), line); ace_os::vfprintf (_log_file, FMT, AP); } Break; } CaseDm_info: {if(Dm_info &_log_mask) {ace_os::fprintf (_log_file,"[dm_info][%s][%s][%d]:", File.c_str (), Func.c_str (), line); ace_os::vfprintf (_log_file, FMT, AP); } Break; } Casedm_warning: {if(Dm_warning &_log_mask) {ace_os::fprintf (_log_file,"[dm_warning][%s][%s][%d]:", File.c_str (), Func.c_str (), line); ace_os::vfprintf (_log_file, FMT, AP); } Break; } Casedm_error: {if(Dm_error &_log_mask) {ace_os::fprintf (_log_file,"[dm_error][%s][%s][%d]:", File.c_str (), Func.c_str (), line); ace_os::vfprintf (_log_file, FMT, AP); } Break; }} close_log_file (); Va_end (AP);}
For more technical information, please follow GitHub:Https://github.com/binchen-china
Architecture Design-Log Management interface design