C ++ log class (useful for the c ++ console)

Source: Internet
Author: User

Sometimes we often need to use logs when writing programs on the server side. When doing some things over the past few days, we find that there is no log class that I often use, now we have found another very good log class, so we can record it and keep it for future use:
Log. h

//////////////////////////////////////// /// // If! Defined (_ test_logincluded _) # DEFINE _ test_logincluded _ # If _ msc_ver> 1000 # pragma once # endif/_ msc_ver> 1000 # include <direct. h> # include <time. h> # include <stdio. h> /************************************* * ********************************* // program log class *//******************************** **************************************** /# define log_level_all0 # define log_level_import1 # define log_level_er Ror2 # define log_level_none3class clog {public: clog (); Virtual ~ Clog (); // is divided into four levels: 0, 1, 2, and 3. The default value is Level 2 // 0 -- the lowest level, and all logs are recorded // 1 -- the level is average, record the error log and important information // 2 -- the level is relatively high. Only the error and exception logs are recorded // 3 -- the highest level. The log void setloglevel (INT nlevel) is not recorded) {m_nlevel = nlevel ;}; bool getwritelog () {return m_nlevel ;}; int getcurrentpath (char * chpath); void add (const char * FMT ,...); PRIVATE: Enum {bufsize = 3000}; // working buffer charm_tbuf [bufsize]; int m_nlevel; // whether to record the log critical_section m_crit; // set a critical section }; # endif //! Defined (_ test_logtailincluded _)

Log. cpp:

//////////////////////////////////////// //// // # Include "stdafx. H "# include" log. H "////////////////////////////////////// /// // construction/destruction /// //////////////////////////////////////// //// // clog:: clog () {m_nlevel = log_level_error;: initializecriticalsection (& m_crit); // initialize the critical section} clog ::~ Clog () {: deletecriticalsection (& m_crit); // release the inner critical section} int clog: getcurrentpath (char * chpath) {int CH = '\\'; getmodulefilenamea (null, chpath, max_path); char * Start = (char *) chpath; while (* chpath ++)/* Find end of string */; /* search towards front */while (-- chpath! = Start & * chpath! = (Char) CH); If (* chpath = (char) CH)/* char found? */* Chpath = '\ 0'; chpath = (char *) Start; return 0;} void clog: add (const char * FMT ,...) {char chpath [512], chfile [512]; getcurrentpath (chpath); strcat (chpath, "\ ocxerrorlog"); _ mkdir (chpath); struct TM * now; time_t ltime; time (<IME); now = localtime (<IME); char szdate [20], sztime [20]; _ strdate (szdate); _ strtime (sztime ); sprintf (chfile, "% s \ log_1_d_mongod.txt", chpath, now-> tm_year + 1900, now-> tm_mon + 1, now-> tm_mday ); /* --------------------- enter the critical section (Write File) into */: entercriticalsection (& m_crit); try {va_list argptr; // analyze the string format va_start (argptr, FMT ); _ vsnprintf (m_tbuf, bufsize, FMT, argptr); va_end (argptr);} catch (...) {m_tbuf [0] = 0;} file * fp = fopen (chfile, "A"); // output to the file if (FP) {fprintf (FP, "[% S % s] \ t", szdate, sztime); fprintf (FP, "% s \ n", m_tbuf); fclose (FP );}:: leavecriticalsection (& m_crit);/* ---------------------------- exit the critical section ---------------------------------*/}

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.