Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/TextListenerBase.cs @ 65:653c4e04968b
minor changes
author | cin |
---|---|
date | Mon, 30 Jun 2014 13:55:22 +0400 |
parents | d9d794b61bb9 |
children | 4c0e5ef99986 |
rev | line source |
---|---|
40 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
5 | |
6 namespace Implab.Diagnostics { | |
7 public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> { | |
8 | |
9 readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>(); | |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
10 readonly LogicalOperation m_boundOperation; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
11 readonly int m_baseIndent; |
40 | 12 |
48 | 13 protected TextListenerBase(bool global) { |
40 | 14 Register(this); |
48 | 15 if (!global) { |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
16 m_boundOperation = TraceContext.Current.CurrentOperation; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
17 m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
18 } |
40 | 19 } |
20 | |
21 public void Subscribe(Type eventType) { | |
22 if (eventType == null) | |
23 throw new ArgumentNullException("eventType"); | |
24 GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null); | |
25 } | |
26 | |
27 public void Subscribe<TEvent>() { | |
28 Subscribe<TEvent>(LogChannel<TEvent>.Default); | |
29 } | |
30 | |
31 public void Subscribe<TEvent>(LogChannel<TEvent> channel) { | |
32 if (channel == null) | |
33 throw new ArgumentNullException("channel"); | |
34 | |
35 lock (m_subscriptions) { | |
36 AssertNotDisposed(); | |
37 | |
38 var formatter = GetService<IEventTextFormatter<TEvent>>(); | |
48 | 39 var channelName = channel.Name; |
40 | 40 |
41 EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => { | |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
42 TraceContext context = (TraceContext)sender; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
43 var text = formatter.Format(context, args.Value); |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
44 text.indent -= m_baseIndent; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
45 |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
46 if (IsRelated(context.CurrentOperation)) |
48 | 47 WriteEntry(context, text, channelName); |
40 | 48 }; |
49 | |
50 if (m_subscriptions.ContainsKey(channel)) | |
51 return; | |
52 | |
53 channel.Events += handler; | |
54 | |
55 Action unsubscribe = () => { | |
56 channel.Events -= handler; | |
57 }; | |
58 | |
59 m_subscriptions.Add(channel, unsubscribe); | |
60 } | |
61 } | |
62 | |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
63 public bool IsRelated(LogicalOperation op) { |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
64 if (m_boundOperation == null) |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
65 return true; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
66 |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
67 while (op != m_boundOperation && op.Level > m_boundOperation.Level) |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
68 op = op.Parent; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
69 return op == m_boundOperation; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
70 } |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
71 |
40 | 72 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) { |
73 if (channel == null) | |
74 throw new ArgumentNullException("channel"); | |
75 | |
76 lock (m_subscriptions) { | |
77 Action subscription; | |
78 if (m_subscriptions.TryGetValue(channel, out subscription)) { | |
79 subscription(); | |
80 m_subscriptions.Remove(channel); | |
81 } | |
82 } | |
83 } | |
84 | |
85 public void UnsubscribeAll() { | |
86 lock (m_subscriptions) { | |
87 foreach (var subscription in m_subscriptions.Values) | |
88 subscription(); | |
89 m_subscriptions.Clear(); | |
90 } | |
91 } | |
92 | |
47 | 93 /// <summary> |
94 /// Вызывается для записи текста сообщения, в журнал. | |
95 /// </summary> | |
96 /// <remarks> | |
97 /// Данный метод может вызваться из разных потоков одновременно. Возможна ситуация, когда | |
98 /// данный метод вызывается уже после освобождения ообъекта методом <see cref="Dispose()"/>. | |
99 /// </remarks> | |
100 /// <param name="context">Контекст трассировки.</param> | |
101 /// <param name="text">Текст сообщения.</param> | |
48 | 102 protected abstract void WriteEntry(TraceContext context, EventText text, string channel); |
40 | 103 |
104 public EventText Format(TraceContext context, object data) { | |
105 return new EventText { | |
106 indent = context.CurrentOperation.Level, | |
107 content = data.ToString() | |
108 }; | |
109 } | |
110 | |
111 public EventText Format(TraceContext context, TraceEvent data) { | |
112 var level = context.CurrentOperation.Level; | |
113 if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) | |
114 level--; | |
115 | |
116 return new EventText { | |
117 indent = level, | |
118 content = data.ToString() | |
119 }; | |
120 } | |
121 | |
122 protected override void Dispose(bool disposing) { | |
47 | 123 base.Dispose(disposing); |
40 | 124 if (disposing) { |
125 UnsubscribeAll(); | |
126 } | |
127 } | |
128 } | |
129 } |