LogModule.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // LogModule.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "LogModule.h"
  5. #include <Shlwapi.h>
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // 唯一的应用程序对象
  10. CWinApp theApp;
  11. using namespace std;
  12. CIOCPModel g_IOCP; // 主要对象,完成端口模型
  13. // 启动tcp服务;
  14. BOOL StartServer(unsigned int port)
  15. {
  16. // 1.加载配置文件;
  17. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  18. TCHAR szDir[_MAX_DIR] = { 0 };
  19. TCHAR szExt[_MAX_DIR] = { 0 };
  20. ::GetModuleFileName(NULL, Global::g_szCurModulePath, sizeof(Global::g_szCurModulePath) / sizeof(TCHAR));
  21. _tsplitpath_s(Global::g_szCurModulePath, szDrive, szDir, Global::g_szFna, szExt);
  22. _tcscpy_s(Global::g_szCurModuleDir, szDrive);
  23. _tcscat_s(Global::g_szCurModuleDir, szDir);
  24. // 初始化网络库;
  25. if ( !g_IOCP.LoadSocketLib() )
  26. {
  27. return FALSE;
  28. }
  29. Global::WriteTextLog(_T("初始化网络库成功"));
  30. // 开启tcp服务;
  31. if ( !g_IOCP.Start(port) )
  32. {
  33. return FALSE;
  34. }
  35. Global::g_time = time(NULL);
  36. Global::g_lastTime = COleDateTime::GetCurrentTime();
  37. Global::WriteTextLog(_T("开启TCP服务成功"));
  38. if ( PathFileExists(Global::g_szConfig) )
  39. {
  40. // runner_tcp_port
  41. TCHAR szTcpPort[10] = {0};
  42. _itoa_s(port, szTcpPort, 10);
  43. Global::WriteTextLog(_T("配置文件路径:%s"), Global::g_szConfig);
  44. for ( int i = 0; i < 10; i++ )
  45. {
  46. if (!WritePrivateProfileString(_T("COMM"), _T("runner_tcp_port"), szTcpPort, Global::g_szConfig))
  47. Global::WriteTextLog(_T("保存端口号:%d 失败; 原因:%d"), port, GetLastError());
  48. else
  49. {
  50. Global::WriteTextLog(_T("保存端口号:%d 成功;"), port);
  51. break;
  52. }
  53. }
  54. }
  55. return TRUE;
  56. }
  57. // 停止tcp服务;
  58. void StopServer()
  59. {
  60. g_IOCP.Stop();
  61. }
  62. // 是否写入日志文件;
  63. void EnableWriteLog(bool bEnable)
  64. {
  65. Global::g_bEnableLog = bEnable;
  66. if ( !bEnable )
  67. Global::ClosePythonLog();
  68. Global::WriteTextLog(_T("当前日志路径:%s; 状态:%s"), Global::g_szLogPath, bEnable ? _T("启用日志") : _T("禁用日志") );
  69. }
  70. void SetCaselogPath(LPCTSTR lpCaselogPath)
  71. {
  72. if ( lpCaselogPath == NULL || lpCaselogPath[0] == '\0')
  73. return;
  74. Global::WriteTextLog(_T("设置日志路径:%s"), lpCaselogPath);
  75. _stprintf_s(Global::g_szLogPath, _T("%s"), lpCaselogPath);
  76. Global::WritePythonLog(_T("C++ Operation"));
  77. }
  78. void SetConfigPath(LPCTSTR lpConfigPath)
  79. {
  80. if ( lpConfigPath == NULL || lpConfigPath[0] == '\0')
  81. return;
  82. _stprintf_s(Global::g_szConfig, _T("%s"), lpConfigPath);
  83. }
  84. void SetCaselogPath3(const char* buffer, const int& len)
  85. {
  86. if ( buffer == NULL || buffer[0] == '\0')
  87. return;
  88. Global::WriteTextLog(_T("路径3:%s, len=%d"), (char*)buffer, len);
  89. _tcscpy_s(Global::g_szLogPath, (char*)buffer);
  90. }
  91. LPCTSTR GetCaselogPath()
  92. {
  93. return Global::g_szLogPath;
  94. }
  95. __int64 GetReceivePrintTime()
  96. {
  97. return Global::g_time;
  98. }
  99. void SetReceivePrintTime(__int64 dt)
  100. {
  101. Global::g_time = dt;
  102. }
  103. void ClosePythonLog()
  104. {
  105. Global::ClosePythonLog();
  106. }