diff Implab.Diagnostics.Interactive/InteractiveListener.cs @ 48:d9d794b61bb9 interactive logger

Interactive tracing Improved working with tracing contexts
author cin
date Fri, 18 Apr 2014 12:34:45 +0400
parents b181f7bcb259
children 790e8a997d30
line wrap: on
line diff
--- a/Implab.Diagnostics.Interactive/InteractiveListener.cs	Thu Apr 17 18:49:36 2014 +0400
+++ b/Implab.Diagnostics.Interactive/InteractiveListener.cs	Fri Apr 18 12:34:45 2014 +0400
@@ -9,15 +9,14 @@
 
 namespace Implab.Diagnostics.Interactive
 {
-    public class InteractiveListener: Disposable
+    public class InteractiveListener: TextListenerBase
     {
         TraceForm m_form;
         
         SynchronizationContext m_syncGuiThread;
+        readonly Promise<object> m_guiStarted = new Promise<object>();
         
         readonly IPromiseBase m_guiFinished;
-        readonly Promise<object> m_guiStarted = new Promise<object>();
-
         readonly IPromiseBase m_workerFinished = new Promise<object>();
         
         readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
@@ -30,17 +29,19 @@
         bool m_paused;
         readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
 
-        public InteractiveListener() {
-            m_guiFinished = AsyncPool.InvokeNewThread(() => {
-                GuiThread();
-                return 0;
-            });
+        public InteractiveListener(bool global) : base(global) {
+            m_guiFinished = AsyncPool.InvokeNewThread(GuiThread);
+            m_workerFinished = AsyncPool.InvokeNewThread(QueueThread);
 
             m_guiStarted.Join();
         }
 
         void GuiThread() {
             m_form = new TraceForm(); // will create SynchronizationContext
+
+            m_form.PauseEvents += (s,a) => Pause();
+            m_form.ResumeEvents += (s, a) => Resume();
+
             m_syncGuiThread = SynchronizationContext.Current;
             m_guiStarted.Resolve();
             Application.Run();
@@ -93,6 +94,8 @@
         }
 
         void Terminate() {
+            m_exitPending = true;
+            Resume();            
             m_syncGuiThread.Post(x => Application.ExitThread(), null);
         }
 
@@ -103,5 +106,17 @@
             }
             base.Dispose(disposing);
         }
+
+        protected override void WriteEntry(TraceContext context, EventText text, string channel) {
+            var item = new TraceViewItem {
+                Indent = text.indent,
+                Message = text.content,
+                Thread = context.ThreadId,
+                Channel = channel,
+                Timestamp = Environment.TickCount
+            };
+
+            Enqueue(item);
+        }
     }
 }