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