annotate Implab/Diagnostics/ListenerBase.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents 04d4c92d0f28
children 240aa6994018
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
134
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
1 using System;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
2 using System.Collections.Generic;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
3 using System.Linq;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
4 using System.Text;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
5
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
6 namespace Implab.Diagnostics {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
7 public abstract class ListenerBase : ServiceLocator, ILogWriter<object>, ILogWriter<TraceEvent> {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
8
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
9 readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
10
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
11 protected ListenerBase() {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
12 Register(this);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
13 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
14
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
15 public void Subscribe(Type eventType) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
16 if (eventType == null)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
17 throw new ArgumentNullException("eventType");
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
18 GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
19 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
20
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
21 public void Subscribe<TEvent>() {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
22 Subscribe<TEvent>(LogChannel<TEvent>.Default);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
23 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
24
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
25 public void Subscribe<TEvent>(LogChannel<TEvent> channel) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
26 if (channel == null)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
27 throw new ArgumentNullException("channel");
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
28
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
29 lock (m_subscriptions) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
30 AssertNotDisposed();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
31 if (m_subscriptions.ContainsKey(channel))
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
32 return;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
33
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
34 var writer = GetService<ILogWriter<TEvent>>();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
35
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
36 EventHandler<LogEventArgs<TEvent>> handler = (sender, args) => writer.Write(args,args.Value);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
37
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
38 channel.Events += handler;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
39
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
40 Action unsubscribe = () => {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
41 channel.Events -= handler;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
42 };
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
43
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
44 m_subscriptions.Add(channel, unsubscribe);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
45 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
46 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
47
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
48 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
49 if (channel == null)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
50 throw new ArgumentNullException("channel");
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
51
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
52 lock (m_subscriptions) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
53 Action subscription;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
54 if (m_subscriptions.TryGetValue(channel, out subscription)) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
55 subscription();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
56 m_subscriptions.Remove(channel);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
57 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
58 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
59 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
60
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
61 public void UnsubscribeAll() {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
62 lock (m_subscriptions) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
63 foreach (var subscription in m_subscriptions.Values)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
64 subscription();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
65 m_subscriptions.Clear();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
66 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
67 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
68
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
69 #region ILogWriter implementation
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
70 public abstract void Write(LogEventArgs args, object entry);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
71 #endregion
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
72
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
73 #region ILogWriter implementation
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
74 public virtual void Write(LogEventArgs args, TraceEvent entry) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
75 Write(args, (object)entry);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
76 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
77 #endregion
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
78
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
79
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
80 protected override void Dispose(bool disposing) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
81 base.Dispose(disposing);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
82 if (disposing) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
83 UnsubscribeAll();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
84 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
85 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
86 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
87 }