# HG changeset patch # User cin # Date 2014-04-16 06:12:56 # Node ID 7c2369f580b8f4b4d556614d2c892bb836137823 # Parent 3ba6778ed3364554405562bae4c2ac3cd89c5b2f improved tracing, TextListenerBase can be bound to logical operation scope. diff --git a/Implab.v11.suo b/Implab.v11.suo deleted file mode 100644 index 2499e7be2058885bde9009de32d2756ec4238c87..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 GIT binary patch literal 0 Hc$@, IEventTextFormatter { readonly Dictionary m_subscriptions = new Dictionary(); + readonly LogicalOperation m_boundOperation; + readonly int m_baseIndent; - protected TextListenerBase() { + protected TextListenerBase(bool local) { Register(this); + if (local) { + m_boundOperation = TraceContext.Current.CurrentOperation; + m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); + } } public void Subscribe(Type eventType) { @@ -32,7 +38,12 @@ namespace Implab.Diagnostics { var formatter = GetService>(); EventHandler> handler = (sender, args) => { - WriteEntry((TraceContext)sender, formatter.Format((TraceContext)sender, args.Value)); + TraceContext context = (TraceContext)sender; + var text = formatter.Format(context, args.Value); + text.indent -= m_baseIndent; + + if (IsRelated(context.CurrentOperation)) + WriteEntry(context, text); }; if (m_subscriptions.ContainsKey(channel)) @@ -48,6 +59,15 @@ namespace Implab.Diagnostics { } } + public bool IsRelated(LogicalOperation op) { + if (m_boundOperation == null) + return true; + + while (op != m_boundOperation && op.Level > m_boundOperation.Level) + op = op.Parent; + return op == m_boundOperation; + } + public void Unsubscribe(LogChannel channel) { if (channel == null) throw new ArgumentNullException("channel"); diff --git a/Implab/Diagnostics/TraceContext.cs b/Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs +++ b/Implab/Diagnostics/TraceContext.cs @@ -92,6 +92,7 @@ namespace Implab.Diagnostics { try { action(); } finally { + _current.EndAllOperations(); _current = old; } } @@ -156,6 +157,14 @@ namespace Implab.Diagnostics { } } + /// + /// Заврешает все начатые в этом контексте операции + /// + public void EndAllOperations() { + while (m_bound != m_currentOperation) + EndLogicalOperation(); + } + void LogEvent(TraceEventType type, string format, params object[] args) { LogChannel.Default.LogEvent(this, TraceEvent.Create(type, format, args)); } diff --git a/Implab/Diagnostics/TraceEvent.cs b/Implab/Diagnostics/TraceEvent.cs --- a/Implab/Diagnostics/TraceEvent.cs +++ b/Implab/Diagnostics/TraceEvent.cs @@ -25,7 +25,7 @@ namespace Implab.Diagnostics { } public static TraceEvent Create(TraceEventType type, string format, params object[] args) { - return new TraceEvent(type, String.Format(format ?? String.Empty, args)); + return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args)); } } }