Global.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. CLock g_loglock;
  9. TCHAR g_szDomain[64] = {0};
  10. TCHAR g_szModuleFilePath[MAX_PATH + 1] = {0};
  11. BOOL lyfzCopyFileEx(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, const BOOL &bFailIfExists)
  12. {
  13. // 1.如果是覆盖选项;
  14. if (!bFailIfExists)
  15. return CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists);
  16. // 2.如果不是覆盖选项;
  17. TCHAR szNewFileName[1024] = { 0 };
  18. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  19. TCHAR szDir[_MAX_DIR] = { 0 };
  20. TCHAR szFna[_MAX_FNAME] = { 0 };
  21. TCHAR szExt[_MAX_EXT] = { 0 };
  22. #ifdef _UNICODE
  23. wsprintf(szNewFileName, _T("%s"), lpNewFileName);
  24. #else
  25. sprintf(szNewFileName, _T("%s"), lpNewFileName);
  26. #endif //#ifdef _UNICODE
  27. _tsplitpath(szNewFileName, szDrive, szDir, szFna, szExt);
  28. int nIndex = 1;
  29. while (PathFileExists(szNewFileName))
  30. {
  31. #ifdef _UNICODE
  32. wsprintf(szNewFileName, _T("%s%s%s (%d)%s"), szDrive, szDir, szFna, nIndex++, szExt);
  33. #else
  34. sprintf(szNewFileName, _T("%s%s%s (%d)%s"), szDrive, szDir, szFna, nIndex++, szExt);
  35. #endif //#ifdef _UNICODE
  36. }
  37. return CopyFile(lpExistingFileName, szNewFileName, bFailIfExists);
  38. }
  39. void WriteLogin(CString str) // 线程安全问题,如果同时有两个线程同时访问,可能存在线程安全问题;
  40. {
  41. CAutoLock autolock(&g_loglock);
  42. try
  43. {
  44. CStdioFile fp;
  45. CString path = g_szModuleFilePath;
  46. path += _T("检测备份日志.txt");
  47. if (::PathFileExists(path))
  48. {
  49. #ifndef _UNICODE
  50. fp.Open(path, CFile::modeWrite);
  51. #else
  52. fp.Open(path, CFile::modeWrite | CFile::typeBinary | CFile::typeText);
  53. #endif
  54. int length = fp.GetLength();
  55. if (length > 1024 * 1024)
  56. {
  57. fp.Close();
  58. ::DeleteFile(path); // 删除检测备份日志.txt
  59. return;
  60. }
  61. fp.SeekToEnd();
  62. }
  63. else
  64. {
  65. #ifndef _UNICODE
  66. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  67. #else
  68. fp.Open(path, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
  69. #endif //#ifndef _UNICODE
  70. }
  71. CString strMsg = _T("");
  72. strMsg.Format(_T("%s%s\n"), CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")), str);
  73. fp.WriteString(strMsg);
  74. fp.Close();
  75. }
  76. catch (...)
  77. {
  78. }
  79. }