C + + language write log class _c language

Source: Internet
Author: User
Tags sprintf

Write log class in C + + language, support write log level setting, support multithreading, support deformable parameter table write log.
Mainly provides the following interface:

    • 1, set the level of write log
    • 2, write key log information
    • 3. Write error log information
    • 4, write warning log information
    • 5, write general log information
#ifndef command_define_h
#define COMMAND_DEFINE_H
//log level message
static const char * Keyinfoprefix = "Key: \ n" ;
static const char * Errorprefix = "Error: \ n";
static const char * Warningprefix = "Warning: \ n";
static const char * infoprefix  = "Info: \ n";
 
static const int Max_str_len = 1024;
Log level enumeration
typedef enum ENUMLOGLEVEL
{
 loglevelall = 0,//All information write log
 loglevelmid,  //write error, warning message
 Loglevelnormal,  //write only error messages
 Loglevelstop  //Do not write log
};
 
#endif
#ifndef logger_h_ #define Logger_h_ #include <Windows.h> #include <stdio.h> #include "CommandDefine.h" * Class Name: Logger * Function: Provide write log function, support multithreading, support deformable parameter operation, support write log level setting * Interface: setloglevel: Set Write log level Tracekeyinfo: Ignore log level, write key information traceerror: Write wrong
 Error message tracewarning: Write a warning message traceinfo: Write General Information */class Logger {public://default constructor Logger ();
 Constructor Logger (const char * strlogpath, enumloglevel nloglevel = enumloglevel::loglevelnormal);
destructor virtual ~logger ();
 Public://write key information void Tracekeyinfo (const char * strinfo, ...);
 Write error message void TraceError (const char* strinfo, ...);
 Write warning message void tracewarning (const char * strinfo, ...);
 Write General information void Traceinfo (const char * strinfo, ...);
Set write log level void Setloglevel (Enumloglevel nlevel);
 Private://write file operation void Trace (const char * strinfo);
 Gets the current system time char * getcurrenttime ();
 Create log file name void Generatelogname ();
Create log path void Createlogpath ();
 Private://write log file stream files * m_pfilestream;
 Write log level enumloglevel m_nloglevel; Path char m_strlog for logPath[max_str_len];
 The name of the log Char M_strcurlogname[max_str_len];
The critical region variable of thread synchronization is critical_section M_cs;
 
};

 #endif
#include "Logger.h" #include <imagehlp.h> #include <time.h> #include <string.h> #include <stdarg.h
 > #pragma comment (lib, "DbgHelp.lib")//default constructor Logger::logger () {//Initialize memset (m_strlogpath, 0, Max_str_len);
 memset (m_strcurlogname, 0, Max_str_len);
 M_pfilestream = NULL;
 Set the default write log level m_nloglevel = Enumloglevel::loglevelnormal;
 Initialization critical Region variable initializecriticalsection (&AMP;M_CS);
Create log file name Generatelogname (); }//constructor Logger::logger (const char * strlogpath, Enumloglevel nloglevel): M_nloglevel (nloglevel) {//Initialize M_pfilestream
 = NULL;
 strcpy (M_strlogpath, Strlogpath);
 InitializeCriticalSection (&m_cs);
 Createlogpath ();
Generatelogname ();
 }//destructor Logger::~logger () {//release critical Zone deletecriticalsection (&AMP;M_CS);
