TextListenerBase.cs
119 lines
| 4.0 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r40 | using System; | ||
using System.Collections.Generic; | ||||
using System.Linq; | ||||
using System.Text; | ||||
namespace Implab.Diagnostics { | ||||
public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> { | ||||
readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>(); | ||||
cin
|
r43 | readonly LogicalOperation m_boundOperation; | ||
readonly int m_baseIndent; | ||||
cin
|
r40 | |||
cin
|
r43 | protected TextListenerBase(bool local) { | ||
cin
|
r40 | Register(this); | ||
cin
|
r43 | if (local) { | ||
m_boundOperation = TraceContext.Current.CurrentOperation; | ||||
m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); | ||||
} | ||||
cin
|
r40 | } | ||
public void Subscribe(Type eventType) { | ||||
if (eventType == null) | ||||
throw new ArgumentNullException("eventType"); | ||||
GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null); | ||||
} | ||||
public void Subscribe<TEvent>() { | ||||
Subscribe<TEvent>(LogChannel<TEvent>.Default); | ||||
} | ||||
public void Subscribe<TEvent>(LogChannel<TEvent> channel) { | ||||
if (channel == null) | ||||
throw new ArgumentNullException("channel"); | ||||
lock (m_subscriptions) { | ||||
AssertNotDisposed(); | ||||
var formatter = GetService<IEventTextFormatter<TEvent>>(); | ||||
EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => { | ||||
cin
|
r43 | TraceContext context = (TraceContext)sender; | ||
var text = formatter.Format(context, args.Value); | ||||
text.indent -= m_baseIndent; | ||||
if (IsRelated(context.CurrentOperation)) | ||||
WriteEntry(context, text); | ||||
cin
|
r40 | }; | ||
if (m_subscriptions.ContainsKey(channel)) | ||||
return; | ||||
channel.Events += handler; | ||||
Action unsubscribe = () => { | ||||
channel.Events -= handler; | ||||
}; | ||||
m_subscriptions.Add(channel, unsubscribe); | ||||
} | ||||
} | ||||
cin
|
r43 | 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; | ||||
} | ||||
cin
|
r40 | public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) { | ||
if (channel == null) | ||||
throw new ArgumentNullException("channel"); | ||||
lock (m_subscriptions) { | ||||
Action subscription; | ||||
if (m_subscriptions.TryGetValue(channel, out subscription)) { | ||||
subscription(); | ||||
m_subscriptions.Remove(channel); | ||||
} | ||||
} | ||||
} | ||||
public void UnsubscribeAll() { | ||||
lock (m_subscriptions) { | ||||
foreach (var subscription in m_subscriptions.Values) | ||||
subscription(); | ||||
m_subscriptions.Clear(); | ||||
} | ||||
} | ||||
protected abstract void WriteEntry(TraceContext context, EventText text); | ||||
public EventText Format(TraceContext context, object data) { | ||||
return new EventText { | ||||
indent = context.CurrentOperation.Level, | ||||
content = data.ToString() | ||||
}; | ||||
} | ||||
public EventText Format(TraceContext context, TraceEvent data) { | ||||
var level = context.CurrentOperation.Level; | ||||
if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) | ||||
level--; | ||||
return new EventText { | ||||
indent = level, | ||||
content = data.ToString() | ||||
}; | ||||
} | ||||
protected override void Dispose(bool disposing) { | ||||
if (disposing) { | ||||
UnsubscribeAll(); | ||||
} | ||||
base.Dispose(disposing); | ||||
} | ||||
} | ||||
} | ||||