StdAfx.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // stdafx.cpp : source file that includes just the standard includes
  2. // CPhotoFTPSend.pch will be the pre-compiled header
  3. // stdafx.obj will contain the pre-compiled type information
  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. static ThreadSection _critSection;
  23. AutoThreadSection aSection(&_critSection);
  24. // 解析出日志路径;
  25. static TCHAR szlogpath[MAX_PATH] = {0};
  26. if ( szlogpath[0] == _T('\0') )
  27. {
  28. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  29. TCHAR szDir[_MAX_DIR] = { 0 };
  30. TCHAR szFna[_MAX_DIR] = { 0 };
  31. TCHAR szExt[_MAX_DIR] = { 0 };
  32. TCHAR szModulePath[MAX_PATH] = {0};
  33. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  34. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  35. _tcscpy_s(szModulePath, szDrive);
  36. _tcscat_s(szModulePath, szDir);
  37. _stprintf_s(szlogpath, _T("%s\\ftp-client.txt"), szModulePath);
  38. }
  39. // 打开或创建文件;
  40. CStdioFile fp;
  41. if (PathFileExists(szlogpath))
  42. {
  43. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  44. {
  45. return;
  46. }
  47. ULONGLONG length = fp.GetLength();
  48. if (length > 10 * 1024 * 1024) // 10M;
  49. {
  50. fp.Close();
  51. ::DeleteFile(szlogpath);
  52. return;
  53. }
  54. fp.SeekToEnd();
  55. }
  56. else
  57. {
  58. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  59. return;
  60. }
  61. // 格式化前设置语言区域;
  62. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  63. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  64. // 格式化日志内容;
  65. va_list args = NULL;
  66. int len = 0;
  67. TCHAR *buffer = NULL;
  68. va_start( args, format );
  69. // _vscprintf doesn't count. terminating '\0'
  70. len = _vsctprintf( format, args );
  71. if ( len == -1 )
  72. {
  73. goto clear;
  74. }
  75. len++;
  76. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  77. _vstprintf_s( buffer, len, format, args ); // C4996
  78. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  79. // 将日志内容输入到文件中;
  80. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  81. fp.WriteString(buffer);
  82. fp.WriteString(_T("\n"));
  83. // 关闭文件,释放资源并设置回原语言区域;
  84. free( buffer );
  85. clear:
  86. _tsetlocale(LC_CTYPE, old_locale);
  87. free(old_locale);//还原区域设定;
  88. fp.Close();
  89. }
  90. catch (CException *e)
  91. {
  92. e->ReportError();
  93. e->Delete();
  94. }
  95. }