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
#ifdef Win32 // RegistryLogControl Only Valid in Windows System / * -------------- CREGFILOG Definition ------------------ * / Typedef CMJLOG
#include
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.