浏览代码

新增函数:WriteTextLogEx

scbc.sat2 5 年之前
父节点
当前提交
90a956fa60
共有 2 个文件被更改,包括 54 次插入0 次删除
  1. 53 0
      SATHelper/SATHelper/Global.cpp
  2. 1 0
      SATHelper/SATHelper/Global.h

+ 53 - 0
SATHelper/SATHelper/Global.cpp

@@ -223,6 +223,59 @@ namespace Global
 		free(old_locale);//还原区域设定;
 	}
 
+	void WriteTextLogEx(int nType, const TCHAR* format, ...)
+	{
+		// 解析出日志路径;
+		TCHAR szlogpath[MAX_PATH] = { 0 };
+		_stprintf_s(szlogpath, _T("%s%s-%d.txt"), g_szCurModuleDir, g_szFna, nType);
+		// 打开或创建文件;
+		FILE* fp = NULL;
+		//if (_taccess(szlogpath, 0) != -1)
+#ifndef UNICODE
+		if (_access(szlogpath, 0) != -1)
+#else
+		if (_taccess(szlogpath, 0) != -1)
+#endif
+		{// 存在;
+			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);
+		// 将日志内容输入到文件中;
+		// 获取今年年份;
+		__time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
+		struct tm gmtm = { 0 };
+		localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
+		_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, buffer);
+
+		// 关闭文件,释放资源并设置回原语言区域;
+		free(buffer);
+		fclose(fp);
+		_tsetlocale(LC_CTYPE, old_locale);
+		free(old_locale);//还原区域设定;
+	}
+
 	int ReadReg(char* path, char* key, char* value)
 	{
 		HKEY hKey;

+ 1 - 0
SATHelper/SATHelper/Global.h

@@ -79,6 +79,7 @@ namespace Global
 	BOOL Python27Dir();
 	BOOL GetSysSerialPort(std::vector<std::string>& vtports);
 	void WriteTextLog(const TCHAR* format, ...);
+	void WriteTextLogEx(int nType, const TCHAR* format, ...);
 	BOOL LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath);
 	BOOL LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen);
 	BOOL LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath);