##// END OF EJS Templates
Added IObservable to TraceRegistry
cin -
r288:90cef6117ced v3
parent child
Show More
@@ -19,7 +19,9
19 19 </ItemGroup>
20 20
21 21 <ItemGroup>
22 <PackageReference Include="Unity" Version="5.8.5" />
22 <PackageReference Include="Unity" Version="5.8.6" />
23 <PackageReference Include="System.Reactive" Version="4.0.0" />
24
23 25 </ItemGroup>
24 26
25 27 </Project>
@@ -12,6 +12,7 using Unity.Injection;
12 12 using Unity.Registration;
13 13
14 14 namespace Implab.Playground {
15 using System.Reactive.Linq;
15 16 using static Trace<Bar>;
16 17
17 18 class Foo {
@@ -27,14 +28,10 namespace Implab.Playground {
27 28 Trace<Foo>.Log("First!");
28 29 Log("+1!");
29 30
30 using(TraceRegistry.Global.Subscribe(x => {
31 var ch = x as TraceSourceChannel;
32 if (ch == null)
33 return;
34
31 using(TraceRegistry.Global.OfType<TraceSourceChannel>().Subscribe(ch => {
35 32 Console.WriteLine($"{ch.Id}: {ch.Source.Name}");
36 33
37 }, true)) {
34 })) {
38 35 Trace<Foo>.Log("Hi!");
39 36 Log("Respect!");
40 37 }
@@ -17,7 +17,7
17 17 </PropertyGroup>
18 18
19 19 <ItemGroup>
20 <PackageReference Include="Unity" Version="5.8.5" />
20 <PackageReference Include="Unity" Version="5.8.6" />
21 21 </ItemGroup>
22 22
23 23 <ItemGroup>
@@ -23,14 +23,7 namespace Implab.Test {
23 23 TraceRegistry.Global.Subscribe(x => {
24 24 visited++;
25 25 found = x as TraceSourceChannel;
26 }, false);
27
28 Assert.Equal(0, visited);
29
30 TraceRegistry.Global.Subscribe(x => {
31 visited++;
32 found = x as TraceSourceChannel;
33 }, true);
26 });
34 27
35 28 Assert.Equal(1,visited);
36 29 Assert.Equal(channel, found);
@@ -14,6 +14,7
14 14
15 15 <ItemGroup>
16 16 <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0-preview-20180109-01" />
17 <PackageReference Include="System.Reactive" Version="4.0.0" />
17 18 <PackageReference Include="xunit" Version="2.3.1" />
18 19 <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
19 20 <ProjectReference Include="../Implab/Implab.csproj"/>
@@ -4,11 +4,10 using System.Diagnostics;
4 4 using Implab.Parallels;
5 5
6 6 namespace Implab.Diagnostics {
7 public class TraceRegistry {
7 public class TraceRegistry: IObservable<TraceChannel> {
8 8
9 9 class Subscription : IDisposable {
10 10 readonly WeakReference<TraceRegistry> m_registry;
11 readonly Action<object> m_unsubscribe;
12 11
13 12 public Subscription(TraceRegistry registry) {
14 13 m_registry = new WeakReference<TraceRegistry>(registry);
@@ -21,28 +20,32 namespace Implab.Diagnostics {
21 20 }
22 21 }
23 22
23 /// <summary>
24 /// The global collection of available diagnostic channels
25 /// </summary>
26 /// <returns></returns>
24 27 public static TraceRegistry Global { get; } = new TraceRegistry();
25 28
26 29 readonly object m_lock = new object();
27 30
28 readonly Dictionary<object, Action<TraceChannel>> m_subscriptions = new Dictionary<object, Action<TraceChannel>>();
31 readonly Dictionary<object, IObserver<TraceChannel>> m_subscriptions = new Dictionary<object, IObserver<TraceChannel>>();
29 32 readonly SimpleAsyncQueue<TraceChannel> m_channels = new SimpleAsyncQueue<TraceChannel>();
30 33
31 internal void Register(TraceChannel channel) {
34 public void Register(TraceChannel channel) {
32 35 // notifications can run in parallel
33 Action<TraceChannel>[] handlers = null;
36 IObserver<TraceChannel>[] handlers = null;
34 37
35 38 lock(m_lock) {
36 39 m_channels.Enqueue(channel);
37 40 if (m_subscriptions.Count > 0) {
38 handlers = new Action<TraceChannel>[m_subscriptions.Count];
41 handlers = new IObserver<TraceChannel>[m_subscriptions.Count];
39 42 m_subscriptions.Values.CopyTo(handlers, 0);
40 43 }
41 44 }
42 45
43 46 if (handlers != null)
44 47 foreach(var h in handlers)
45 h(channel);
48 h.OnNext(channel);
46 49 }
47 50
48 51 /// <summary>
@@ -51,7 +54,7 namespace Implab.Diagnostics {
51 54 /// </summary>
52 55 /// <param name="handler"></param>
53 56 /// <returns></returns>
54 public IDisposable Subscribe(Action<TraceChannel> handler, bool existing) {
57 public IDisposable Subscribe(IObserver<TraceChannel> handler) {
55 58 Safe.ArgumentNotNull(handler, nameof(handler));
56 59
57 60 var cookie = new Subscription(this);
@@ -66,9 +69,9 namespace Implab.Diagnostics {
66 69 }
67 70
68 71 // announce previously declared channels if required
69 if (existing) {
72 if (snap != null) {
70 73 foreach(var c in snap)
71 handler(c);
74 handler.OnNext(c);
72 75 }
73 76
74 77 // return the subscription
@@ -8,7 +8,7
8 8 and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc.
9 9 </Description>
10 10 <Copyright>2012-2018 Sergey Smirnov</Copyright>
11 <Version>3.0.12</Version>
11 <Version>3.0.14</Version>
12 12 <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl>
13 13 <PackageProjectUrl>https://implab.org</PackageProjectUrl>
14 14 <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl>
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now