소스 검색

修改WritePythonLog时实打开句柄;

scbc.sat2 5 년 전
부모
커밋
ec57abffc1
1개의 변경된 파일30개의 추가작업 그리고 24개의 파일을 삭제
  1. 30 24
      LogModule/LogModule/Global.cpp

+ 30 - 24
LogModule/LogModule/Global.cpp

@@ -86,7 +86,7 @@ namespace Global
 
 		// 解析出日志路径;
 		TCHAR szlogpath[MAX_PATH] = { 0 };
-		_stprintf_s(szlogpath, _T("%slog %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
+		_stprintf_s(szlogpath, _T("%srunner-log %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
 		// 打开或创建文件;
 		FILE* fp = NULL;
 		//MKDIR(g_szLogPath);
@@ -142,41 +142,47 @@ namespace Global
 
 		int nErr = -1;
 		// 打开或创建文件;
-		static TCHAR szLastLog[MAX_PATH] = {0};
-		if ( _tcsicmp(szLastLog, g_szLogPath) != 0 )
-		{
-			MKDIR(g_szLogPath);
-			_tcscpy_s(szLastLog, g_szLogPath);
-			if ( gp_log_fp != NULL )
-			{	
-				WriteTextLog(_T("关闭文件:%s"), g_szLogPath);
-				fclose(gp_log_fp);
-				gp_log_fp = NULL;
+		FILE* fp = NULL;
+		
+#ifndef UNICODE
+		if (_access(g_szLogPath, 0) != -1)
+#else
+		if (_taccess(g_szLogPath, 0) != -1)
+#endif
+		{ // 存在;
+			if (0 == _tfopen_s(&fp, g_szLogPath, _T("a+")))
+			{
+				// 移动到末尾;
+				fseek(fp, 0, SEEK_END);
 			}
-
-			nErr = _tfopen_s(&gp_log_fp, g_szLogPath, "w+");
-			if ( gp_log_fp == NULL )
+			else
 			{
-				WriteTextLog(_T("创建文件失败:%s,%d,%d"), g_szLogPath, nErr, GetLastError());
+				WriteTextLog(_T("打开文件失败:%s,%d"), g_szLogPath, GetLastError());
+				return;
+			}
+		}
+		else
+		{ // 不存在;
+			MKDIR(g_szLogPath);
+			if (0 !=  _tfopen_s(&fp, g_szLogPath, _T("w+")) )
+			{
+				WriteTextLog(_T("创建文件失败:%s,%d"), g_szLogPath, GetLastError());
 				return;
 			}
-			WriteTextLog(_T("创建文件成功:%s"), g_szLogPath);
 		}
 
-		if (gp_log_fp == NULL )
+		if (fp == NULL)
 		{
-			WriteTextLog(_T("追加文件失败:%s,%d,%d"), g_szLogPath, nErr, GetLastError());
+			WriteTextLog(_T("文件句柄空:%s,%d"), g_szLogPath, GetLastError());
 			return;
 		}
-		
-		fseek(gp_log_fp, 0, SEEK_END);
+
 		// 格式化前设置语言区域;
 		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
 		_tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
-
-		_ftprintf(gp_log_fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, msg);
-
-		fflush(gp_log_fp);
+		_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, msg);
+		// 关闭文件,释放资源并设置回原语言区域;
+		fclose(fp);
 		_tsetlocale(LC_CTYPE, old_locale);
 		free(old_locale); //还原区域设定;
 	}