stdafx.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // WxAdoInterface.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. // TODO: 在 STDAFX.H 中
  6. // 引用任何所需的附加头文件,而不是在此文件中引用
  7. /************************************************************************/
  8. /* 函数:WriteTextLog[7/28/2016 IT];
  9. /* 描述:写文本日志;
  10. /* 参数:;
  11. /* [IN] :;
  12. /* 返回:void;
  13. /* 注意:;
  14. /* 示例:;
  15. /*
  16. /* 修改:;
  17. /* 日期:;
  18. /* 内容:;
  19. /************************************************************************/
  20. void WriteTextLog(const TCHAR *format, ...)
  21. {
  22. try
  23. {
  24. static ThreadSection _critSection;
  25. AutoThreadSection aSection(&_critSection);
  26. // 解析出日志路径;
  27. TCHAR szlogpath[MAX_PATH] = { 0 };
  28. static TCHAR szModulePath[MAX_PATH] = { 0 };
  29. static TCHAR szFna[_MAX_DIR] = { 0 };
  30. if (szModulePath[0] == _T('\0'))
  31. {
  32. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  33. TCHAR szDir[_MAX_DIR] = { 0 };
  34. TCHAR szExt[_MAX_DIR] = { 0 };
  35. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  36. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  37. _tcscpy_s(szModulePath, szDrive);
  38. _tcscat_s(szModulePath, szDir);
  39. }
  40. _stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
  41. // 打开或创建文件;
  42. CStdioFile fp;
  43. if (PathFileExists(szlogpath))
  44. {
  45. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  46. {
  47. return;
  48. }
  49. fp.SeekToEnd();
  50. }
  51. else
  52. {
  53. if (!fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite))
  54. return;
  55. }
  56. // 格式化前设置语言区域;
  57. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  58. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  59. // 格式化日志内容;
  60. va_list args = NULL;
  61. int len = 0;
  62. TCHAR *buffer = NULL;
  63. va_start(args, format);
  64. // _vscprintf doesn't count. terminating '\0'
  65. len = _vsctprintf_p(format, args);
  66. if (len == -1)
  67. {
  68. goto clear;
  69. }
  70. len++;
  71. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  72. _vstprintf_s(buffer, len, format, args); // C4996
  73. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  74. // 将日志内容输入到文件中;
  75. fp.WriteString(CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")));
  76. fp.WriteString(buffer);
  77. fp.WriteString(_T("\n"));
  78. // 关闭文件,释放资源并设置回原语言区域;
  79. free(buffer);
  80. clear:
  81. _tsetlocale(LC_CTYPE, old_locale);
  82. free(old_locale);//还原区域设定;
  83. fp.Close();
  84. }
  85. catch (CException *e)
  86. {
  87. e->ReportError();
  88. e->Delete();
  89. }
  90. }