Recently wrote a log class, very easy to use :)

xiaoxiao2021-04-03  222

Recently I have written a log class CLOG. In the actual system, I have been inspected in the actual system, the effect is good, posted and exchanges exchanges, and there is no way to improve.

Some of the CLOG classes are now explained:

This class uses the MFC's cfilefind, if not, the CFILEFIND implemented lookup folder function can be implemented with PathFileExists, of course, the CString type also needs to be converted. I have stopped writing log functions, so it is a thread.

The program generates a log folder in the current directory (if the folder exists, no), the daily log is placed in a folder named after the day.

Max_row defines the maximum number of rows of a log file, and a log file is turned off after this number of log files, and create a new file.

Void disable (); Close log function

Void enable (); Open log function

INT newlogfile (); create a new log file

Bool ischangeday (); judgment if it is cross-day (0 am in the morning)

CString getCurnTtime (); get the current time string, format is 2006-06-21-11-50

Void Writelogfile (const char * fmt, ...); write log function

Head file logfile.h

#include #include #include #include

#define max_row 5000

Class clog {public: void disable (); void enable (); clog (); ~ clog (); void writelogfile (const char * fmt, ...); protected: int newlogfile (); bool iSchangeDay (); cstring GetCurnTtime (); Private: int m_filehandle; int m_row; int m_dirnum; int m_filenum; bool m_needlog; char m_scontent [1024 * 1024]; cstring m_strcontent;

CSTRING M_SCURTTIME; CSTRING M_SYESTADY; CSTRING M_SCURTDIR; CSTRING M_SLOGDIR; CSTRING M_SLOGDAYDIR;

CcriticalSECTION M_CS;

Implement code logfile.cpp

#include "stdafx.h" #include "logfile.h"

#include

Static char log_directory [] = "IVR_LOG";

CLOG :: Clog () {m_row = 0; m_filehandle = 0; m_filenum = 0; m_filenum = 0; m_scurttime = _t ("); m_stoday = _t ("); m_syestady = _t ("); m_scurtdir = _t (""); M_slogdir = _t (""); m_needlog = false;

GetCurrentDirectory (_MAX_PATH, m_sCurtDir.GetBuffer (_MAX_PATH)); m_sCurtDir.ReleaseBuffer (); CString sDirName; CFileFind f; BOOL bHasLogDir = FALSE; BOOL bFind = f.FindFile (m_sCurtDir "//*.*");

While (bfind) {bfind = f.FindNextFile (); if (f.ISDOTS ()) Continue; if (f.Indirectory ()) {sdirName = f.GetFileName (); sdirname.maker (); if (sdirName = = (Cstring) log_directory) {bhasLogdir = true; break;}}} m_slogdir = m_scurtdir (cstring) "//" (cstring) log_directory; if (! Bhaslogdir) {_mkdir (m_slogdir);}

Cstring startime = getCurnTtime ();

m_stoday = setime.mid (0, 10);}

CLOG :: ~ clog () {_close (m_filehandle);

INT CLOG :: NewLogfile () {CString StrlogfileName

if (m_filenum> 0) {_close (m_filehandle);} strLogfileName = m_sLogDayDir (CString) "//" m_sCurtTime (CString) "txt."; if ((m_filehandle = _open (strLogfileName, _O_WRONLY | _O_CREAT | O_TRUNC, _S_iread | _s_iwrite) == -1) {PrintF ("Cannot Create Log File:% S / N", strlogfilename); Return -1;} m_filenum ; return 0;}

BOOL CLog :: IsChangeDay () {m_sCurtTime = GetCurntTime (); m_sToday = m_sCurtTime.Mid (0,10); if (m_sToday = m_sYestady!) {M_sYestady = m_sToday; m_sLogDayDir = m_sLogDir (CString) "//" m_sToday _Mkdir (m_slogdaydir); return true;} Return False;}

Void Clog :: Writelogfile (const char * fmt, ...) {if (m_needlog) {m_cs.lock ();

IF (m_row> = max_row || ischangeDay ()) {if (newlogfile () == -1) {m_cs.unlock (); return;} m_row = 0;} va_list v_args; va_start (v_args, fmt); vsprintf M_SCONTENT, FMT, V_ARGS; VA_END (V_ARGS); CSTRING STRTIME; CTIME CTIME = CTIME:: GetCurrentTime (); start = _t ("20") cTIME.Format ("% Y-% M-% D% H: % M:% s "); / * char stime [32]; time_t now = time (null); TM * TM_TIME = localtime (& now); // Note, localtime non-thread security strftime (stime, sizeof (stime), "20% Y-% M-% D% H:% M:% S", TM_TIME); * / m_strcontent = (cstring) "[" STRTIME (CSTRING) "]" (cstring) m_scontent "/ N "; _ write (m_filehandle, m_strcontent, m_strcontent.getlength ()); m_row ;

m_cs.unlock ();}}

CString Clog :: getcurnttime () {cstring start;

CTIME CTIME = CTIME :: getCurrentTime (); start = _t ("20") ctime.format ("% Y-% M-% D-% H-% M-% S");

Return strtime;}

Void clog :: enable () {m_needlog = true;

Void clog :: disable () {m_needlog = false;}

转载请注明原文地址:https://www.9cbs.com/read-131618.html

New Post(0)