Expandable platform-independent log class with Template

zhaozj2021-02-17  57

In a project development, you often need to write some running logs, Debug information. Just I am learning GP-related things, I wrote this thing, I hope to throw bricks.

/ * @ ********************************************************* ***************************** File name: mjlog.h ** Copyright Chen mingjie 2001 bloodchen@hotmail.com*** *********************************************************** *********************************** / # ifndef _MJTOOLS_CMJLOG_ # define _mjtools_cmjlog_namespace mjtools // namespace defination for my tools {class generallogcontrol // base class of any specific log control class, providing basic controls {protected: bool m_bEnabled; public: GeneralLogControl (const std :: string ControlStr): m_bEnabled (true) {}; // log is enabled by default GeneralLogControl (): m_bEnabled (true) {} ; virtual ~ GeneralLogControl () {}; virtual void Pause () {m_bEnabled = false;} virtual void Resume () {m_bEnabled = true;} virtual bool CanAdd (std :: string & LogStr) {return m_bEnabled;}}; # ifdef WIN32 // RegistryLogControl only valid in Windows system class RegistryLogControl: public GeneralLogControl {protected: std :: string m_ControlReg; bool CheckReg (const std :: string & reg); public: RegistryLogControl (const std :: string ControlStr): m_ControlReg (ControlStr) {m_bEnabled = CheckReg (ControlStr);} void Resume () {m_bEnabled = CheckReg (m_ControlReg);} virtual ~ RegistryLogControl () {};}; # endif class FileLogImpl // class that implements file logging ability {protected: std :: string m_LogFileName; public: FileLogImpl (const std :: string FileName): m_LogFileName (FileName) {if (m_LogFileName == "") m_LogFileName = "log.txt"; // default file name}; virtual void AddTimeStamp (std :: string & LogStr); // you may want to override this method to provide your time stamp style virtual ~ FileLogImpl () {}; virtual bool AddLog (std :: string & LogStr );

// you may want to override this method to provide your log style virtual void Clear (); // clear log file content}; template class CMjLog {protected: ControlT m_Control; // log control class ImplT m_Impl; // log implementation class public: CMjLog (const std :: string ImplStr = "", const std :: string ControlStr = ""): m_Control (ControlStr), m_Impl (ImplStr) {}; virtual ~ CMjLog () { }; bool AddLog (const std :: string & LogStr) // Add one piece of log message {return AddLog (LogStr.c_str ());} bool AddLog (const char * pLogStr) // Add one piece of log message {std :: string log = plogstr; if (m_control.canadd (log)) Return m_impl.addlog (log); Else Return False;} void pause () // Pause log process {m_control.pause (); void resume () // resume log process {m_control.resume ();} void clear () // clear log content {m_impl.clear ();}}; / * -------------- CFILOG Definition ------------------ * / Typedef cmjlog cfile Log; / * ------------ CFILELOG USAGE ------------------------- Mjtools :: cfilelog log (" C: //test.log "); // construct a new logfile or open a existing log file log.clear (); // delete previous logs. Log.addlog (" this is a test line "); // add One log message ---------------------------------------------- ---- * /

#ifdef Win32 // RegistryLogControl Only Valid in Windows System / * -------------- CREGFILOG Definition ------------------ * / Typedef CMJLOG CREGFILOG; / * ------------ CREGFILOG USAGE ----------------------- MJTools :: CREGFILOG log ("c: //test.log", "hkey_local_machine // Software // YourLogControlKey"); // Construct a new logfile or open a existing log file log.clear (); // delete previous logs. Log.addlog ("this is a test line"); // Add one log message ------------------------------- -------------------- * / #ENDIF} #ENDIF / * @ $ ******************** *********************************************************** ******* File name: mjlog.cpp ** Copyright Chen mingjie 2001 bloodchen@hotmail.com*********************************** *********************************************************** *** /

