Kaynağa Gözat

improve log logic in report app

gsp 3 yıl önce
ebeveyn
işleme
80c8578289

+ 24 - 3
OTSCPP/OTSLog/COTSUtilityDllFunExport.cpp

@@ -63,7 +63,25 @@ void  InitLogFile(LPCTSTR lpLogName)
 
 	
 }
+void  LogToFile(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
+{
+	if (NULL == g_LpLogFile)
+	{
+		InitLogFile(g_csLogName);
+	}
+	std::string strLog;
+	va_list arglist;
+	va_start(arglist, lpTraceLog);
+	strLog = getFormattedStr(lpTraceLog, arglist);
+	va_end(arglist);
+
 
+	LPCTSTR szFileName = ::PathFindFileName(szFile);
+
+
+
+	g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
+}
 void  LogTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
 {
 	/*if (NULL == g_LpLogFile)
@@ -182,7 +200,12 @@ bool SendLogMessageToNlog(LPCTSTR Msg, int postLogLevel)
 		       memset(s, 0, 200);
 		       GetWindowText(hd, s, 200);
 			   CString winTxt(s);
-			   if (winTxt.Find(_T("OTSMeasureApp"))>-1)
+			   if (winTxt.Find(_T("OTSMeasureApp"))>-1)//find the measure app mainform
+			   {
+				   m_hWnd = hd;
+				   break;
+			   }
+			   if (winTxt.Find(_T("OTSReportApp")) > -1)//find the report app mainform
 			   {
 				   m_hWnd = hd;
 				   break;
@@ -193,8 +216,6 @@ bool SendLogMessageToNlog(LPCTSTR Msg, int postLogLevel)
 	}
 
 
-	//HWND m_hWnd = ::FindWindow(NULL, _T("ÑùÆ·²âÁ¿³ÌÐò(Inclusion)"));
-
 	if (NULL == m_hWnd)
 	{
 		

+ 1 - 0
OTSCPP/OTSLog/COTSUtilityDllFunExport.h

@@ -19,6 +19,7 @@
 };
 
 extern "C" __declspec(dllexport) void InitLogFile(LPCTSTR lpLogName);     // 由APP加载DLL成功后设置路径,设置Log文件保存的路径,如 "C:\\OPTON\\APP"      (APP为保存的log名字
+extern "C" __declspec(dllexport) void  LogToFile(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...);
 extern "C" __declspec(dllexport) void LogTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...);       //记录日志
 extern "C" __declspec(dllexport) void LogInfoTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...);       //记录日志
 extern "C" __declspec(dllexport) void LogErrorTrace(LPCTSTR szFile, long lLine, LPCTSTR lpErrorTrace, ...);   // 记录错误或者异常

+ 1 - 2
OTSIncAMeasureApp/OTSMeasureOutputNlog.cs

@@ -26,8 +26,7 @@ namespace OTSMeasureApp
         private PostLogMsg m_LogMsg;
         public const int MsgID = 0x0464;
         public const int LogMsgID = 0x0465;
-        //public List<STMrsSampleRetThreadMsg> m_MSTMsg = new List<STMrsSampleRetThreadMsg>();
-        //private STMrsSampleRetThreadMsg MSTMsg;
+    
 
         public OTSMeasureOutputNlog()
         {

+ 59 - 0
OTSIncAReportApp/1-UI/frmReportApp.cs

@@ -16,10 +16,17 @@ using System.Data;
 using System.Diagnostics;
 using System.Drawing;
 using System.IO;
+using System.Runtime.InteropServices;
 using System.Windows.Forms;
 
 namespace OTSIncAReportApp
 {
+    public struct PostLogMsg
+    {
+        public int logLevel;//1 trace 2 debug 3info 4 warn 5 error 6 fatal
+        [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)200)]
+        public char[] logMessage;
+    };
     /// <summary>
     /// 报告主窗体类
     /// </summary>
@@ -38,6 +45,11 @@ namespace OTSIncAReportApp
 
         #region 报告类结构相关------------------------------------------------------------------------------------
         NLog.Logger log;
+
+        private PostLogMsg m_LogMsg;
+        public const int MsgID = 0x0464;
+        public const int LogMsgID = 0x0465;
+
         public OutputNlog m_OutputNlog;                                                     //日志类对象
         public ResultDataMgr m_rstDataMgr = null;                                              //DataMgrFun 对象, 此类是和调用C++ DataMGR交互数据
         public OTSRibbonFun m_RibbonFun = null;                                             //报告菜单相关操作类
@@ -1068,6 +1080,53 @@ namespace OTSIncAReportApp
             Help help = new Help();
             help.ShowDialog();
         }
+        protected override void DefWndProc(ref Message m)
+        {
+            switch (m.Msg)
+            {
+                
+                case LogMsgID:
+                    m_LogMsg = new PostLogMsg();
+
+                    m_LogMsg = (PostLogMsg)Marshal.PtrToStructure(m.LParam, typeof(PostLogMsg));
+                    var log = NLog.LogManager.GetCurrentClassLogger();
+                    string s = GetString(m_LogMsg.logMessage);
+                    switch (m_LogMsg.logLevel)
+                    {
+                        case 1:
+                            log.Trace(s);
+                            break;
+                        case 2:
+                            log.Debug(s);
+                            break;
+                        case 3:
+                            log.Info(s);
+                            break;
+                        case 4:
+                            log.Warn(s);
+                            break;
+                        case 5:
+                            log.Error(s);
+                            break;
+                        case 6:
+                            log.Fatal(s);
+                            break;
+                    }
+
+                    break;
+                default:
+                    base.DefWndProc(ref m);
+                    break;
+            }
+        }
+        private string GetString(char[] csStr)
+        {
+            int ilen = csStr.Length;
+            string csName = new string(csStr);                //MSTMsg.STMSampleStu.cSName
+            csName.IndexOf('\0');
+            csName = csName.Substring(0, csName.IndexOf('\0'));
+            return csName;
+        }
     }
 }