@@ -0,0 +1,37 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Web; | |
|
5 | ||
|
6 | namespace Implab { | |
|
7 | /// <summary> | |
|
8 | /// ΠΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡ ΡΠ²Π»ΡΡΡΡΡ ΡΠΎΡΡΠΎΠ²Π½ΡΠΌΠΈ ΠΎΠ±ΡΠ΅ΠΊΡΠ°ΠΌΠΈ, ΠΈΠΌΠ΅ΡΡ Π΄Π΅ΡΠ΅ΡΠΌΠΈΠ½ΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΏΠ΅ΡΠΈΠΎΠ΄ ΠΆΠΈΠ·Π½ΠΈ, Π°Π²ΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°ΡΡ ΡΠ΅ΡΡΡΡΡ Π²Ρ ΠΎΠ΄ΡΡΠΈΠ΅ Π² Π½ΠΈΡ . | |
|
9 | /// </summary> | |
|
10 | /// <remarks>ΠΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° ΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ Π²ΡΠ΅ΠΌΠ΅Π½Π΅ΠΌ ΠΆΠΈΠ·Π½ΠΈ Π²ΠΊΠ»ΡΡΠ΅Π½Π½ΡΡ Π² Π½Π΅Π΅ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ</remarks> | |
|
11 | public class Component: Disposable { | |
|
12 | LinkedList<IDisposable> m_components = new LinkedList<IDisposable>(); | |
|
13 | ||
|
14 | /// <summary> | |
|
15 | /// ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ, ΠΈΠ· ΠΊΠΎΡΠΎΡΡΡ ΡΠΎΡΡΠΎΠΈΡ ΡΠ΅ΠΊΡΡΠ°Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ°. | |
|
16 | /// </summary> | |
|
17 | public ICollection<IDisposable> Components { | |
|
18 | get { | |
|
19 | AssertNotDisposed(); | |
|
20 | return m_components; | |
|
21 | } | |
|
22 | } | |
|
23 | ||
|
24 | /// <summary> | |
|
25 | /// ΠΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Π΅Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡ, Π²Ρ ΠΎΠ΄ΡΡΠΈΠ΅ Π² ΡΠΎΡΡΠ°Π² ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡ. | |
|
26 | /// </summary> | |
|
27 | /// <param name="disposing">ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΡΠ΅ΡΡΡΡΠΎΠ².</param> | |
|
28 | protected override void Dispose(bool disposing) { | |
|
29 | if (disposing) { | |
|
30 | foreach (var item in m_components) | |
|
31 | item.Dispose(); | |
|
32 | m_components.Clear(); | |
|
33 | } | |
|
34 | base.Dispose(disposing); | |
|
35 | } | |
|
36 | } | |
|
37 | } No newline at end of file |
@@ -0,0 +1,12 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | ||
|
6 | namespace Implab.Diagnostics { | |
|
7 | public struct EventText { | |
|
8 | public int indent; | |
|
9 | ||
|
10 | public string content; | |
|
11 | } | |
|
12 | } |
@@ -0,0 +1,10 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | ||
|
6 | namespace Implab.Diagnostics { | |
|
7 | public interface IEventTextFormatter<in TEvent> { | |
|
8 | EventText Format(TraceContext context, TEvent data); | |
|
9 | } | |
|
10 | } |
@@ -0,0 +1,44 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.IO; | |
|
4 | using System.Linq; | |
|
5 | using System.Text; | |
|
6 | ||
|
7 | namespace Implab.Diagnostics { | |
|
8 | public class TextFileListener: TextListenerBase { | |
|
9 | readonly TextWriter m_textWriter; | |
|
10 | ||
|
11 | public TextFileListener(string fileName) { | |
|
12 | m_textWriter = File.CreateText(fileName); | |
|
13 | ||
|
14 | m_textWriter.WriteLine("LOG {0}", DateTime.Now); | |
|
15 | Register(this); | |
|
16 | } | |
|
17 | ||
|
18 | protected override void WriteEntry(TraceContext context, EventText text) { | |
|
19 | var msg = new StringBuilder(); | |
|
20 | for (int i = 0; i < text.indent; i++) | |
|
21 | msg.Append(" "); | |
|
22 | msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content); | |
|
23 | ||
|
24 | lock (m_textWriter) { | |
|
25 | if (!IsDisposed) { | |
|
26 | m_textWriter.WriteLine(msg.ToString()); | |
|
27 | m_textWriter.Flush(); | |
|
28 | } | |
|
29 | } | |
|
30 | } | |
|
31 | ||
|
32 | ||
|
33 | protected override void Dispose(bool disposing) { | |
|
34 | base.Dispose(disposing); | |
|
35 | if (disposing) { | |
|
36 | lock (m_textWriter) { | |
|
37 | Safe.Dispose(m_textWriter); | |
|
38 | } | |
|
39 | } | |
|
40 | } | |
|
41 | ||
|
42 | ||
|
43 | } | |
|
44 | } |
@@ -0,0 +1,99 | |||
|
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>(); | |
|
10 | ||
|
11 | protected TextListenerBase() { | |
|
12 | Register(this); | |
|
13 | } | |
|
14 | ||
|
15 | public void Subscribe(Type eventType) { | |
|
16 | if (eventType == null) | |
|
17 | throw new ArgumentNullException("eventType"); | |
|
18 | GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null); | |
|
19 | } | |
|
20 | ||
|
21 | public void Subscribe<TEvent>() { | |
|
22 | Subscribe<TEvent>(LogChannel<TEvent>.Default); | |
|
23 | } | |
|
24 | ||
|
25 | public void Subscribe<TEvent>(LogChannel<TEvent> channel) { | |
|
26 | if (channel == null) | |
|
27 | throw new ArgumentNullException("channel"); | |
|
28 | ||
|
29 | lock (m_subscriptions) { | |
|
30 | AssertNotDisposed(); | |
|
31 | ||
|
32 | var formatter = GetService<IEventTextFormatter<TEvent>>(); | |
|
33 | ||
|
34 | EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => { | |
|
35 | WriteEntry((TraceContext)sender, formatter.Format((TraceContext)sender, args.Value)); | |
|
36 | }; | |
|
37 | ||
|
38 | if (m_subscriptions.ContainsKey(channel)) | |
|
39 | return; | |
|
40 | ||
|
41 | channel.Events += handler; | |
|
42 | ||
|
43 | Action unsubscribe = () => { | |
|
44 | channel.Events -= handler; | |
|
45 | }; | |
|
46 | ||
|
47 | m_subscriptions.Add(channel, unsubscribe); | |
|
48 | } | |
|
49 | } | |
|
50 | ||
|
51 | public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) { | |
|
52 | if (channel == null) | |
|
53 | throw new ArgumentNullException("channel"); | |
|
54 | ||
|
55 | lock (m_subscriptions) { | |
|
56 | Action subscription; | |
|
57 | if (m_subscriptions.TryGetValue(channel, out subscription)) { | |
|
58 | subscription(); | |
|
59 | m_subscriptions.Remove(channel); | |
|
60 | } | |
|
61 | } | |
|
62 | } | |
|
63 | ||
|
64 | public void UnsubscribeAll() { | |
|
65 | lock (m_subscriptions) { | |
|
66 | foreach (var subscription in m_subscriptions.Values) | |
|
67 | subscription(); | |
|
68 | m_subscriptions.Clear(); | |
|
69 | } | |
|
70 | } | |
|
71 | ||
|
72 | protected abstract void WriteEntry(TraceContext context, EventText text); | |
|
73 | ||
|
74 | public EventText Format(TraceContext context, object data) { | |
|
75 | return new EventText { | |
|
76 | indent = context.CurrentOperation.Level, | |
|
77 | content = data.ToString() | |
|
78 | }; | |
|
79 | } | |
|
80 | ||
|
81 | public EventText Format(TraceContext context, TraceEvent data) { | |
|
82 | var level = context.CurrentOperation.Level; | |
|
83 | if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) | |
|
84 | level--; | |
|
85 | ||
|
86 | return new EventText { | |
|
87 | indent = level, | |
|
88 | content = data.ToString() | |
|
89 | }; | |
|
90 | } | |
|
91 | ||
|
92 | protected override void Dispose(bool disposing) { | |
|
93 | if (disposing) { | |
|
94 | UnsubscribeAll(); | |
|
95 | } | |
|
96 | base.Dispose(disposing); | |
|
97 | } | |
|
98 | } | |
|
99 | } |
@@ -0,0 +1,46 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Diagnostics; | |
|
4 | using System.Linq; | |
|
5 | using System.Web; | |
|
6 | ||
|
7 | namespace Implab { | |
|
8 | public class Disposable : IDisposable { | |
|
9 | ||
|
10 | bool m_disposed; | |
|
11 | ||
|
12 | public event EventHandler Disposed; | |
|
13 | ||
|
14 | public bool IsDisposed { | |
|
15 | get { return m_disposed; } | |
|
16 | } | |
|
17 | ||
|
18 | protected void AssertNotDisposed() { | |
|
19 | if (m_disposed) | |
|
20 | throw new ObjectDisposedException(this.ToString()); | |
|
21 | } | |
|
22 | ||
|
23 | protected virtual void Dispose(bool disposing) { | |
|
24 | if (disposing && !m_disposed) { | |
|
25 | m_disposed = true; | |
|
26 | ||
|
27 | EventHandler temp = Disposed; | |
|
28 | if (temp != null) | |
|
29 | temp(this,EventArgs.Empty); | |
|
30 | } | |
|
31 | } | |
|
32 | public void Dispose() { | |
|
33 | Dispose(true); | |
|
34 | GC.SuppressFinalize(this); | |
|
35 | } | |
|
36 | ||
|
37 | protected virtual void ReportObjectLeaks() { | |
|
38 | Trace.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); | |
|
39 | } | |
|
40 | ||
|
41 | ~Disposable() { | |
|
42 | Dispose(false); | |
|
43 | ReportObjectLeaks(); | |
|
44 | } | |
|
45 | } | |
|
46 | } No newline at end of file |
@@ -0,0 +1,12 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | using System.Threading.Tasks; | |
|
6 | ||
|
7 | namespace Implab { | |
|
8 | public interface IServiceLocator: IServiceProvider { | |
|
9 | T GetService<T>(); | |
|
10 | bool TryGetService<T>(out T service); | |
|
11 | } | |
|
12 | } |
@@ -0,0 +1,209 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Web; | |
|
5 | ||
|
6 | namespace Implab { | |
|
7 | /// <summary> | |
|
8 | /// ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΡΠ΅ΡΠ²ΠΈΡΠΎΠ², ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°ΡΡ ΠΈ ΠΏΠΎΠ»ΡΡΠ°ΡΡ ΡΠ΅ΡΠ²ΠΈΡΡ. | |
|
9 | /// </summary> | |
|
10 | public class ServiceLocator: Component, IServiceLocator, IServiceProvider { | |
|
11 | // Π·Π°ΠΏΠΈΡΡ ΠΎΠ± ΡΠ΅ΡΠ²ΠΈΡΠ΅ | |
|
12 | struct ServiceEntry { | |
|
13 | public object service; // ΡΠ΅ΡΠ²ΠΈΡ | |
|
14 | public bool shared; // ΠΏΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΡΠ΅ΡΠ²ΠΈΡ ΠΠ Π½ΡΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°ΡΡ | |
|
15 | public Func<object> activator; // Π°ΠΊΡΠΈΠ²Π°ΡΠΎΡ ΡΠ΅ΡΠ²ΠΈΡΠ° ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ | |
|
16 | public List<Type> associated; // ΡΡΡΠ»ΠΊΠΈ Π½Π° ΡΠ΅ΠΊΡΡΡΡ Π·Π°ΠΏΠΈΡΡ | |
|
17 | public Type origin; // ΡΡΡΠ»ΠΊΠ° Π½Π° ΠΎΡΠΈΠ³ΠΈΠ½Π°Π»ΡΠ½ΡΡ Π·Π°ΠΏΠΈΡΡ ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ΅ | |
|
18 | } | |
|
19 | ||
|
20 | // ΡΠ»ΠΎΠ²Π°ΡΡ ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠΈΡ ΡΠ΅ΡΠ²ΠΈΡΠΎΠ² | |
|
21 | Dictionary<Type, ServiceEntry> m_services = new Dictionary<Type,ServiceEntry>(); | |
|
22 | ||
|
23 | /// <summary> | |
|
24 | /// ΠΠΎΠ»ΡΡΠ°Π΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ <typeparamref name="T"/>. | |
|
25 | /// </summary> | |
|
26 | /// <typeparam name="T">Π’ΠΈΠΏ Π·Π°ΠΏΡΠ°ΡΠΈΠ²Π°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°</typeparam> | |
|
27 | /// <returns>ΠΠ±ΡΠ΅ΠΊΡ, ΡΠ΅Π°Π»ΠΈΠ·ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ</returns> | |
|
28 | /// <exception cref="KeyNotFoundException">Π‘Π΅ΡΠ²ΠΈΡ Π½Π΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½</exception> | |
|
29 | public T GetService<T>() { | |
|
30 | return (T)GetService(typeof(T)); | |
|
31 | } | |
|
32 | ||
|
33 | ||
|
34 | /// <summary> | |
|
35 | /// ΠΡΡΠ°Π΅ΡΡΡ ΠΏΠΎΠ»ΡΡΠΈΡΡ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΡΠ΅ΡΠ²ΠΈΡ, Π² ΡΠ»ΡΡΠ°Π΅, Π΅ΡΠ»ΠΈ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° Π½Π΅ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΠ΅Ρ ΡΡΠ΅Π±ΡΠ΅ΠΌΡΠΉ ΡΠ΅ΡΠ²ΠΈΡ | |
|
36 | /// Π½Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠΉ. | |
|
37 | /// </summary> | |
|
38 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΡΠ΅Π±ΡΠ΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°.</typeparam> | |
|
39 | /// <param name="service">ΠΠ±ΡΠ΅ΠΊΡ ΡΠ΅Π°Π»ΠΈΠ·ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ, ΠΈΠ»ΠΈ <c>default(T)</c> Π΅ΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ²ΠΎΠ³ΠΎ Π½Π΅Ρ.</param> | |
|
40 | /// <returns><c>true</c> - ΡΠ΅ΡΠ²ΠΈΡ Π½Π°ΠΉΠ΄Π΅Π½, <c>false</c> - ΡΠ΅ΡΠ²ΠΈΡ Π½Π΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½.</returns> | |
|
41 | public bool TryGetService<T>(out T service) { | |
|
42 | AssertNotDisposed(); | |
|
43 | ||
|
44 | try { | |
|
45 | service = GetService<T>(); | |
|
46 | return true; | |
|
47 | } catch(KeyNotFoundException) { | |
|
48 | service = default(T); | |
|
49 | return false; | |
|
50 | } | |
|
51 | } | |
|
52 | ||
|
53 | /// <summary> | |
|
54 | /// ΠΠΎΠ»ΡΡΠ°Π΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ <paramref name="serviceType"/> | |
|
55 | /// </summary> | |
|
56 | /// <param name="serviceType">Π’ΠΈΠΏ Π·Π°ΠΏΡΠ°ΡΠΈΠ²Π°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°</param> | |
|
57 | /// <returns>ΠΠ±ΡΠ΅ΠΊΡ, ΡΠ΅Π°Π»ΠΈΠ·ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ</returns> | |
|
58 | /// <exception cref="KeyNotFoundException">Π‘Π΅ΡΠ²ΠΈΡ Π½Π΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½</exception> | |
|
59 | public object GetService(Type serviceType) { | |
|
60 | if (serviceType == null) | |
|
61 | throw new ArgumentNullException("serviceType"); | |
|
62 | AssertNotDisposed(); | |
|
63 | ||
|
64 | ServiceEntry se; | |
|
65 | if (!m_services.TryGetValue(serviceType, out se)) { | |
|
66 | // ΠΈΡΠ΅ΠΌ Π±Π»ΠΈΠΆΠ°ΠΉΡΠΈΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ, ΡΠ΅Π°Π»ΠΈΠ·ΡΡΡΠΈΠΉ Π½ΡΠΆΠ½ΡΠΉ ΡΠ΅ΡΠ²ΠΈΡ | |
|
67 | Type pt = null; | |
|
68 | foreach (var t in m_services.Keys) | |
|
69 | if (serviceType.IsAssignableFrom(t) && (pt == null || t.IsAssignableFrom(pt))) | |
|
70 | pt = t; | |
|
71 | if (pt == null) | |
|
72 | throw new ApplicationException(String.Format("{0} doesn't provide {1} service",this,serviceType)); | |
|
73 | ||
|
74 | var pe = m_services[pt]; | |
|
75 | ||
|
76 | // Π½Π°ΠΉΠ΄Π΅Π½Π½Π°Ρ Π·Π°ΠΏΠΈΡΡ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ»Π°ΡΡΡΡ Π½Π° ΠΎΡΠΈΠ³ΠΈΠ½Π°Π»ΡΠ½ΡΡ Π·Π°ΠΏΠΈΡΡ Ρ ΡΠ΅ΡΠ²ΠΈΡΠΎΠΌ | |
|
77 | if(pe.origin != null) { | |
|
78 | pt = pe.origin; | |
|
79 | pe = m_services[pt]; | |
|
80 | } | |
|
81 | ||
|
82 | // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΠΏΠΈΡΠΎΠΊ Ρ ΠΎΠ±ΡΠ°ΡΠ½ΡΠΌΠΈ ΡΡΡΠ»ΠΊΠ°ΠΌΠΈ | |
|
83 | if (pe.associated == null) | |
|
84 | pe.associated = new List<Type>(); | |
|
85 | ||
|
86 | pe.associated.Add(serviceType); | |
|
87 | ||
|
88 | // ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΡΠΊΡΡ Π·Π°ΠΏΠΈΡΡ | |
|
89 | m_services[pt] = pe; | |
|
90 | ||
|
91 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π·Π°ΠΏΠΈΡΡ ΡΠΎ ΡΡΡΠ»ΠΊΠΎΠΉ | |
|
92 | se = new ServiceEntry { | |
|
93 | service = pe.service, | |
|
94 | origin = pt, | |
|
95 | shared = true // ΠΏΡΠ΅Π΄ΠΎΡΠ²ΡΠ°ΡΠ°Π΅ΠΌ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π΅Π½Π½ΡΠ΅ ΠΏΠΎΠΏΡΡΠΊΠΈ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ | |
|
96 | }; | |
|
97 | ||
|
98 | m_services[serviceType] = se; | |
|
99 | } | |
|
100 | ||
|
101 | // Π·Π°ΠΏΠΈΡΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ΅ | |
|
102 | if (se.service != null) | |
|
103 | return se.service; | |
|
104 | ||
|
105 | // ΡΠ΅ΠΊΡΡΠ°Ρ Π·Π°ΠΏΠΈΡΡ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΡΡΠ»ΠΊΠΎΠΉ | |
|
106 | if (se.origin != null) { | |
|
107 | se.service = GetService(se.origin); | |
|
108 | m_services[serviceType] = se; | |
|
109 | return se.service; | |
|
110 | } | |
|
111 | ||
|
112 | // ΡΠ΅ΠΊΡΡΠ°Ρ Π·Π°ΠΏΠΈΡΡ Π½Π΅ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΡΡΠ»ΠΊΠΎΠΉ ΠΈ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ΅ | |
|
113 | // ΠΎΠ½Π° Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠΎΠΆΠ΅ΡΠΆΠ°ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎΠ± Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ | |
|
114 | if (se.activator != null) { | |
|
115 | se.service = se.activator(); | |
|
116 | ||
|
117 | m_services[serviceType] = se; | |
|
118 | ||
|
119 | return se.service; | |
|
120 | } | |
|
121 | ||
|
122 | throw new Exception("Unable to create a service instance"); | |
|
123 | } | |
|
124 | ||
|
125 | /// <summary> | |
|
126 | /// Π Π΅Π³ΠΈΡΡΡΠΈΡΡΠ΅Ρ ΡΠ°Π±ΡΠΈΠΊΡ Π΄Π»Ρ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ ΡΠ΅ΡΠ²ΠΈΡΠ° ΠΏΠΎ ΠΏΠ΅ΡΠ²ΠΎΠΌΡ ΡΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΡ. | |
|
127 | /// </summary> | |
|
128 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π³ΠΈΡΡΡΠΈΡΡΠ΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°.</typeparam> | |
|
129 | /// <param name="activator">Π€Π°Π±ΡΠΈΠΊΠ° Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ/ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠ°, ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠ΅Π³ΠΎ ΡΠ΅ΡΠ²ΠΈΡ.</param> | |
|
130 | /// <exception cref="InvalidOperationException">Π£ΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΡΠ΅ΡΠ²ΠΈΡ ΡΠΆΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½.</exception> | |
|
131 | /// <remarks>ΠΡΠΈ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΠΈ ΡΠ΅ΡΠ²ΠΈΡ-Π»ΠΎΠΊΠ°ΡΠΎΡΠ°, ΡΠ΅ΡΠ²ΠΈΡΡ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠ΅ Π² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ΅ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½Ρ.</remarks> | |
|
132 | public void Register<T>(Func<T> activator) { | |
|
133 | if (activator == null) | |
|
134 | throw new ArgumentNullException("activator"); | |
|
135 | ||
|
136 | AssertNotDisposed(); | |
|
137 | ||
|
138 | Unregister(typeof(T)); | |
|
139 | ||
|
140 | m_services[typeof(T)] = new ServiceEntry { | |
|
141 | activator = () => activator() as object | |
|
142 | }; | |
|
143 | } | |
|
144 | ||
|
145 | /// <summary> | |
|
146 | /// Π Π΅Π³ΠΈΡΡΡΠΈΡΡΠ΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡ, ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ. | |
|
147 | /// </summary> | |
|
148 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π³ΠΈΡΡΡΠΈΡΡΠ΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°.</typeparam> | |
|
149 | /// <param name="service">ΠΠ±ΡΠ΅ΠΊΡ, ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ.</param> | |
|
150 | /// <exception cref="InvalidOperationException">Π£ΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΡΠ΅ΡΠ²ΠΈΡ ΡΠΆΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½.</exception> | |
|
151 | /// <remarks>Π‘Π΅ΡΠ²ΠΈΡ-Π»ΠΎΠΊΠ°ΡΠΎΡΠΎΠΌ Π½Π΅ ΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ Π²ΡΠ΅ΠΌΠ΅Π½Π΅ΠΌ ΠΆΠΈΠ·Π½ΠΈ ΠΎΠ±ΡΠ΅ΠΊΡΠ° Π΄Π»Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°.</remarks> | |
|
152 | public void Register<T>(T service) { | |
|
153 | Register(service, true); | |
|
154 | } | |
|
155 | ||
|
156 | /// <summary> | |
|
157 | /// Π Π΅Π³ΠΈΡΡΡΠΈΡΡΠ΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡ, ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ. | |
|
158 | /// </summary> | |
|
159 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π³ΠΈΡΡΡΠΈΡΡΠ΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ²ΠΈΡΠ°.</typeparam> | |
|
160 | /// <param name="service">ΠΠ±ΡΠ΅ΠΊΡ, ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»ΡΡΡΠΈΠΉ ΡΠ΅ΡΠ²ΠΈΡ.</param> | |
|
161 | /// <param name="shared">ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ΅ΠΊΡ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠ°Π·Π΄Π΅Π»ΡΠ΅ΠΌΡΠΌ ΠΈ ΡΠ΅ΡΠ²ΠΈΡ-Π»ΠΎΠΊΠ°ΡΠΎΡ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π΅Π³ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°ΡΡ.</param> | |
|
162 | /// <exception cref="InvalidOperationException">Π£ΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΡΠ΅ΡΠ²ΠΈΡ ΡΠΆΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½.</exception> | |
|
163 | public void Register<T>(T service, bool shared) { | |
|
164 | if (service == null) | |
|
165 | throw new ArgumentNullException("service"); | |
|
166 | ||
|
167 | AssertNotDisposed(); | |
|
168 | ||
|
169 | Unregister(typeof(T)); | |
|
170 | ||
|
171 | m_services[typeof(T)] = new ServiceEntry { service = service, shared = shared }; | |
|
172 | } | |
|
173 | ||
|
174 | public void Unregister(Type serviceType) { | |
|
175 | if (serviceType == null) | |
|
176 | throw new ArgumentNullException("serviceType"); | |
|
177 | ||
|
178 | AssertNotDisposed(); | |
|
179 | ||
|
180 | ServiceEntry se; | |
|
181 | if (m_services.TryGetValue(serviceType, out se)) { | |
|
182 | // ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Π΅ΠΌ ΡΠ΅ΡΡΡΡΡ | |
|
183 | if (se.service != null && !se.shared) | |
|
184 | ((IDisposable)se.service).Dispose(); | |
|
185 | m_services.Remove(serviceType); | |
|
186 | ||
|
187 | // ΡΠ±ΠΈΡΠ°Π΅ΠΌ ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π·Π°ΠΏΠΈΡΠΈ | |
|
188 | if (se.associated != null) | |
|
189 | foreach (var item in se.associated) | |
|
190 | m_services.Remove(item); | |
|
191 | } | |
|
192 | } | |
|
193 | ||
|
194 | /// <summary> | |
|
195 | /// ΠΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Π΅Ρ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ ΡΠ΅ΡΠ²ΠΈΡΡ (ΠΊΠΎΡΠΎΡΡΠ΅ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΈΡΡ). | |
|
196 | /// </summary> | |
|
197 | /// <param name="disposing">ΠΡΠΈΠ·Π°Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π½ΡΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ ΡΠ΅ΡΡΡΡΡ.</param> | |
|
198 | protected override void Dispose(bool disposing) { | |
|
199 | if (disposing) { | |
|
200 | ||
|
201 | foreach (var entry in m_services.Values) | |
|
202 | if (!entry.shared && entry.service is IDisposable) | |
|
203 | ((IDisposable)entry.service).Dispose(); | |
|
204 | ||
|
205 | } | |
|
206 | base.Dispose(disposing); | |
|
207 | } | |
|
208 | } | |
|
209 | } No newline at end of file |
@@ -1,37 +1,24 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Diagnostics { |
|
7 | public class ConsoleTraceListener { | |
|
7 | public class ConsoleTraceListener: TextListenerBase { | |
|
8 | 8 | |
|
9 | 9 | static readonly object _consoleLock = new object(); |
|
10 | 10 | |
|
11 | public void Subscribe() { | |
|
12 | LogChannel<TraceEvent>.Default.Events += Default_Events; | |
|
13 | } | |
|
14 | ||
|
15 | public void Unsubscribe() { | |
|
16 | LogChannel<TraceEvent>.Default.Events -= Default_Events; | |
|
17 | } | |
|
11 | protected override void WriteEntry(TraceContext context, EventText text) { | |
|
12 | var msg = new StringBuilder(); | |
|
18 | 13 | |
|
19 | void Default_Events(object sender, ValueEventArgs<TraceEvent> e) { | |
|
20 | LogEvent((TraceContext)sender, e.Value); | |
|
21 | } | |
|
22 | ||
|
23 | void LogEvent(TraceContext context, TraceEvent evt) { | |
|
24 | var msg = new StringBuilder(); | |
|
25 | for (int i = 0; i < context.CurrentOperation.Level; i++) | |
|
14 | for (int i = 0; i < text.indent; i++) | |
|
26 | 15 | msg.Append(" "); |
|
27 | msg.Append(evt.EventType); | |
|
28 | msg.AppendFormat("[{0}]: ",context.ThreadId); | |
|
29 | msg.Append(evt.Message); | |
|
16 | msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content); | |
|
30 | 17 | |
|
31 | 18 | lock (_consoleLock) { |
|
32 | 19 | Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1); |
|
33 | 20 | Console.WriteLine(msg.ToString()); |
|
34 | 21 | } |
|
35 | 22 | } |
|
36 | 23 | } |
|
37 | 24 | } |
@@ -1,97 +1,163 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Threading.Tasks; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Diagnostics { |
|
9 | /// <summary> | |
|
10 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΊ ΠΏΠΎΡΠΎΠΊΡ ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. | |
|
11 | /// </summary> | |
|
12 | /// <remarks> | |
|
13 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΡΠΌ ΡΠΎΠ±ΡΡΠΈΠΉ Π΄Π»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠ΅ΡΡΠ°, Π³Π΄Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅. | |
|
14 | /// </remarks> | |
|
9 | 15 | public class TraceContext { |
|
10 | 16 | LogicalOperation m_currentOperation; |
|
11 |
readonly LogicalOperation m_ |
|
|
17 | readonly LogicalOperation m_bound; | |
|
12 | 18 | readonly int m_threadId; |
|
13 | readonly TraceContext m_parent; | |
|
14 | ||
|
15 | readonly static object _consoleLock = new object(); | |
|
16 | 19 | |
|
17 | 20 | [ThreadStatic] |
|
18 | 21 | static TraceContext _current; |
|
19 | 22 | |
|
23 | /// <summary> | |
|
24 | /// Π’Π΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ Π΄Π»Ρ ΠΏΠΎΡΠΎΠΊΠ°, ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ Π°ΡΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ. | |
|
25 | /// </summary> | |
|
20 | 26 | public static TraceContext Current { |
|
21 | 27 | get { |
|
22 | 28 | if (_current == null) |
|
23 | 29 | _current = new TraceContext(); |
|
24 | 30 | return _current; |
|
25 | 31 | } |
|
26 | 32 | } |
|
27 | 33 | |
|
28 | 34 | TraceContext(TraceContext context) { |
|
29 | 35 | if (context == null) |
|
30 | 36 | throw new ArgumentNullException("context"); |
|
31 | 37 | |
|
32 | m_parent = context; | |
|
33 | 38 | m_currentOperation = context.CurrentOperation; |
|
34 |
m_ |
|
|
39 | m_bound = context.CurrentOperation; | |
|
35 | 40 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
36 | ||
|
37 | LogEvent(TraceEventType.Transfer, "FORK {0}", context.ThreadId); | |
|
38 | 41 | } |
|
39 | 42 | |
|
40 | 43 | TraceContext() { |
|
41 | 44 | m_currentOperation = new LogicalOperation(); |
|
42 |
m_ |
|
|
45 | m_bound = m_currentOperation; | |
|
43 | 46 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
44 | 47 | } |
|
45 | 48 | |
|
49 | /// <summary> | |
|
50 | /// ΠΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ ΠΊΠΎΠΏΠΈΡΡΠ΅Ρ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΠ²ΡΠΎΠ²ΠΊΠΈ Π² ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ. | |
|
51 | /// </summary> | |
|
52 | /// <param name="from">ΠΡΡ ΠΎΠ΄Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ.</param> | |
|
53 | /// <remarks> | |
|
54 | /// <para> | |
|
55 | /// ΠΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ Π·Π° ΡΡΠ΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΈ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ Π΅Π³ΠΎ | |
|
56 | /// ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°. ΠΡΠΈ ΡΡΠΎΠΌ ΠΊΠΎΠΏΠΈΡΡΠ΅ΡΡΡ ΡΡΠ΅ΠΊ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π½ΠΎΠ²ΠΎΠΌ | |
|
57 | /// ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Ρ. | |
|
58 | /// </para> | |
|
59 | /// <para> | |
|
60 | /// ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΡΠΎΡΡΠΎΡΠ»Π°ΡΡ, ΡΠΎ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ <see cref="TraceEventType.Transfer"/>. | |
|
61 | /// </para> | |
|
62 | /// </remarks> | |
|
46 | 63 | public static void Transfer(TraceContext from) { |
|
47 | _current = from == null ? new TraceContext() : new TraceContext(from); | |
|
48 | } | |
|
49 | ||
|
50 | public TraceContext ParentContext { | |
|
51 | get { | |
|
52 |
|
|
|
64 | if (_current == from) | |
|
65 | return; | |
|
66 | if (from != null) { | |
|
67 | var context = new TraceContext(from); | |
|
68 | context.LogEvent(TraceEventType.Transfer, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); | |
|
69 | _current = context; | |
|
70 | } else { | |
|
71 | _current = new TraceContext(); | |
|
53 | 72 | } |
|
54 | 73 | } |
|
55 | 74 | |
|
75 | /// <summary> | |
|
76 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΏΠΎΡΡΠΎΡΠ½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π΄Π°Π½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ Ρ ΡΠ°Π½ΠΈΡΡ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ ΡΠ΅ΡΠ΅Π· <see cref="Transfer(TraceContext)"/> | |
|
77 | /// </summary> | |
|
78 | /// <returns>ΠΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΈΠ»ΠΈ <c>null</c> Π΅ΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ²ΠΎΠΉ Π½Π΅ Π±ΡΠ» ΡΠΎΠ·Π΄Π°Π½.</returns> | |
|
79 | public static TraceContext Snapshot() { | |
|
80 | return _current == null ? null : new TraceContext(_current); | |
|
81 | } | |
|
82 | ||
|
83 | /// <summary> | |
|
84 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΠΈ Π²ΠΎΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°. | |
|
85 | /// </summary> | |
|
86 | /// <param name="action"></param> | |
|
87 | public void Invoke(Action action) { | |
|
88 | if (action == null) | |
|
89 | throw new ArgumentNullException("action"); | |
|
90 | var old = _current; | |
|
91 | Transfer(this); | |
|
92 | try { | |
|
93 | action(); | |
|
94 | } finally { | |
|
95 | _current = old; | |
|
96 | } | |
|
97 | } | |
|
98 | ||
|
99 | /// <summary> | |
|
100 | /// Π’Π΅ΠΊΡΡΠ°Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. | |
|
101 | /// </summary> | |
|
56 | 102 | public LogicalOperation CurrentOperation { |
|
57 | 103 | get { |
|
58 | 104 | return m_currentOperation; |
|
59 | 105 | } |
|
60 | 106 | } |
|
61 | 107 | |
|
62 | public LogicalOperation TraceBound { | |
|
108 | /// <summary> | |
|
109 | /// ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π½ΠΈΠΆΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π΅Π»ΡΠ·Ρ ΠΎΠΏΡΡΠΊΠ°ΡΡΡΡ Π² ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, Ρ.Π΅. ΠΎΠ½Π° Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Π° Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
110 | /// </summary> | |
|
111 | public LogicalOperation BoundOperation { | |
|
63 | 112 | get { |
|
64 |
return m_ |
|
|
113 | return m_bound; | |
|
65 | 114 | } |
|
66 | 115 | } |
|
67 | 116 | |
|
117 | /// <summary> | |
|
118 | /// ΠΠΎΡΠΎΠΊ, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ. | |
|
119 | /// </summary> | |
|
68 | 120 | public int ThreadId { |
|
69 | 121 | get { |
|
70 | 122 | return m_threadId; |
|
71 | 123 | } |
|
72 | 124 | } |
|
73 | 125 | |
|
126 | /// <summary> | |
|
127 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π±Π΅Π·ΡΠΌΡΠ½Π½ΡΡ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. | |
|
128 | /// </summary> | |
|
74 | 129 | public void StartLogicalOperation() { |
|
75 | 130 | StartLogicalOperation(null); |
|
76 | 131 | } |
|
77 | 132 | |
|
133 | /// <summary> | |
|
134 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ. Π‘ΠΎΠ·Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π΄ΠΎΠ±Π²Π°Π»Π΅Π½Π° Π² ΡΡΠ΅ΠΊ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π·Π°ΡΠ΅ΠΌ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΎ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠ΅Π΅ ΡΠΎΠ±ΡΡΠΈΠ΅. | |
|
135 | /// </summary> | |
|
136 | /// <param name="name">ΠΠΌΡ Π½Π°ΡΠΈΠ½Π°Π΅ΠΌΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
|
78 | 137 | public void StartLogicalOperation(string name) { |
|
79 | LogEvent(TraceEventType.OperationStarted, "{0}", name); | |
|
80 | 138 | m_currentOperation = new LogicalOperation(name, m_currentOperation); |
|
139 | LogEvent(TraceEventType.OperationStarted, name); | |
|
81 | 140 | } |
|
82 | 141 | |
|
142 | /// <summary> | |
|
143 | /// ΠΠ°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π½Π°ΡΠ°ΡΡΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π°ΡΠ°ΡΡΠ΅ Π² Π΄ΡΡΠ³ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°Ρ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Ρ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
144 | /// </summary> | |
|
145 | /// <remarks> | |
|
146 | /// ΠΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΆΡΡΠ½Π°Π»Π° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, Π»ΠΈΠ±ΠΎ ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π»ΠΈΠ±ΠΎ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ | |
|
147 | /// Π½Π°ΡΠ°ΡΠ° Π² Π΄ΡΡΠ³ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
148 | /// </remarks> | |
|
83 | 149 | public void EndLogicalOperation() { |
|
84 |
if (m_ |
|
|
150 | if (m_bound == m_currentOperation) { | |
|
85 | 151 | LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); |
|
86 | 152 | } else { |
|
87 | 153 | var op = m_currentOperation; |
|
154 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); | |
|
88 | 155 | m_currentOperation = m_currentOperation.Parent; |
|
89 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); | |
|
90 | 156 | } |
|
91 | 157 | } |
|
92 | 158 | |
|
93 | 159 | void LogEvent(TraceEventType type, string format, params object[] args) { |
|
94 | 160 | LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); |
|
95 | 161 | } |
|
96 | 162 | } |
|
97 | 163 | } |
@@ -1,27 +1,31 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Diagnostics { |
|
7 | 7 | public class TraceEvent { |
|
8 | 8 | public string Message { |
|
9 | 9 | get; |
|
10 | 10 | private set; |
|
11 | 11 | } |
|
12 | 12 | |
|
13 | 13 | public TraceEventType EventType { |
|
14 | 14 | get; |
|
15 | 15 | private set; |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | public TraceEvent(TraceEventType type, string message) { |
|
19 | 19 | EventType = type; |
|
20 | 20 | Message = message; |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | public override string ToString() { | |
|
24 | return String.Format("{0}: {1}", EventType, Message); | |
|
25 | } | |
|
26 | ||
|
23 | 27 | public static TraceEvent Create(TraceEventType type, string format, params object[] args) { |
|
24 | 28 | return new TraceEvent(type, String.Format(format, args)); |
|
25 | 29 | } |
|
26 | 30 | } |
|
27 | 31 | } |
@@ -1,53 +1,50 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Diagnostics; |
|
4 | 4 | using System.Linq; |
|
5 | 5 | using System.Text; |
|
6 | 6 | using System.Threading.Tasks; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Diagnostics { |
|
9 | 9 | /// <summary> |
|
10 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ ΠΏΡΠ±Π»ΠΈΠΊΠ°ΡΠΈΠΈ ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, ΡΠΎΠ±ΡΡΠΈΡ ΠΏΡΠ±Π»ΠΈΠΊΡΡΡΡΡ ΡΠ΅ΡΠ΅Π· <see cref="LogChannel{TraceEvent}"/> | |
|
10 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ ΠΏΡΠ±Π»ΠΈΠΊΠ°ΡΠΈΠΈ ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, ΡΠΎΠ±ΡΡΠΈΡ ΠΏΡΠ±Π»ΠΈΠΊΡΡΡΡΡ ΡΠ΅ΡΠ΅Π· <see cref="LogChannel{TraceEvent}"/>. | |
|
11 | /// ΠΡΡΠ½Π°Π» ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡΡΠ°ΠΆΠ°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠΉ Ρ ΠΎΠ΄ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΈ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ Π²ΡΠ΅Π³Π΄Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ΅ΡΠ½ΠΎ ΡΠ²ΡΠ·Π°Π½ Ρ | |
|
12 | /// ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠΎΠΌ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ. | |
|
11 | 13 | /// </summary> |
|
12 | 14 | public static class TraceLog { |
|
13 | 15 | [Conditional("TRACE")] |
|
14 | public static void Transfer(TraceContext from) { | |
|
15 | TraceContext.Transfer(from); | |
|
16 | } | |
|
17 | ||
|
18 | [Conditional("TRACE")] | |
|
19 | 16 | public static void StartLogicalOperation() { |
|
20 | 17 | TraceContext.Current.StartLogicalOperation(); |
|
21 | 18 | } |
|
22 | 19 | |
|
23 | 20 | [Conditional("TRACE")] |
|
24 | 21 | public static void StartLogicalOperation(string name) { |
|
25 | 22 | TraceContext.Current.StartLogicalOperation(name); |
|
26 | 23 | } |
|
27 | 24 | |
|
28 | 25 | [Conditional("TRACE")] |
|
29 | 26 | public static void EndLogicalOperation() { |
|
30 | 27 | TraceContext.Current.EndLogicalOperation(); |
|
31 | 28 | } |
|
32 | 29 | |
|
33 | 30 | [Conditional("TRACE")] |
|
34 | 31 | public static void TraceInformation(string format, params object[] arguments) { |
|
35 | 32 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Information, format, arguments)); |
|
36 | 33 | } |
|
37 | 34 | |
|
38 | 35 | [Conditional("TRACE")] |
|
39 | 36 | public static void TraceWarning(string format, params object[] arguments) { |
|
40 | 37 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Warning, format, arguments)); |
|
41 | 38 | } |
|
42 | 39 | |
|
43 | 40 | [Conditional("TRACE")] |
|
44 | 41 | public static void TraceError(string format, params object[] arguments) { |
|
45 | 42 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Error, format, arguments)); |
|
46 | 43 | } |
|
47 | 44 | |
|
48 | 45 | [Conditional("TRACE")] |
|
49 | 46 | public static void TraceError(Exception err) { |
|
50 | 47 | TraceError("{0}", err); |
|
51 | 48 | } |
|
52 | 49 | } |
|
53 | 50 | } |
@@ -1,63 +1,71 | |||
|
1 | 1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
3 | 3 | <PropertyGroup> |
|
4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
5 | 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
6 | 6 | <ProductVersion>10.0.0</ProductVersion> |
|
7 | 7 | <SchemaVersion>2.0</SchemaVersion> |
|
8 | 8 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
9 | 9 | <OutputType>Library</OutputType> |
|
10 | 10 | <RootNamespace>Implab</RootNamespace> |
|
11 | 11 | <AssemblyName>Implab</AssemblyName> |
|
12 | 12 | </PropertyGroup> |
|
13 | 13 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
14 | 14 | <DebugSymbols>true</DebugSymbols> |
|
15 | 15 | <DebugType>full</DebugType> |
|
16 | 16 | <Optimize>false</Optimize> |
|
17 | 17 | <OutputPath>bin\Debug</OutputPath> |
|
18 | 18 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
19 | 19 | <ErrorReport>prompt</ErrorReport> |
|
20 | 20 | <WarningLevel>4</WarningLevel> |
|
21 | 21 | <ConsolePause>false</ConsolePause> |
|
22 | 22 | </PropertyGroup> |
|
23 | 23 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
24 | 24 | <DebugType>full</DebugType> |
|
25 | 25 | <Optimize>true</Optimize> |
|
26 | 26 | <OutputPath>bin\Release</OutputPath> |
|
27 | 27 | <ErrorReport>prompt</ErrorReport> |
|
28 | 28 | <WarningLevel>4</WarningLevel> |
|
29 | 29 | <ConsolePause>false</ConsolePause> |
|
30 | 30 | </PropertyGroup> |
|
31 | 31 | <ItemGroup> |
|
32 | 32 | <Reference Include="System" /> |
|
33 | 33 | </ItemGroup> |
|
34 | 34 | <ItemGroup> |
|
35 | <Compile Include="Component.cs" /> | |
|
35 | 36 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
37 | <Compile Include="Diagnostics\EventText.cs" /> | |
|
38 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> | |
|
36 | 39 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
37 | 40 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
41 | <Compile Include="Diagnostics\TextFileListener.cs" /> | |
|
42 | <Compile Include="Diagnostics\TextListenerBase.cs" /> | |
|
38 | 43 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
39 | 44 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
40 | 45 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
41 | 46 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
47 | <Compile Include="Disposable.cs" /> | |
|
42 | 48 | <Compile Include="ICancellable.cs" /> |
|
43 | 49 | <Compile Include="IProgressHandler.cs" /> |
|
44 | 50 | <Compile Include="IProgressNotifier.cs" /> |
|
45 | 51 | <Compile Include="IPromise.cs" /> |
|
46 | 52 | <Compile Include="IPromiseBase.cs" /> |
|
53 | <Compile Include="IServiceLocator.cs" /> | |
|
47 | 54 | <Compile Include="ITaskController.cs" /> |
|
48 | 55 | <Compile Include="ManagedPromise.cs" /> |
|
49 | 56 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
50 | 57 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
51 | 58 | <Compile Include="Parallels\MTQueue.cs" /> |
|
52 | 59 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
60 | <Compile Include="ServiceLocator.cs" /> | |
|
53 | 61 | <Compile Include="TaskController.cs" /> |
|
54 | 62 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
55 | 63 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
56 | 64 | <Compile Include="Promise.cs" /> |
|
57 | 65 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
58 | 66 | <Compile Include="Safe.cs" /> |
|
59 | 67 | <Compile Include="ValueEventArgs.cs" /> |
|
60 | 68 | </ItemGroup> |
|
61 | 69 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
62 | 70 | <ItemGroup /> |
|
63 | 71 | </Project> No newline at end of file |
@@ -1,50 +1,50 | |||
|
1 | 1 | using Implab.Diagnostics; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Parallels { |
|
6 | 6 | /// <summary> |
|
7 | 7 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ ΡΠ°ΡΠΏΠ°ΡΠ°Π»Π΅Π»ΠΈΠ²Π°Π½ΠΈΡ Π·Π°Π΄Π°Ρ. |
|
8 | 8 | /// </summary> |
|
9 | 9 | /// <remarks> |
|
10 | 10 | /// ΠΡΠΏΠΎΠ»ΡΠ·ΡΡ Π΄Π°Π½Π½ΡΠΉ ΠΊΠ»Π°ΡΡ ΠΈ Π»ΡΠΌΠ΄Π° Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°ΡΠΏΠ°ΡΠ°Π»Π»Π΅Π»ΠΈΡΡ |
|
11 | 11 | /// Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ, Π΄Π»Ρ ΡΡΠΎΠ³ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΠΊΠΎΠ½ΡΠ΅ΠΏΡΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
12 | 12 | /// </remarks> |
|
13 | 13 | public static class AsyncPool { |
|
14 | 14 | |
|
15 | 15 | public static Promise<T> Invoke<T>(Func<T> func) { |
|
16 | 16 | var p = new Promise<T>(); |
|
17 |
var caller = TraceContext. |
|
|
17 | var caller = TraceContext.Snapshot(); | |
|
18 | 18 | |
|
19 | 19 | ThreadPool.QueueUserWorkItem(param => { |
|
20 |
Trace |
|
|
20 | TraceContext.Transfer(caller); | |
|
21 | 21 | try { |
|
22 | 22 | p.Resolve(func()); |
|
23 | 23 | } catch(Exception e) { |
|
24 | 24 | p.Reject(e); |
|
25 | 25 | } |
|
26 | 26 | }); |
|
27 | 27 | |
|
28 | 28 | return p; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | public static Promise<T> InvokeNewThread<T>(Func<T> func) { |
|
32 | 32 | var p = new Promise<T>(); |
|
33 | 33 | |
|
34 |
var caller = TraceContext. |
|
|
34 | var caller = TraceContext.Snapshot(); | |
|
35 | 35 | |
|
36 | 36 | var worker = new Thread(() => { |
|
37 |
Trace |
|
|
37 | TraceContext.Transfer(caller); | |
|
38 | 38 | try { |
|
39 | 39 | p.Resolve(func()); |
|
40 | 40 | } catch (Exception e) { |
|
41 | 41 | p.Reject(e); |
|
42 | 42 | } |
|
43 | 43 | }); |
|
44 | 44 | worker.IsBackground = true; |
|
45 | 45 | worker.Start(); |
|
46 | 46 | |
|
47 | 47 | return p; |
|
48 | 48 | } |
|
49 | 49 | } |
|
50 | 50 | } |
@@ -1,97 +1,98 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Diagnostics; |
|
7 | 7 | using Implab.Diagnostics; |
|
8 | 8 | |
|
9 | 9 | namespace Implab.Parallels { |
|
10 | 10 | public class WorkerPool : DispatchPool<Action> { |
|
11 | 11 | |
|
12 | 12 | MTQueue<Action> m_queue = new MTQueue<Action>(); |
|
13 | 13 | int m_queueLength = 0; |
|
14 | 14 | readonly int m_threshold = 1; |
|
15 | 15 | |
|
16 | 16 | public WorkerPool(int minThreads, int maxThreads, int threshold) |
|
17 | 17 | : base(minThreads, maxThreads) { |
|
18 | 18 | m_threshold = threshold; |
|
19 | 19 | InitPool(); |
|
20 | 20 | } |
|
21 | 21 | |
|
22 | 22 | public WorkerPool(int minThreads, int maxThreads) : |
|
23 | 23 | base(minThreads, maxThreads) { |
|
24 | 24 | InitPool(); |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | public WorkerPool(int threads) |
|
28 | 28 | : base(threads) { |
|
29 | 29 | InitPool(); |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | public WorkerPool() |
|
33 | 33 | : base() { |
|
34 | 34 | InitPool(); |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | public Promise<T> Invoke<T>(Func<T> task) { |
|
38 | 38 | if (task == null) |
|
39 | 39 | throw new ArgumentNullException("task"); |
|
40 | 40 | if (IsDisposed) |
|
41 | 41 | throw new ObjectDisposedException(ToString()); |
|
42 | 42 | |
|
43 | 43 | var promise = new Promise<T>(); |
|
44 | 44 | |
|
45 |
var caller = TraceContext. |
|
|
45 | var caller = TraceContext.Snapshot(); | |
|
46 | 46 | |
|
47 | 47 | EnqueueTask(delegate() { |
|
48 | TraceLog.Transfer(caller); | |
|
49 | try { | |
|
50 | promise.Resolve(task()); | |
|
51 | } catch (Exception e) { | |
|
52 | promise.Reject(e); | |
|
53 | } | |
|
48 | caller.Invoke(delegate() { | |
|
49 | try { | |
|
50 | promise.Resolve(task()); | |
|
51 | } catch (Exception e) { | |
|
52 | promise.Reject(e); | |
|
53 | } | |
|
54 | }); | |
|
54 | 55 | }); |
|
55 | 56 | |
|
56 | 57 | return promise; |
|
57 | 58 | } |
|
58 | 59 | |
|
59 | 60 | protected void EnqueueTask(Action unit) { |
|
60 | 61 | Debug.Assert(unit != null); |
|
61 | 62 | var len = Interlocked.Increment(ref m_queueLength); |
|
62 | 63 | m_queue.Enqueue(unit); |
|
63 | 64 | |
|
64 | 65 | if (len > m_threshold*ActiveThreads) |
|
65 | 66 | GrowPool(); |
|
66 | 67 | } |
|
67 | 68 | |
|
68 | 69 | protected override bool TryDequeue(out Action unit) { |
|
69 | 70 | if (m_queue.TryDequeue(out unit)) { |
|
70 | 71 | Interlocked.Decrement(ref m_queueLength); |
|
71 | 72 | return true; |
|
72 | 73 | } |
|
73 | 74 | return false; |
|
74 | 75 | } |
|
75 | 76 | |
|
76 | 77 | protected override bool Suspend() { |
|
77 | 78 | // This override solves race condition |
|
78 | 79 | // WORKER CLIENT |
|
79 | 80 | // --------------------------------------- |
|
80 | 81 | // TryDeque == false |
|
81 | 82 | // Enqueue(unit), queueLen++ |
|
82 | 83 | // GrowPool? == NO |
|
83 | 84 | // ActiveThreads-- |
|
84 | 85 | // Suspend |
|
85 | 86 | // queueLength > 0 |
|
86 | 87 | // continue |
|
87 | 88 | if (m_queueLength > 0) |
|
88 | 89 | return true; |
|
89 | 90 | return base.Suspend(); |
|
90 | 91 | } |
|
91 | 92 | |
|
92 | 93 | protected override void InvokeUnit(Action unit) { |
|
93 | 94 | unit(); |
|
94 | 95 | } |
|
95 | 96 | |
|
96 | 97 | } |
|
97 | 98 | } |
General Comments 0
You need to be logged in to leave comments.
Login now