#include #include #include #include "mjlog.h" #include "time.h" # ifefdef win32 # include #include #ENDIFNAMESPACE MJTOOLS { void FileLogImpl :: AddTimeStamp (std :: string & LogStr) {time_t long_time; time (& long_time); struct tm * pt = localtime (& long_time); if (pt) {std :: ostringstream stream; stream << "AT:" < TM_HOUR << ':' << Pt-> TM_MIN << ':' << Pt-> TM_SEC << ',' << Pt-> TM_Year 1900 << '/' << Pt-> TM_MON 1 << '/' << Pt-> TM_MDAY << "-------->"; logstr = stream.str () logstr;}} Bool FilelogImpl :: AddLog (std :: string & {AddTimeStamp (logstr); std :: OFSTREAM OS (M_LogFileName.c_STR (), std :: os :: app); os << logstr; os.flush (); OS. CLOSE (); Return True;} Void Filelogimpl :: clear () // Clear File Content {std :: OFSTREAM OS (M_LogFileName.c_STR (), std :: os :: out); Os.Flush (); OS. CLOSE ();} #iDef Win32 // RegistryLogControl Only Valid in Windows System Bool RegistryLogcontrol :: Checkreg (const st :: string & reg) // check if required - CHECK IF Requir ed reg key exists {HKEY hKey; HKEY hKeyRoot; bool ret = true; (! m_ControlReg = "") if // if no control_reg_key provided, it means "no reg_key control" {int nPos = m_ControlReg.find ( '//' ); If (npos == - 1) RET = false; else {std :: string root = m_controlreg.substr (0, npos); std :: string rest = m_controlreg.substr (npos 1); if (root = = "HKEY_LOCAL_MACHINE") HKEYROOT = HKEY_LOCAL_MACHINE; if (root == "

HKEY_CLASSES_ROOT ") hKeyRoot = HKEY_CLASSES_ROOT; if (root ==" HKEY_CURRENT_CONFIG ") hKeyRoot = HKEY_CURRENT_CONFIG; if (root ==" HKEY_CURRENT_USER ") hKeyRoot = HKEY_CURRENT_USER; if (root ==" HKEY_USERS ") hKeyRoot = HKEY_USERS; if (root = = "HKEY_PERFORMANCE_DATA") hKeyRoot = HKEY_PERFORMANCE_DATA; if (root == "HKEY_DYN_DATA") hKeyRoot = HKEY_DYN_DATA; ret = (:: RegOpenKey (hKeyRoot, rest.c_str (), & hKey) == ERROR_SUCCESS); :: RegCloseKey (hKey) }}}}}; # Endif} #ifdef _test_int main () {mjtools :: cfilelog m_log ("test.log"); std :: string a = "aaa"; m_log.clear (); m_log.addlog ( "Abc"); m_Log.AddLog (a); MjTools :: CFileLog m_Log1 = m_Log; m_Log1.AddLog ( "From Log1"); # ifdef WIN32 // RegistryLogControl only valid in Windows system // construct a registry key controled log . object If the specified registry key is found, the log is enabled MjTools :: CRegFileLog m_regLog ( "reglog.log", "HKEY_LOCAL_MACHINE // Software // YourLogControlKey"); m_regLog.AddLog ( "reglog"); m_regLog.Pause ( ); M_reglog.addlog ("reglog1"); M_REGLOG .Resume (); m_reglog.addlog ("reglog2"); # endif return 0;} # ENDIF How to compile these two files itself can be compiled under different OS, such as

VC : CL / D "_TEST_" mjlog.cpp / with command line LINK will have a fault, I don't know why, build an empty Win32 console project and join these two files is not wrong (don't forget predefine _test_)

BC : BCC32 / D_TEST_ MJLOG.CPP

G : g / d_test mjlog.cpp

How to expand

Just establish (or inherited) your own Control Class or Impl Class can be used for a variety of purposes, but the interface to the user is constant.

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

New Post(0)