annotate Implab/Diagnostics/TextListenerBase.cs @ 45:d10034588e38 interactive logger

initial work on interactive logger
author cin
date Thu, 17 Apr 2014 03:05:53 +0400
parents fe33f4e02ad5
children 7c2369f580b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
1 using System;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
2 using System.Collections.Generic;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.Linq;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 using System.Text;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6 namespace Implab.Diagnostics {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7 public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
8
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
9 readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
10
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
11 protected TextListenerBase() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12 Register(this);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15 public void Subscribe(Type eventType) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 if (eventType == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
17 throw new ArgumentNullException("eventType");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
18 GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
20
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 public void Subscribe<TEvent>() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22 Subscribe<TEvent>(LogChannel<TEvent>.Default);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
24
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 public void Subscribe<TEvent>(LogChannel<TEvent> channel) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
26 if (channel == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
27 throw new ArgumentNullException("channel");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
28
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
29 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
30 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32 var formatter = GetService<IEventTextFormatter<TEvent>>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
33
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
34 EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35 WriteEntry((TraceContext)sender, formatter.Format((TraceContext)sender, args.Value));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 if (m_subscriptions.ContainsKey(channel))
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39 return;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 channel.Events += handler;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
43 Action unsubscribe = () => {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
44 channel.Events -= handler;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
45 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
46
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
47 m_subscriptions.Add(channel, unsubscribe);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
48 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
49 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
50
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
51 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
52 if (channel == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
53 throw new ArgumentNullException("channel");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
54
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56 Action subscription;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57 if (m_subscriptions.TryGetValue(channel, out subscription)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
58 subscription();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
59 m_subscriptions.Remove(channel);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
60 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
61 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
62 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
63
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
64 public void UnsubscribeAll() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
65 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
66 foreach (var subscription in m_subscriptions.Values)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
67 subscription();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
68 m_subscriptions.Clear();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
69 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
70 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
71
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
72 protected abstract void WriteEntry(TraceContext context, EventText text);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
73
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
74 public EventText Format(TraceContext context, object data) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
75 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
76 indent = context.CurrentOperation.Level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
77 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
78 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
79 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
80
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
81 public EventText Format(TraceContext context, TraceEvent data) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
82 var level = context.CurrentOperation.Level;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
83 if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
84 level--;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
85
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
86 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
87 indent = level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
88 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
89 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
92 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
93 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
94 UnsubscribeAll();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
95 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
96 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
97 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
98 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
99 }