This article mainly describes the C ++ code. Once you have mastered some programming skills and methods, the tedious and complex code won't make it difficult for some scholars and technical personnel specialized in development. The following code is an example.
The C ++ code is as follows:
- // Log. h
-
- # Ifndef _ LOG_H _
- # Define _ LOG_H _
- /*
- LOG Library (WIN98/NT/2000) ver 0.1
-
- Compile by: BC ++ 5; C ++ BUILDER 4, 5, 6, X; VC ++ 5, 6; VC. NET; GCC;
- Copyright (c) 2006.5-2007.4 llbird wushaojian@21cn.com http://blog.csdn.net/wujian53
-
- Use:
- This is a simple log. It uses C-style functions and supports multithreading.
- You only need to include this file and add the log. cpp file to the project.
- In VC, you may need to add # include "stdafx. h" to log. cpp"
- Usage
- InitLog (); // Initialization
- LOG ("program startup ");
- LOG1 ("% s", str );
- DestroyLog (); // optional
-
- When debugging, the output can be defined as LOG_TO_STD or LOG_TO_DEBUG.
- For C ++ Builder
- Available
- LOG (Exception * e or Exception & e );
- For WIN32 APIs
- LOG_LAST_ERROR ();
- For _ com_error
- LOG (_ com_error & e );
- */
-
- # Include<Stdio. h>
- # Include<Time. h>
- # Include<Windows. h>
- # Include<Process. h>
- // Use the short original file name
- # Define LOG_SHORT_SOURCE_FILE
- // Use Log
- # Define LOG_TO_FILE
- // Define a standard error output device
- # Define LOG_STD_DEV stderr
- // Use a standard output device
- // # Define LOG_TO_STD
- // Output to the debugging window
- // # Define LOG_TO_DEBUG
- // Output messagebox
- // # Define LOG_TO_MESSAGE_BOX
- // Multi-thread critical section
- Extern CRITICAL_SECTION _ g_LogMutex;
- // Global Log File Name
- Extern char _ g_LogFileName [MAX_PATH];
- Extern void InitLog ();//>Initialization log
- Extern void DestroyLog ();//>Clear logs
- Extern BOOL Log (const char * src/* source program name */, int line/* line number */, const char * description/* description */);//>Add log
- // Record the log macro list
- # Define LOG (arg) Log (_ FILE __, _ LINE __, (arg ))
- // Log macro with multiple parameters
- # Define LOG1 (str, p1) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1); LOG (buffer );}
- # Define LOG2 (str, p1, p2) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2); LOG (buffer );}
- # Define LOG3 (str, p1, p2, p3) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3 )); LOG (buffer );}
- # Define LOG4 (str, p1, p2, p3, p4) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3 ), (p4); LOG (buffer );}
- # Define LOG5 (str, p1, p2, p3, p4, p5) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3 ), (p4), (p5); LOG (buffer );}
- // Record windows API error values
- # Define LOG_LAST_ERROR () {LOG_SPRINTF_BUFFER; DWORDEid=GetLastError(); Sprintf (buffer, "Last Error (% d):", eid); intLen=Strlen(Buffer );\
- FormatMessage (\
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,\
- NULL ,\
- Eid ,\
- MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT ),\
- Buffer + len ,\
- DEFAULT_LOG_SPRINTF_BUFFER_SIZE-len-1 ,\
- NULL \
- );\
- LOG (buffer );\
- }\
-
- # If defined (_ cplusplus) & defined (_ INC_COMDEF)
- /// Add a COM error message
- Inline BOOL Log (const char * src, int line, _ com_error & e)
- {
- Char buffer [DEFAULT_LOG_SPRINTF_BUFFER_SIZE];
- Sprintf (buffer, "_ com_error \TCode= % X \ tCodeMeaning= % S \TSource= % S \TDescription= % S ",
- E. error (), (LPCSTR) (_ bstr_t) e. errorMessage (), (LPCSTR) (_ bstr_t) e. source (), (LPCSTR) (_ bstr_t) e. description ());
- Return LOG_POS (src, line, buffer );
- }
- # Endif
-
- /// Add VCL exception information
- # If defined (_ cplusplus) & defined (_ BORLANDC _) & defined (INC_VCL)
- Inline BOOL Log (const char * src, int line, Exception * e)
- {
- Return LOG_POS (src, line, e->Message. c_str ());
- }
- Inline BOOL Log (const char * src, int line, Exception & e)
- {
- Return LOG_POS (src, line, e. Message. c_str ());
- }
- # Endif
-
- # Endif _ LOG_H _
After reading the above many C ++ codes, I believe everyone is a bit confused. So let's take a look at it.
- How to Write C ++ project development and project plan correctly
- Summary Notes on learning and exploring C ++ library functions
- In-depth demonstration of high security of C ++
- Describes in detail how to accurately Write C ++ languages.
- In-depth demonstration of high security of C ++