}//end constructor. // // // // ************************************************************* // **** Initialize() **** // ************************************************************* /// <summary> /// Called by main GUI Thread via a constructor. Allows creation of form, if desired. /// </summary> private void Initialize() { // Create a log viewer form. if (m_LogViewer != null) { m_LogViewer.Show(); if (!m_IsViewActive) { m_LogViewer.WindowState = System.Windows.Forms.FormWindowState.Minimized; } } // ViewCreator lock (m_ViewCreatorLock) { if (m_ViewCreator == null) { m_ViewCreator = new LogViewCreator(); // must produce on UI thread. m_ViewCreator.Show(); m_ViewCreator.Hide(); } } // initialize our queues m_ThreadPriority = ThreadPriority.Lowest; // Reset Hub thread to lowest priority. m_OutQueue = new Queue <string>(m_QueueSize); m_EventStorage = new Queue <LogEventArgs>(m_EventStorageSize); for (int i = 0; i < m_EventStorageSize; ++i) { m_EventStorage.Enqueue(new LogEventArgs()); } // Start our clock. string clockOffsetMessage; TimeSpan offset; Misty.Lib.Utilities.NistServices nist = null; bool isConnectToNIST = false; try { nist = Misty.Lib.Utilities.NistServices.GetInstance(isConnectToNIST); } catch (Exception e) { Console.WriteLine("LogHub: " + e.Message); } if (nist != null && nist.IsGood) { offset = nist.SystemTimeOffset; clockOffsetMessage = String.Format("Using NIST-corrected system time, with offset = {0} for start time.", offset.ToString()); } else { offset = new TimeSpan(0); clockOffsetMessage = String.Format("Using local system time for start time.", offset.ToString()); } m_StopWatch.Start(); m_StartTime = DateTime.Now.Add(offset); // Send a starting message to log. string msg = string.Format("{0} started at {2} on {1}. ", LogName, m_StartTime.ToLongDateString(), m_StartTime.ToLongTimeString()); HubEventEnqueue(GetNewLogEvent(LogRequest.NewMessage, msg, LogLevel.Major, m_StopWatch.Elapsed)); HubEventEnqueue(GetNewLogEvent(LogRequest.NewMessage, clockOffsetMessage, LogLevel.Major, m_StopWatch.Elapsed)); // Send nist messages. string[] nistMsgs = nist.Messages; foreach (string msg2 in nistMsgs) { HubEventEnqueue(GetNewLogEvent(LogRequest.NewMessage, msg2, LogLevel.Major, m_StopWatch.Elapsed)); } base.Start(); // starts thread. }//end Initialize().
}// CreateLogViewer() // // // #endregion//private methods #region HubBase overrides // ********************************************************* // **** HubEvent Handler **** // ********************************************************* /// <summary> /// This method is called by the LogHub thread to process a new log message /// or a special log request (such as "flush buffers" etc). /// </summary> /// <param name="e"></param> protected override void HubEventHandler(EventArgs[] eventArgArray) { if (eventArgArray == null) { return; } // this can happen as the thread is dying. See HubBase.WaitListen() foreach (EventArgs eventArg in eventArgArray) { if ((eventArg != null) && (eventArg != EventArgs.Empty)) { LogEventArgs logArg = (LogEventArgs)eventArg; switch (logArg.Request) { case LogRequest.FlushNow: FlushOutQueue(); break; case LogRequest.Stop: //ProcessStopRequest(); string msg = string.Format("{0} stopping.", LogName); LogEventArgs eventArgs = GetNewLogEvent(LogRequest.NewMessage, msg, LogLevel.Major, m_StopWatch.Elapsed); m_OutQueue.Enqueue(BuildMessage(eventArgs)); FlushOutQueue(); m_StopWatch.Stop(); if (m_LogViewer != null) { m_LogViewer.LogClose(this, null); } LogHub.m_ViewCreator = null; // disconnect myself. // Close bool needToCloseErrorView = false; lock (m_ErrorViewLock) { if (m_ErrorView != null) { needToCloseErrorView = true; } } if (needToCloseErrorView) { m_ErrorView.LogClose(this, EventArgs.Empty); } base.Stop(); break; case LogRequest.NewMessage: ProcessOutMessage(logArg); break; case LogRequest.CopyTo: FlushOutQueue(); string targetPath = logArg.Message; if (!targetPath.EndsWith("\\")) { targetPath += "\\"; } CopyFileToTarget(targetPath); break; default: break; }//end switch. lock (m_EventStorageLock) { m_EventStorage.Enqueue(logArg); // push back into the storage area. } } } //next eventArg. } //end HubEventHander().