| @@ -1,15 +1,26 | |||||
| 1 | namespace Implab.Diagnostics { |
|
1 | namespace Implab.Diagnostics { | |
| 2 | public static class Extensions { |
|
2 | public static class Extensions { | |
| 3 | public static IPromise<T> EndLogicalOperation<T>(this IPromise<T> promise) { |
|
3 | public static IPromise<T> EndLogicalOperation<T>(this IPromise<T> promise) { | |
| 4 | Safe.ArgumentNotNull(promise, "promise"); |
|
4 | Safe.ArgumentNotNull(promise, "promise"); | |
| 5 | var op = TraceContext.Instance.DetachLogicalOperation(); |
|
5 | var op = TraceContext.Instance.DetachLogicalOperation(); | |
| 6 |
|
6 | |||
| 7 | return promise.Anyway(() => { |
|
7 | return promise.Anyway(() => { | |
| 8 | TraceContext.Instance.EnterLogicalOperation(op,true); |
|
8 | TraceContext.Instance.EnterLogicalOperation(op,true); | |
| 9 | TraceLog.EndLogicalOperation(); |
|
9 | TraceLog.EndLogicalOperation(); | |
| 10 | TraceContext.Instance.Leave(); |
|
10 | TraceContext.Instance.Leave(); | |
| 11 | }); |
|
11 | }); | |
| 12 | } |
|
12 | } | |
|
|
13 | ||||
|
|
14 | public static IPromise EndLogicalOperation(this IPromise promise) { | |||
|
|
15 | Safe.ArgumentNotNull(promise, "promise"); | |||
|
|
16 | var op = TraceContext.Instance.DetachLogicalOperation(); | |||
|
|
17 | ||||
|
|
18 | return promise.Anyway(() => { | |||
|
|
19 | TraceContext.Instance.EnterLogicalOperation(op,true); | |||
|
|
20 | TraceLog.EndLogicalOperation(); | |||
|
|
21 | TraceContext.Instance.Leave(); | |||
|
|
22 | }); | |||
|
|
23 | } | |||
| 13 | } |
|
24 | } | |
| 14 | } |
|
25 | } | |
| 15 |
|
26 | |||
| @@ -1,80 +1,83 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System.Threading; |
|
3 | using System.Threading; | |
| 4 |
|
4 | |||
| 5 | namespace Implab.Diagnostics { |
|
5 | namespace Implab.Diagnostics { | |
| 6 | /// <summary> |
|
6 | /// <summary> | |
| 7 | /// Trace context is bound to the specific thread, each thread has it's own ThreadContext. |
|
7 | /// Trace context is bound to the specific thread, each thread has it's own ThreadContext. | |
| 8 | /// </summary> |
|
8 | /// </summary> | |
| 9 | /// <remarks> |
|
9 | /// <remarks> | |
| 10 | /// ThreadContext manages relations between logical operations and threads. |
|
10 | /// ThreadContext manages relations between logical operations and threads. | |
| 11 | /// </remarks> |
|
11 | /// </remarks> | |
| 12 | public class TraceContext { |
|
12 | public class TraceContext { | |
| 13 |
|
13 | |||
| 14 | [ThreadStatic] |
|
14 | [ThreadStatic] | |
| 15 | static TraceContext _instance; |
|
15 | static TraceContext _instance; | |
| 16 |
|
16 | |||
| 17 | OperationContext m_current = OperationContext.EMPTY; |
|
17 | OperationContext m_current = OperationContext.EMPTY; | |
| 18 | readonly Stack<OperationContext> m_stack = new Stack<OperationContext>(); |
|
18 | readonly Stack<OperationContext> m_stack = new Stack<OperationContext>(); | |
| 19 | readonly int m_threadId; |
|
19 | readonly int m_threadId; | |
| 20 |
|
20 | |||
| 21 | public static TraceContext Instance { |
|
21 | public static TraceContext Instance { | |
| 22 | get { |
|
22 | get { | |
| 23 | if (_instance == null) |
|
23 | if (_instance == null) | |
| 24 | _instance = new TraceContext(); |
|
24 | _instance = new TraceContext(); | |
| 25 | return _instance; |
|
25 | return _instance; | |
| 26 | } |
|
26 | } | |
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public TraceContext() { |
|
29 | public TraceContext() { | |
| 30 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
30 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |
| 31 | } |
|
31 | } | |
| 32 |
|
32 | |||
| 33 | public int ThreadId { |
|
33 | public int ThreadId { | |
| 34 | get { return m_threadId; } |
|
34 | get { return m_threadId; } | |
| 35 | } |
|
35 | } | |
| 36 |
|
36 | |||
| 37 | public LogicalOperation CurrentOperation { |
|
37 | public LogicalOperation CurrentOperation { | |
| 38 | get { |
|
38 | get { | |
| 39 | return m_current.CurrentOperation; |
|
39 | return m_current.CurrentOperation; | |
| 40 | } |
|
40 | } | |
| 41 | } |
|
41 | } | |
| 42 |
|
42 | |||
| 43 | public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { |
|
43 | public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { | |
| 44 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Attach, String.Format("{0} -> [{1}]", operation.Name, m_threadId))); |
|
44 | var prev = CurrentOperation; | |
| 45 | m_stack.Push(m_current); |
|
45 | m_stack.Push(m_current); | |
| 46 | m_current = new OperationContext(operation, takeOwnership); |
|
46 | m_current = new OperationContext(operation, takeOwnership); | |
|
|
47 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name))); | |||
| 47 | } |
|
48 | } | |
| 48 |
|
49 | |||
| 49 | public void StartLogicalOperation(string name) { |
|
50 | public void StartLogicalOperation(string name) { | |
| 50 | m_current.BeginLogicalOperation(name); |
|
51 | m_current.BeginLogicalOperation(name); | |
| 51 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name))); |
|
52 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name))); | |
| 52 | } |
|
53 | } | |
| 53 |
|
54 | |||
| 54 | public void StartLogicalOperation() { |
|
55 | public void StartLogicalOperation() { | |
| 55 | StartLogicalOperation(String.Empty); |
|
56 | StartLogicalOperation(String.Empty); | |
| 56 | } |
|
57 | } | |
| 57 |
|
58 | |||
| 58 | public void EndLogicalOperation() { |
|
59 | public void EndLogicalOperation() { | |
| 59 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration))); |
|
60 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration))); | |
| 60 | m_current.EndLogicalOperation(); |
|
61 | m_current.EndLogicalOperation(); | |
| 61 | } |
|
62 | } | |
| 62 |
|
63 | |||
| 63 | public LogicalOperation DetachLogicalOperation() { |
|
64 | public LogicalOperation DetachLogicalOperation() { | |
| 64 |
var |
|
65 | var prev = m_current.DetachLogicalOperation(); | |
| 65 |
LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format(" |
|
66 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name))); | |
| 66 |
return |
|
67 | return prev; | |
| 67 | } |
|
68 | } | |
| 68 |
|
69 | |||
| 69 | public void Leave() { |
|
70 | public void Leave() { | |
| 70 | if (m_stack.Count > 0) { |
|
71 | if (m_stack.Count > 0) { | |
| 71 | m_current.Leave(); |
|
72 | m_current.Leave(); | |
|
|
73 | var prev = CurrentOperation; | |||
| 72 | m_current = m_stack.Pop(); |
|
74 | m_current = m_stack.Pop(); | |
|
|
75 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name))); | |||
| 73 | } else { |
|
76 | } else { | |
| 74 | TraceLog.TraceWarning("Attemtp to leave the last operation context"); |
|
77 | TraceLog.TraceWarning("Attemtp to leave the last operation context"); | |
| 75 | m_current = OperationContext.EMPTY; |
|
78 | m_current = OperationContext.EMPTY; | |
| 76 | } |
|
79 | } | |
| 77 | } |
|
80 | } | |
| 78 | } |
|
81 | } | |
| 79 | } |
|
82 | } | |
| 80 |
|
83 | |||
| @@ -1,17 +1,19 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System.Linq; |
|
3 | using System.Linq; | |
| 4 | using System.Text; |
|
4 | using System.Text; | |
| 5 | using System.Threading.Tasks; |
|
5 | using System.Threading.Tasks; | |
| 6 |
|
6 | |||
| 7 | namespace Implab.Diagnostics { |
|
7 | namespace Implab.Diagnostics { | |
| 8 | public enum TraceEventType { |
|
8 | public enum TraceEventType { | |
| 9 | Information = 1, |
|
9 | Information = 1, | |
| 10 | Warning, |
|
10 | Warning, | |
| 11 | Error, |
|
11 | Error, | |
| 12 | OperationStarted, |
|
12 | OperationStarted, | |
| 13 | OperationCompleted, |
|
13 | OperationCompleted, | |
| 14 | Attach, |
|
14 | Attach, | |
| 15 | Detach, |
|
15 | Detach, | |
|
|
16 | Enter, | |||
|
|
17 | Leave | |||
| 16 | } |
|
18 | } | |
| 17 | } |
|
19 | } | |
| @@ -1,43 +1,40 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
|||
| 3 | using System.Linq; |
|
|||
| 4 | using System.Text; |
|
|||
| 5 |
|
2 | |||
| 6 | namespace Implab { |
|
3 | namespace Implab { | |
| 7 | public interface IPromise<T> : IPromise { |
|
4 | public interface IPromise<T> : IPromise { | |
| 8 |
|
5 | |||
| 9 | new T Join(); |
|
6 | new T Join(); | |
| 10 |
|
7 | |||
| 11 | new T Join(int timeout); |
|
8 | new T Join(int timeout); | |
| 12 |
|
9 | |||
| 13 | void Last(ResultHandler<T> success, ErrorHandler error, Action cancel); |
|
10 | void Last(ResultHandler<T> success, ErrorHandler error, Action cancel); | |
| 14 |
|
11 | |||
| 15 | void Last(ResultHandler<T> success, ErrorHandler error); |
|
12 | void Last(ResultHandler<T> success, ErrorHandler error); | |
| 16 |
|
13 | |||
| 17 | void Last(ResultHandler<T> success); |
|
14 | void Last(ResultHandler<T> success); | |
| 18 |
|
15 | |||
| 19 | IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel); |
|
16 | IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel); | |
| 20 |
|
17 | |||
| 21 | IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error); |
|
18 | IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error); | |
| 22 |
|
19 | |||
| 23 | IPromise<T> Then(ResultHandler<T> success); |
|
20 | IPromise<T> Then(ResultHandler<T> success); | |
| 24 |
|
21 | |||
| 25 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper, ErrorHandler<T2> error, Action cancel); |
|
22 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper, ErrorHandler<T2> error, Action cancel); | |
| 26 |
|
23 | |||
| 27 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper, ErrorHandler<T2> error); |
|
24 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper, ErrorHandler<T2> error); | |
| 28 |
|
25 | |||
| 29 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper); |
|
26 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper); | |
| 30 |
|
27 | |||
| 31 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained, ErrorHandler<IPromise<T2>> error, Action cancel); |
|
28 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained, ErrorHandler<IPromise<T2>> error, Action cancel); | |
| 32 |
|
29 | |||
| 33 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained, ErrorHandler<IPromise<T2>> error); |
|
30 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained, ErrorHandler<IPromise<T2>> error); | |
| 34 |
|
31 | |||
| 35 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained); |
|
32 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained); | |
| 36 |
|
33 | |||
| 37 | IPromise<T> Error(ErrorHandler<T> error); |
|
34 | IPromise<T> Error(ErrorHandler<T> error); | |
| 38 |
|
35 | |||
| 39 | new IPromise<T> Cancelled(Action handler); |
|
36 | new IPromise<T> Cancelled(Action handler); | |
| 40 |
|
37 | |||
| 41 | new IPromise<T> Anyway(Action handler); |
|
38 | new IPromise<T> Anyway(Action handler); | |
| 42 | } |
|
39 | } | |
| 43 | } |
|
40 | } | |
| @@ -1,71 +1,71 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System.Linq; |
|
3 | using System.Linq; | |
| 4 | using System.Text; |
|
4 | using System.Text; | |
| 5 | using System.Text.RegularExpressions; |
|
5 | using System.Text.RegularExpressions; | |
| 6 | using System.Diagnostics; |
|
6 | using System.Diagnostics; | |
| 7 |
|
7 | |||
| 8 | namespace Implab |
|
8 | namespace Implab | |
| 9 | { |
|
9 | { | |
| 10 | public static class Safe |
|
10 | public static class Safe | |
| 11 | { |
|
11 | { | |
| 12 | public static void ArgumentMatch(string param, string name, Regex rx) { |
|
12 | public static void ArgumentMatch(string param, string name, Regex rx) { | |
| 13 | if (rx == null) |
|
13 | if (rx == null) | |
| 14 | throw new ArgumentNullException("rx"); |
|
14 | throw new ArgumentNullException("rx"); | |
| 15 | if (!rx.IsMatch(param)) |
|
15 | if (!rx.IsMatch(param)) | |
| 16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), name); |
|
16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), name); | |
| 17 | } |
|
17 | } | |
| 18 |
|
18 | |||
| 19 | public static void ArgumentNotEmpty(string param, string name) { |
|
19 | public static void ArgumentNotEmpty(string param, string name) { | |
| 20 | if (String.IsNullOrEmpty(param)) |
|
20 | if (String.IsNullOrEmpty(param)) | |
| 21 | throw new ArgumentException("The parameter can't be empty", name); |
|
21 | throw new ArgumentException("The parameter can't be empty", name); | |
| 22 | } |
|
22 | } | |
| 23 |
|
23 | |||
| 24 | public static void ArgumentNotEmpty<T>(T[] param, string name) { |
|
24 | public static void ArgumentNotEmpty<T>(T[] param, string name) { | |
| 25 | if (param == null || param.Length == 0) |
|
25 | if (param == null || param.Length == 0) | |
| 26 | throw new ArgumentException("The array must be not emty"); |
|
26 | throw new ArgumentException("The array must be not emty"); | |
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public static void ArgumentNotNull(object param, string name) { |
|
29 | public static void ArgumentNotNull(object param, string name) { | |
| 30 | if (param == null) |
|
30 | if (param == null) | |
| 31 | throw new ArgumentNullException(name); |
|
31 | throw new ArgumentNullException(name); | |
| 32 | } |
|
32 | } | |
| 33 |
|
33 | |||
| 34 | public static void ArgumentInRange(int arg, int min, int max, string name) { |
|
34 | public static void ArgumentInRange(int arg, int min, int max, string name) { | |
| 35 | if (arg < min || arg > max) |
|
35 | if (arg < min || arg > max) | |
| 36 | throw new ArgumentOutOfRangeException(name); |
|
36 | throw new ArgumentOutOfRangeException(name); | |
| 37 | } |
|
37 | } | |
| 38 |
|
38 | |||
| 39 | public static void Dispose<T>(T obj) where T : class |
|
39 | public static void Dispose<T>(T obj) where T : class | |
| 40 | { |
|
40 | { | |
| 41 | var disp = obj as IDisposable; |
|
41 | var disp = obj as IDisposable; | |
| 42 | if (disp != null) |
|
42 | if (disp != null) | |
| 43 | disp.Dispose(); |
|
43 | disp.Dispose(); | |
| 44 | } |
|
44 | } | |
| 45 |
|
45 | |||
| 46 | [DebuggerStepThrough] |
|
46 | [DebuggerStepThrough] | |
| 47 |
public static IPromise<T> |
|
47 | public static IPromise<T> InvokePromise<T>(Func<T> action) { | |
| 48 | ArgumentNotNull(action, "action"); |
|
48 | ArgumentNotNull(action, "action"); | |
| 49 |
|
49 | |||
| 50 | var p = new Promise<T>(); |
|
50 | var p = new Promise<T>(); | |
| 51 | try { |
|
51 | try { | |
| 52 | p.Resolve(action()); |
|
52 | p.Resolve(action()); | |
| 53 | } catch (Exception err) { |
|
53 | } catch (Exception err) { | |
| 54 | p.Reject(err); |
|
54 | p.Reject(err); | |
| 55 | } |
|
55 | } | |
| 56 |
|
56 | |||
| 57 | return p; |
|
57 | return p; | |
| 58 | } |
|
58 | } | |
| 59 |
|
59 | |||
| 60 | [DebuggerStepThrough] |
|
60 | [DebuggerStepThrough] | |
| 61 |
public static IPromise<T> |
|
61 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { | |
| 62 | ArgumentNotNull(action, "action"); |
|
62 | ArgumentNotNull(action, "action"); | |
| 63 |
|
63 | |||
| 64 | try { |
|
64 | try { | |
| 65 | return action(); |
|
65 | return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); | |
| 66 | } catch (Exception err) { |
|
66 | } catch (Exception err) { | |
| 67 | return Promise<T>.ExceptionToPromise(err); |
|
67 | return Promise<T>.ExceptionToPromise(err); | |
| 68 | } |
|
68 | } | |
| 69 | } |
|
69 | } | |
| 70 | } |
|
70 | } | |
| 71 | } |
|
71 | } | |
| @@ -1,31 +1,32 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using Implab.Diagnostics; |
|
2 | using Implab.Diagnostics; | |
| 3 | using Implab.Parallels; |
|
3 | using Implab.Parallels; | |
| 4 | using Implab; |
|
4 | using Implab; | |
| 5 |
|
5 | |||
| 6 | namespace MonoPlay { |
|
6 | namespace MonoPlay { | |
| 7 | class MainClass { |
|
7 | class MainClass { | |
| 8 | public static void Main(string[] args) { |
|
8 | public static void Main(string[] args) { | |
|
|
9 | if (args == null) | |||
|
|
10 | throw new ArgumentNullException("args"); | |||
|
|
11 | ||||
| 9 | var listener = new ConsoleTraceListener(true); |
|
12 | var listener = new ConsoleTraceListener(true); | |
| 10 | listener.Subscribe<TraceEvent>(); |
|
13 | listener.Subscribe<TraceEvent>(); | |
| 11 |
|
14 | |||
| 12 | MTComponentContainer.AppContainer.Add(listener); |
|
15 | MTComponentContainer.AppContainer.Add(listener); | |
| 13 |
|
16 | |||
| 14 | TraceLog.StartLogicalOperation("program"); |
|
17 | TraceLog.StartLogicalOperation("program"); | |
| 15 |
|
18 | |||
| 16 | Console.WriteLine("Hello World!"); |
|
|||
| 17 |
|
||||
| 18 | TraceLog.StartLogicalOperation("async"); |
|
19 | TraceLog.StartLogicalOperation("async"); | |
| 19 | AsyncPool.Invoke(() => { |
|
20 | AsyncPool.Invoke(() => { | |
| 20 | TraceLog.TraceInformation("Hello async"); |
|
21 | TraceLog.TraceInformation("Hello async"); | |
| 21 | TraceLog.StartLogicalOperation(); |
|
22 | TraceLog.StartLogicalOperation("foo"); | |
| 22 | return 0; |
|
23 | return 0; | |
| 23 | }) |
|
24 | }) | |
| 24 | .EndLogicalOperation() |
|
25 | .EndLogicalOperation() | |
| 25 | .Join(); |
|
26 | .Join(); | |
| 26 |
|
27 | |||
| 27 | TraceLog.EndLogicalOperation(); |
|
28 | TraceLog.EndLogicalOperation(); | |
| 28 |
|
29 | |||
| 29 | } |
|
30 | } | |
| 30 | } |
|
31 | } | |
| 31 | } |
|
32 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
