浏览代码

添加log

sat23 3 年之前
父节点
当前提交
bdea54712d

+ 54 - 2
StartAPPbyCOM/StartAPPbyCOM/Assist.cpp

@@ -1,5 +1,6 @@
 #include "Assist.h"
-
+#include <time.h>
+#include <locale.h>
 
 namespace Assist
 {
@@ -64,7 +65,7 @@ namespace Assist
 	{
 		if (!PathFileExists(szAppPath))
 		{
-			printf("Error, %ls path not exists!\n", szAppPath);
+			WriteTextLog(_T("Error, %ls path not exists!\n"), szAppPath);
 			return false;
 		}
 
@@ -85,6 +86,8 @@ namespace Assist
 			return true;
 		}
 
+		WriteTextLog(_T("启动进程失败:%d"), GetLastError());
+
 		return false;
 	}
 
@@ -318,4 +321,53 @@ namespace Assist
 
 		return hTagWnd;
 	}
+
+	void WriteTextLog(const TCHAR* format, ...)
+	{
+		// 获取今年年份;
+		__time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
+		struct tm gmtm = { 0 };
+		localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
+
+		// 解析出日志路径;
+		TCHAR szlogpath[MAX_PATH] = { 0 };
+		_stprintf_s(szlogpath, _T("%s\\%04d-%02d-%02d.txt"), g_szCurModuleDir, gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday);
+		// 打开或创建文件;
+		FILE* fp = NULL;
+		if (_taccess(szlogpath, 0) != -1)
+		{// 存在;
+			if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
+				// 移动到末尾;
+				fseek(fp, 0, SEEK_END);
+		}
+		else
+		{// 不存在;
+			_tfopen_s(&fp, szlogpath, _T("w+"));
+		}
+
+		if (fp == NULL)
+			return;
+
+		// 格式化前设置语言区域;
+		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
+		_tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
+
+		// 格式化日志内容;
+		va_list		args = NULL;
+		int			len = 0;
+		TCHAR* buffer = NULL;
+		va_start(args, format);
+		// _vscprintf doesn't count. terminating '\0'
+		len = _vsctprintf(format, args) + 1;
+		buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
+		_vstprintf_s(buffer, len, format, args);
+		// 将日志内容输入到文件中;
+		_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
+
+		// 关闭文件,释放资源并设置回原语言区域;
+		free(buffer);
+		fclose(fp);
+		_tsetlocale(LC_CTYPE, old_locale);
+		free(old_locale);//还原区域设定;
+	}
 };

+ 1 - 0
StartAPPbyCOM/StartAPPbyCOM/Assist.h

@@ -59,6 +59,7 @@ namespace Assist {
 
 	// 获取符合要求的窗口进程;
 	HWND GetWindowNameWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWindowName);
+	void WriteTextLog(const TCHAR* format, ...);
 };
 
 #endif // __ASSIST__

+ 16 - 6
StartAPPbyCOM/StartAPPbyCOM/StartAPPbyCOM.cpp

@@ -22,9 +22,7 @@
 int _tmain(int argc, TCHAR* argv[])
 {
     int ret = 0;
-    Sleep(30000);
-
-    CmdHelper<TCHAR> cmdHelper;
+     CmdHelper<TCHAR> cmdHelper;
     if ((ret = cmdHelper.initialize(argc, argv)) != 0)
     {
         printf("Initialize command failed! ret=%d\n", ret);
@@ -51,10 +49,13 @@ int _tmain(int argc, TCHAR* argv[])
     }
 
 #if 1
+    Assist::Init();
+    Assist::WriteTextLog(_T("进程启动"));
 	// 判断是否有飞书进程;
 	HWND hWnd = Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms"));
 	if (hWnd == NULL)
 	{
+        Assist::WriteTextLog(_T("飞书未启动"));
 		// 结束所有飞书进程;
 		Assist::CloseProcess(_T("FeishuRooms.exe"));
 #pragma warning(disable:28159)
@@ -63,18 +64,27 @@ int _tmain(int argc, TCHAR* argv[])
 		while (FindWindow(_T("Shell_TrayWnd"), NULL) == NULL)
 		{
 			Sleep(500);
-            if ( GetTickCount() - dwTickCount > 20000 )
+            Assist::WriteTextLog(_T("未找到系统任务栏"));
+            if (GetTickCount() - dwTickCount > 20000)
+            {
+                Assist::WriteTextLog(_T("未找到系统任务栏超过20秒"));
                 break;
+            }
 		}
 
+        Assist::WriteTextLog(_T("找到系统任务栏,开始启动飞书进程"));
 		// 启动飞书;
 		Assist::StartApp(tcsAppPath.c_str(), tcsAppArgs.c_str());
         dwTickCount = GetTickCount();
 		while (Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms")) == NULL)
 		{
 			Sleep(500);
-			if (GetTickCount() - dwTickCount > 10000)
-				break;
+            Assist::WriteTextLog(_T("飞书启动中……"));
+            if (GetTickCount() - dwTickCount > 10000)
+            {
+                Assist::WriteTextLog(_T("飞书启动超过10秒,默认启动失败"));
+                break;
+            }
 		}
 
 		// 发送串口消息;