Closes the file stream if (m_pfilestream) fclose (M_pfilestream);
//write key information interface void Logger::tracekeyinfo (const char * strinfo, ...)
 {if (!strinfo) return;
 Char Ptemp[max_str_len] = {0};
 strcpy (Ptemp, GetCurrentTime ()); strcat (Ptemp, KEYINFOPREfix);
 Gets the deformable parameter va_list arg_ptr = NULL;
 Va_start (Arg_ptr, strinfo);
 vsprintf (ptemp + strlen (ptemp), Strinfo, arg_ptr);
 Va_end (ARG_PTR);
 Write log file Trace (ptemp);
 
Arg_ptr = NULL;
//write error message void Logger::traceerror (const char* strinfo, ...)
 {//To determine the current write log level, if set to do not write log, the function returns if (M_nloglevel >= enumloglevel::loglevelstop) return;
 if (!strinfo) return;
 Char Ptemp[max_str_len] = {0};
 strcpy (Ptemp, GetCurrentTime ());
 strcat (ptemp, Errorprefix);
 Va_list arg_ptr = NULL;
 Va_start (Arg_ptr, strinfo);
 vsprintf (ptemp + strlen (ptemp), Strinfo, arg_ptr);
 Va_end (ARG_PTR);
 Trace (ptemp);
Arg_ptr = NULL;
//write warning message void logger::tracewarning (const char * strinfo, ...)
 {//To determine the current write log level, if set to write-only error message, the function returns if (M_nloglevel >= enumloglevel::loglevelnormal) return;
 if (!strinfo) return;
 Char Ptemp[max_str_len] = {0};
 strcpy (Ptemp, GetCurrentTime ());
 strcat (ptemp, Warningprefix);
 Va_list arg_ptr = NULL;
 Va_start (Arg_ptr, strinfo);
 vsprintf (ptemp + strlen (ptemp), Strinfo, arg_ptr); Va_end (ARG_PTR);
 Trace (ptemp);
Arg_ptr = NULL;
//write General information void Logger::traceinfo (const char * strinfo, ...)
 {//To determine the current write log level, if set write-only error and warning message, the function returns if (M_nloglevel >= enumloglevel::loglevelmid) return;
 if (!strinfo) return;
 Char Ptemp[max_str_len] = {0};
 strcpy (Ptemp, GetCurrentTime ());
 strcat (Ptemp,infoprefix);
 Va_list arg_ptr = NULL;
 Va_start (Arg_ptr, strinfo);
 vsprintf (ptemp + strlen (ptemp), Strinfo, arg_ptr);
 Va_end (ARG_PTR);
 Trace (ptemp);
Arg_ptr = NULL;
 //Get system Current Time char * Logger::getcurrenttime () {time_t curtime;
 struct TM * ptimeinfo = NULL;
 Time (&curtime);
 Ptimeinfo = LocalTime (&curtime);
 Char Temp[max_str_len] = {0};
 sprintf (temp, "%02d:%02d:%02d", Ptimeinfo->tm_hour, Ptimeinfo->tm_min, ptimeinfo->tm_sec);
 char * ptemp = temp; 
return ptemp;
 
//Set write log level void Logger::setloglevel (Enumloglevel nlevel) {m_nloglevel = Nlevel;}
 Write file operation void Logger::trace (const char * strinfo) {if (!strinfo) return; try {//Enter critical section entercriticalsection (&AMP;M_CS);
   If the file stream is not open, reopen if (!m_pfilestream) {char temp[1024] = {0};
   strcat (temp, m_strlogpath);
   strcat (temp, m_strcurlogname);
   M_pfilestream = fopen (temp, "A +");
   if (!m_pfilestream) {return;
  //write log information to file stream fprintf (M_pfilestream, "%s\n", strinfo);
  Fflush (M_pfilestream);
 Leaving Critical Zone leavecriticalsection (&AMP;M_CS);
 ////If an exception occurs, leave the critical section first to prevent deadlock catch (...).
 {leavecriticalsection (&AMP;M_CS);
 }///Create the name of the log file void Logger::generatelogname () {time_t curtime;
 struct TM * ptimeinfo = NULL;
 Time (&curtime);
 Ptimeinfo = LocalTime (&curtime);
 Char temp[1024] = {0}; The name of the log is: 2013-01-01.log sprintf (temp, "%04d-%02d-%02d.log", ptimeinfo->tm_year+1900, Ptimeinfo->tm_mon + 1,
 Ptimeinfo->tm_mday);
  if (0!= strcmp (m_strcurlogname, temp)) {strcpy (m_strcurlogname,temp);
  if (M_pfilestream) fclose (M_pfilestream);
  Char temp[1024] = {0};
  strcat (temp, m_strlogpath);
  strcat (temp, m_strcurlogname); Open the file stream as an append M_pfileStream = fopen (temp, "A +");
 }//create log file path void Logger::createlogpath () {if (0!= strlen (m_strlogpath)) {strcat (M_strlogpath, "\");
} makesuredirectorypathexists (M_strlogpath);

 }

The above is the entire content of this article, I hope to learn C + + help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.