stdafx.cpp 2.4 KB

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