C ++ Coding

Source: Internet
Author: User

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:

 
 
  1. // Log. h
  2.  
  3. # Ifndef _ LOG_H _
  4. # Define _ LOG_H _
  5. /*
  6. LOG Library (WIN98/NT/2000) ver 0.1
  7.  
  8. Compile by: BC ++ 5; C ++ BUILDER 4, 5, 6, X; VC ++ 5, 6; VC. NET; GCC;
  9. Copyright (c) 2006.5-2007.4 llbird wushaojian@21cn.com http://blog.csdn.net/wujian53
  10.  
  11. Use:
  12. This is a simple log. It uses C-style functions and supports multithreading.
  13. You only need to include this file and add the log. cpp file to the project.
  14. In VC, you may need to add # include "stdafx. h" to log. cpp"
  15. Usage
  16. InitLog (); // Initialization
  17. LOG ("program startup ");
  18. LOG1 ("% s", str );
  19. DestroyLog (); // optional
  20.  
  21. When debugging, the output can be defined as LOG_TO_STD or LOG_TO_DEBUG.
  22. For C ++ Builder
  23. Available
  24. LOG (Exception * e or Exception & e );
  25. For WIN32 APIs
  26. LOG_LAST_ERROR ();
  27. For _ com_error
  28. LOG (_ com_error & e );
  29. */
  30.  
  31. # Include<Stdio. h> 
  32. # Include<Time. h> 
  33. # Include<Windows. h> 
  34. # Include<Process. h> 
  35. // Use the short original file name
  36. # Define LOG_SHORT_SOURCE_FILE
  37. // Use Log
  38. # Define LOG_TO_FILE
  39. // Define a standard error output device
  40. # Define LOG_STD_DEV stderr
  41. // Use a standard output device
  42. // # Define LOG_TO_STD
  43. // Output to the debugging window
  44. // # Define LOG_TO_DEBUG
  45. // Output messagebox
  46. // # Define LOG_TO_MESSAGE_BOX
  47. // Multi-thread critical section
  48. Extern CRITICAL_SECTION _ g_LogMutex;
  49. // Global Log File Name
  50. Extern char _ g_LogFileName [MAX_PATH];
  51. Extern void InitLog ();//>Initialization log
  52. Extern void DestroyLog ();//>Clear logs
  53. Extern BOOL Log (const char * src/* source program name */, int line/* line number */, const char * description/* description */);//>Add log
  54.  // Record the log macro list
  55. # Define LOG (arg) Log (_ FILE __, _ LINE __, (arg ))
  56. // Log macro with multiple parameters
  57. # Define LOG1 (str, p1) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1); LOG (buffer );}
  58. # Define LOG2 (str, p1, p2) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2); LOG (buffer );}
  59. # Define LOG3 (str, p1, p2, p3) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3 )); LOG (buffer );}
  60. # Define LOG4 (str, p1, p2, p3, p4) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3 ), (p4); LOG (buffer );}
  61. # Define LOG5 (str, p1, p2, p3, p4, p5) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3 ), (p4), (p5); LOG (buffer );}
  62. // Record windows API error values
  63. # Define LOG_LAST_ERROR () {LOG_SPRINTF_BUFFER; DWORDEid=GetLastError(); Sprintf (buffer, "Last Error (% d):", eid); intLen=Strlen(Buffer );\
  64. FormatMessage (\
  65. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,\
  66. NULL ,\
  67. Eid ,\
  68. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT ),\
  69. Buffer + len ,\
  70. DEFAULT_LOG_SPRINTF_BUFFER_SIZE-len-1 ,\
  71. NULL \
  72. );\
  73. LOG (buffer );\
  74. }\
  75.  
  76. # If defined (_ cplusplus) & defined (_ INC_COMDEF)
  77. /// Add a COM error message
  78. Inline BOOL Log (const char * src, int line, _ com_error & e)
  79. {
  80. Char buffer [DEFAULT_LOG_SPRINTF_BUFFER_SIZE];
  81. Sprintf (buffer, "_ com_error \TCode= % X \ tCodeMeaning= % S \TSource= % S \TDescription= % S ",
  82. E. error (), (LPCSTR) (_ bstr_t) e. errorMessage (), (LPCSTR) (_ bstr_t) e. source (), (LPCSTR) (_ bstr_t) e. description ());
  83. Return LOG_POS (src, line, buffer );
  84. }
  85. # Endif
  86.  
  87. /// Add VCL exception information
  88. # If defined (_ cplusplus) & defined (_ BORLANDC _) & defined (INC_VCL)
  89. Inline BOOL Log (const char * src, int line, Exception * e)
  90. {
  91. Return LOG_POS (src, line, e->Message. c_str ());
  92. }
  93. Inline BOOL Log (const char * src, int line, Exception & e)
  94. {
  95. Return LOG_POS (src, line, e. Message. c_str ());
  96. }
  97. # Endif
  98.  
  99. # 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.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

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.