/** * Single header file, can be used independently, as long as the definition of the macro Not_with_mooon, that is, do not rely on Mooon * Simple write log class, not thread-safe, to provide scrolling function by size * Does not pursue the function, nor the pursuit of performance, just simple, to strong, high performance, can
To use Clogger * * Use method: * 1) construct a Csimplelogger object * Csimplelogger Logger (".", "Test.log", 1024*1024, 10);
* 2) Invoke the Print method to write log * Logger.print ("%s\n", "Test"); * * #ifndef mooon_sys_simple_logger_h #define MOOON_SYS_SIMPLE_LOGGER_H//As long as the definition of Not_with_mooon macros,//This document and Mooon without any relationship, easy to integrate into your own code #define NOT_WITH_MOOON #if!defined (not_with_mooon) #include <sys/config.h> #endi F//Not_with_mooon #include <stdio.h> #include <stdarg.h> #include <time.h> #inclu
De <sstream> #if!defined (not_with_mooon) sys_namespace_begin #endif//Not_with_mooon/*** * Universal type conversion function */template <typename anytype> inline std::string any2string (AnyType any_value) {St
D::stringstream Result_stream;
Result_stream << Any_value;
return Result_stream.str (); /*** * To take the current time, and date_util.h have duplicates, but in order to maintain the independence of simple_logger.h, it is inevitable that * * inline void Get_current_datetime (char
* Datetime_buffer, size_t datetime_buffer_size) {struct TM result;
time_t now = time (NULL);
Localtime_r (&now, &result); snprintf (Datetime_buffer, Datetime_buffer_size, "%04d-%02d-%02d%02d:%02d:%02d", result.tm_year+1900, R
Esult.tm_mon+1, Result.tm_mday, Result.tm_hour, Result.tm_min, result.tm_sec); Class Csimplelogger {public:/*** * Constructs a csimplelogger and creates or opens a log file * @log_dir Log stored directory, do not need to end with a slash, the directory must already exist * @filename log file name, does not contain the directory section, * composed of log_dir and filename together log file path * @log_size the size of each log file, in bytes, or, if less than 1024, will be forced to 1024 * @log_numer log scrolling number * @record_size the size of a single log, more than will be truncated, in bytes
Number, if less than 1024, will be forced to 1024 * * Csimplelogger (const std::string& Log_dir , const std::string& filename, unsigned int log_size = 1024*1024*100,
unsigned char Log_numer = ten, unsigned short record_size = 8192);
~csimplelogger ();
/** whether the log file is created or opened successfully */BOOL IS_OK () const;
/** output log, used like printf, does not automatically add line break/void print (const char* format, ...);
/** refreshes the log because the file is a cached */void flush (); Private:void Reset (); /** Reset state value */void Roll_log (); /** scrolling Log * * private:file* _FP; /** The log file descriptor that is currently being written/* char* _log_buffer; /** Storage Log of buffer */int _bytes_writed; /** the number of bytes already written to * * * std::string _log_dir; /** Log Storage directory */std::string _filename; /** log file name, does not contain directory part */unsigned int _log_size; /** the size of a single log file * * unsigned char _log_numer; The number of/** log scrolling * * unsigned short _record_size;
/** a single log size, the unit is the number of bytes */}; Inline Csimplelogger::csimplelogger (conSt std::string& Log_dir, const std::string& filename, unsigned int log_size, unsigned char log_numer, unsigned short record_size): _FP (NULL
), _log_buffer (NULL), _bytes_writed (0), _log_dir (Log_dir), _filename (filename), _log_size (log_size) , _log_numer (Log_numer), _record_size (record_size) {std::string Log_path = Log_dir + std::string ("/") + fi
Lename;
_FP = fopen (Log_path.c_str (), "a");
if (_fp!= NULL) {if ( -1 = fseek (_fp, 0, Seek_end)) {//failed, no log will be written
Fclose (_FP);
_FP = NULL;
else {//Obtain an existing size _bytes_writed = Ftell (_FP);
Not too stingy. if (_log_size < 1024) {_log_size = 1024;
Not too stingy. if (_record_size < 1024) {_record_size = 1024;
} _log_buffer = new char[_record_size];
}} inline Csimplelogger::~csimplelogger () {if (_fp!= NULL) fclose (_FP);
delete []_log_buffer;
} inline bool Csimplelogger::is_ok () const {return _fp!= NULL;
} inline void Csimplelogger::p rint (const char* format, ...)
{if (_fp!= NULL) {va_list ap;
Va_start (AP, format); Char datetime_buffer[sizeof ("2012-12-21 00:00:00")];
Just the end of the world Get_current_datetime (Datetime_buffer, sizeof (Datetime_buffer));
vsnprintf (_log_buffer, _record_size, format, AP);
int bytes_writed = fprintf (_FP, "[%s]%s", Datetime_buffer, _log_buffer);
if (bytes_writed > 0) _bytes_writed + = bytes_writed; if (_bytes_writed > static_cast<int> (_log_size)) {roll_log ();
} va_end (AP); } inline void Csimplelogger::roll_log () {std::string new_path;//scrolling file path, including directory and file name std::string Old_path; The file path before scrolling, containing the directory and filename reset (); Samsara, everything starts again//history scrolling for (int i=_log_numer-1; i>0;-i) {New_path = _log_dir + std::s
Tring ("/") + _filename + std::string (".") + any2string (i);
Old_path = _log_dir + std::string ("/") + _filename + std::string (".") + any2string (i-1);
if (0 = Access (OLD_PATH.C_STR (), F_OK)) {rename (Old_path.c_str (), New_path.c_str ()); } if (_log_numer > 0) {//current scrolling New_path = _log_dir + std::string ("/")
+ _filename + std::string (". 1");
Old_path = _log_dir + std::string ("/") + _filename; if (0 = Access (OLD_PATH.C_STR (), F_OK)) {rename (), Old_path.c_str (), New_path.c_str ());
}//re-create _FP = fopen (Old_path.c_str (), "w+");
} inline void Csimplelogger::reset () {_bytes_writed = 0;
if (_fp!= NULL) {fclose (_FP);
_FP = NULL;
} inline void Csimplelogger::flush () {if (_fp!= NULL) fflush (_FP); /*** * Test code #include "simple_logger.h" int main () {Csimplelogger logger (".", "Test.log", 10240
);
for (int i=0; i<100000; ++i) logger.print ("%d ==> abcdefghijklmnopqrestuvwxyz.\n", i);
return 0; * * * #if!defined (not_with_mooon) sys_namespace_end #endif//Not_with_mooon #endif//Mooon_sys_simple_logge R_h
This article is from the "Flying Month" blog, please be sure to keep this source http://mooon.blog.51cto.com/1246491/941081