diff --git a/Implab/Diagnostics/Extensions.cs b/Implab/Diagnostics/Extensions.cs --- a/Implab/Diagnostics/Extensions.cs +++ b/Implab/Diagnostics/Extensions.cs @@ -10,6 +10,17 @@ TraceContext.Instance.Leave(); }); } + + public static IPromise EndLogicalOperation(this IPromise promise) { + Safe.ArgumentNotNull(promise, "promise"); + var op = TraceContext.Instance.DetachLogicalOperation(); + + return promise.Anyway(() => { + TraceContext.Instance.EnterLogicalOperation(op,true); + TraceLog.EndLogicalOperation(); + TraceContext.Instance.Leave(); + }); + } } } diff --git a/Implab/Diagnostics/TraceContext.cs b/Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs +++ b/Implab/Diagnostics/TraceContext.cs @@ -41,9 +41,10 @@ namespace Implab.Diagnostics { } public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { - LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.Attach, String.Format("{0} -> [{1}]", operation.Name, m_threadId))); + var prev = CurrentOperation; m_stack.Push(m_current); m_current = new OperationContext(operation, takeOwnership); + LogChannel.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name))); } public void StartLogicalOperation(string name) { @@ -61,15 +62,17 @@ namespace Implab.Diagnostics { } public LogicalOperation DetachLogicalOperation() { - var op = m_current.DetachLogicalOperation(); - LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("[{0}] -> {1}", m_threadId, op.Name))); - return op; + var prev = m_current.DetachLogicalOperation(); + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name))); + return prev; } public void Leave() { if (m_stack.Count > 0) { m_current.Leave(); + var prev = CurrentOperation; m_current = m_stack.Pop(); + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name))); } else { TraceLog.TraceWarning("Attemtp to leave the last operation context"); m_current = OperationContext.EMPTY; diff --git a/Implab/Diagnostics/TraceEventType.cs b/Implab/Diagnostics/TraceEventType.cs --- a/Implab/Diagnostics/TraceEventType.cs +++ b/Implab/Diagnostics/TraceEventType.cs @@ -13,5 +13,7 @@ namespace Implab.Diagnostics { OperationCompleted, Attach, Detach, + Enter, + Leave } } diff --git a/Implab/IPromiseT.cs b/Implab/IPromiseT.cs --- a/Implab/IPromiseT.cs +++ b/Implab/IPromiseT.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Implab { public interface IPromise : IPromise { diff --git a/Implab/Safe.cs b/Implab/Safe.cs --- a/Implab/Safe.cs +++ b/Implab/Safe.cs @@ -44,7 +44,7 @@ namespace Implab } [DebuggerStepThrough] - public static IPromise GuargPromise(Func action) { + public static IPromise InvokePromise(Func action) { ArgumentNotNull(action, "action"); var p = new Promise(); @@ -58,11 +58,11 @@ namespace Implab } [DebuggerStepThrough] - public static IPromise GuardPromise(Func> action) { + public static IPromise InvokePromise(Func> action) { ArgumentNotNull(action, "action"); try { - return action(); + return action() ?? Promise.ExceptionToPromise(new Exception("The action returned null")); } catch (Exception err) { return Promise.ExceptionToPromise(err); } diff --git a/MonoPlay/Program.cs b/MonoPlay/Program.cs --- a/MonoPlay/Program.cs +++ b/MonoPlay/Program.cs @@ -6,6 +6,9 @@ using Implab; namespace MonoPlay { class MainClass { public static void Main(string[] args) { + if (args == null) + throw new ArgumentNullException("args"); + var listener = new ConsoleTraceListener(true); listener.Subscribe(); @@ -13,12 +16,10 @@ namespace MonoPlay { TraceLog.StartLogicalOperation("program"); - Console.WriteLine("Hello World!"); - TraceLog.StartLogicalOperation("async"); AsyncPool.Invoke(() => { TraceLog.TraceInformation("Hello async"); - TraceLog.StartLogicalOperation(); + TraceLog.StartLogicalOperation("foo"); return 0; }) .EndLogicalOperation()