龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

漫谈C++代码编写

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
本文主要进行对C++代码进行学习与说明,一旦掌握了一些编程的技巧和方式,在繁琐和复杂的代码也不会难倒一些学者和专门从事开发的技术人员,好了下面进行代码举例说明。 C++ 代

本文主要进行对C++代码进行学习与说明,一旦掌握了一些编程的技巧和方式,在繁琐和复杂的代码也不会难倒一些学者和专门从事开发的技术人员,好了下面进行代码举例说明。

C++代码如下:

  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.     
  10. Copyright(c) 2006.5 - 2007.4  llbird wushaojian@21cn.com http://blog.csdn.net/wujian53  
  11.  
  12. Use:  
  13. 这是一个很简单的日志, 用的是C风格的函数,支持多线程  
  14. 只要包含本文件,并且把log.cpp文件添加到项目中就可以了  
  15. 在VC中你可能需要在log.cpp中添加#include "stdafx.h"  
  16. 具体使用  
  17.  InitLog();//初始化  
  18.  LOG("程序启动");  
  19.  LOG1("%s", str);  
  20.  DestroyLog();//可有可无  
  21.  
  22. 调试时输出可以定义 LOG_TO_STD 或者 LOG_TO_DEBUG  
  23. 对于C++ Builder  
  24. 可以使用  
  25. LOG(Exception *e or Exception &e);  
  26. 对于WIN32 API  
  27. LOG_LAST_ERROR();  
  28. 对于_com_error  
  29. LOG( _com_error &e);  
  30. */  
  31.  
  32. #include <stdio.h> 
  33. #include <time.h> 
  34. #include <windows.h> 
  35. #include <process.h> 
  36. //使用短的原文件名  
  37. #define LOG_SHORT_SOURCE_FILE  
  38. //使用日志  
  39. #define LOG_TO_FILE  
  40. //定义标准错误输出设备  
  41. #define LOG_STD_DEV stderr  
  42. //使用标准输出设备  
  43. //#define LOG_TO_STD  
  44. //向调试窗口输出  
  45. //#define LOG_TO_DEBUG  
  46. //输出messagebox  
  47. //#define LOG_TO_MESSAGE_BOX  
  48. //多线程用临界区  
  49. extern CRITICAL_SECTION _g_LogMutex;  
  50. //全局日志文件名  
  51. extern char _g_LogFileName[MAX_PATH];  
  52. extern void InitLog(); //>初始化日志  
  53. extern void DestroyLog();//>清除日志  
  54. extern BOOL Log(const char* src/*源程序名*/, int line/*行号*/, const char* description/*描述*/);//>新增日志  
  55.  //记录日志宏列表  
  56. #define LOG(arg) Log(__FILE__, __LINE__, (arg))  
  57. //多参数记录日志宏  
  58. #define LOG1(str, p1) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1)); LOG(buffer); }  
  59. #define LOG2(str, p1, p2) {LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2)); LOG(buffer); }  
  60. #define LOG3(str, p1, p2, p3) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2), (p3)); LOG(buffer); }  
  61. #define LOG4(str, p1, p2, p3, p4) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2), (p3), (p4));LOG(buffer);}  
  62. #define LOG5(str, p1, p2, p3, p4, p5) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2), (p3), (p4), (p5)); LOG(buffer);}  
  63. //记录windows API错误值  
  64. #define LOG_LAST_ERROR() { LOG_SPRINTF_BUFFER; DWORD eid = GetLastError();sprintf(buffer, "Last Error(%d):", eid); int len = strlen(buffer);       
  65.  FormatMessage(         
  66.   FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,  
  67.   NULL,  
  68.   eid,    
  69.   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   
  70.   buffer + len,  
  71.   DEFAULT_LOG_SPRINTF_BUFFER_SIZE-len-1,   
  72.   NULL   
  73.   );   
  74.   LOG(buffer);   
  75.   }  
  76.  
  77. #if defined(__cplusplus) && defined(_INC_COMDEF)  
  78. ///新增COM错误信息  
  79. inline BOOL Log(const char* src, int line, _com_error &e)  
  80. {  
  81.  char buffer[DEFAULT_LOG_SPRINTF_BUFFER_SIZE];  
  82.  sprintf(buffer, "_com_errortCode = %xtCode meaning = %stSource = %stDescription = %s",   
  83.   e.Error(), (LPCSTR)(_bstr_t)e.ErrorMessage(), (LPCSTR)(_bstr_t)e.Source(), (LPCSTR)(_bstr_t)e.Description());  
  84.  return LOG_POS(src, line, buffer);  
  85. }  
  86. #endif  
  87.  
  88. ///新增VCL异常信息  
  89. #if defined(__cplusplus) && defined(__BORLANDC__) && defined(INC_VCL)  
  90. inline BOOL Log(const char* src, int line, Exception *e)  
  91. {  
  92.  return LOG_POS(src, line, e->Message.c_str());  
  93. }  
  94. inline BOOL Log(const char* src, int line, Exception &e)  
  95. {  
  96.  return LOG_POS(src, line, e.Message.c_str());  
  97. }  
  98. #endif  
  99.  
  100. #endif _LOG_H_  

看了以上那么多的C++代码我相信大家已经有点迷糊了吧,那就好好消化一下吧。

精彩图集

赞助商链接