| @@ -1,34 +1,34 | |||||
| 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 |
|
5 | |||
| 6 | namespace Implab.Diagnostics { |
|
6 | namespace Implab.Diagnostics { | |
| 7 | public class ConsoleTraceListener: TextListenerBase { |
|
7 | public class ConsoleTraceListener: TextListenerBase { | |
| 8 |
|
8 | |||
| 9 | static readonly object _consoleLock = new object(); |
|
9 | static readonly object _consoleLock = new object(); | |
| 10 |
|
10 | |||
| 11 | public ConsoleTraceListener() |
|
11 | public ConsoleTraceListener() | |
| 12 | : base(true) { |
|
12 | : base(true) { | |
| 13 |
|
13 | |||
| 14 | } |
|
14 | } | |
| 15 |
|
15 | |||
| 16 | public ConsoleTraceListener(bool global) |
|
16 | public ConsoleTraceListener(bool global) | |
| 17 | : base(global) { |
|
17 | : base(global) { | |
| 18 |
|
18 | |||
| 19 | } |
|
19 | } | |
| 20 |
|
20 | |||
| 21 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { |
|
21 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
| 22 | var msg = new StringBuilder(); |
|
22 | var msg = new StringBuilder(); | |
| 23 |
|
23 | |||
| 24 | for (int i = 0; i < text.indent; i++) |
|
24 | for (int i = 0; i < text.indent; i++) | |
| 25 | msg.Append(" "); |
|
25 | msg.Append(" "); | |
| 26 |
msg.AppendFormat("[{0}]: |
|
26 | msg.AppendFormat("[{0}]: {1}", args.ThreadId, text.content); | |
| 27 |
|
27 | |||
| 28 | lock (_consoleLock) { |
|
28 | lock (_consoleLock) { | |
| 29 |
|
|
29 | Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1); | |
| 30 | Console.WriteLine(msg); |
|
30 | Console.WriteLine(msg); | |
| 31 | } |
|
31 | } | |
| 32 | } |
|
32 | } | |
| 33 | } |
|
33 | } | |
| 34 | } |
|
34 | } | |
| @@ -1,83 +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 | var prev = CurrentOperation; |
|
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 | //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name))); | |
| 48 | } |
|
48 | } | |
| 49 |
|
49 | |||
| 50 | public void StartLogicalOperation(string name) { |
|
50 | public void StartLogicalOperation(string name) { | |
| 51 | m_current.BeginLogicalOperation(name); |
|
51 | m_current.BeginLogicalOperation(name); | |
| 52 | 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))); | |
| 53 | } |
|
53 | } | |
| 54 |
|
54 | |||
| 55 | public void StartLogicalOperation() { |
|
55 | public void StartLogicalOperation() { | |
| 56 | StartLogicalOperation(String.Empty); |
|
56 | StartLogicalOperation(String.Empty); | |
| 57 | } |
|
57 | } | |
| 58 |
|
58 | |||
| 59 | public void EndLogicalOperation() { |
|
59 | public void EndLogicalOperation() { | |
| 60 | 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))); | |
| 61 | m_current.EndLogicalOperation(); |
|
61 | m_current.EndLogicalOperation(); | |
| 62 | } |
|
62 | } | |
| 63 |
|
63 | |||
| 64 | public LogicalOperation DetachLogicalOperation() { |
|
64 | public LogicalOperation DetachLogicalOperation() { | |
| 65 | var prev = m_current.DetachLogicalOperation(); |
|
65 | var prev = m_current.DetachLogicalOperation(); | |
| 66 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name))); |
|
66 | //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name))); | |
| 67 | return prev; |
|
67 | return prev; | |
| 68 | } |
|
68 | } | |
| 69 |
|
69 | |||
| 70 | public void Leave() { |
|
70 | public void Leave() { | |
| 71 | if (m_stack.Count > 0) { |
|
71 | if (m_stack.Count > 0) { | |
| 72 | m_current.Leave(); |
|
72 | m_current.Leave(); | |
| 73 | var prev = CurrentOperation; |
|
73 | //var prev = CurrentOperation; | |
| 74 | 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))); |
|
75 | //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name))); | |
| 76 | } else { |
|
76 | } else { | |
| 77 | TraceLog.TraceWarning("Attempt to leave the last operation context"); |
|
77 | TraceLog.TraceWarning("Attempt to leave the last operation context"); | |
| 78 | m_current = OperationContext.EMPTY; |
|
78 | m_current = OperationContext.EMPTY; | |
| 79 | } |
|
79 | } | |
| 80 | } |
|
80 | } | |
| 81 | } |
|
81 | } | |
| 82 | } |
|
82 | } | |
| 83 |
|
83 | |||
| @@ -1,28 +1,29 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 |
|
2 | |||
| 3 | namespace Implab.Diagnostics { |
|
3 | namespace Implab.Diagnostics { | |
| 4 | public class TraceEvent { |
|
4 | public class TraceEvent { | |
| 5 | public string Message { |
|
5 | public string Message { | |
| 6 | get; |
|
6 | get; | |
| 7 | private set; |
|
7 | private set; | |
| 8 | } |
|
8 | } | |
| 9 |
|
9 | |||
| 10 | public TraceEventType EventType { |
|
10 | public TraceEventType EventType { | |
| 11 | get; |
|
11 | get; | |
| 12 | private set; |
|
12 | private set; | |
| 13 | } |
|
13 | } | |
| 14 |
|
14 | |||
| 15 | public TraceEvent(TraceEventType type, string message) { |
|
15 | public TraceEvent(TraceEventType type, string message) { | |
| 16 | EventType = type; |
|
16 | EventType = type; | |
| 17 | Message = message; |
|
17 | Message = message; | |
| 18 | } |
|
18 | } | |
| 19 |
|
19 | |||
| 20 | public override string ToString() { |
|
20 | public override string ToString() { | |
| 21 | return EventType == TraceEventType.Information ? Message : String.Format("{0}: {1}", EventType, Message); |
|
21 | /*return EventType == TraceEventType.Information ? Message : String.Format("{0}: {1}", EventType, Message);*/ | |
|
|
22 | return Message; | |||
| 22 | } |
|
23 | } | |
| 23 |
|
24 | |||
| 24 | public static TraceEvent Create(TraceEventType type, string format, params object[] args) { |
|
25 | public static TraceEvent Create(TraceEventType type, string format, params object[] args) { | |
| 25 | return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args)); |
|
26 | return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args)); | |
| 26 | } |
|
27 | } | |
| 27 | } |
|
28 | } | |
| 28 | } |
|
29 | } | |
| @@ -1,114 +1,118 | |||||
| 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 value, string paramName, Regex rx) { |
|
12 | public static void ArgumentMatch(string value, string paramName, Regex rx) { | |
| 13 | if (rx == null) |
|
13 | if (rx == null) | |
| 14 | throw new ArgumentNullException("rx"); |
|
14 | throw new ArgumentNullException("rx"); | |
| 15 | if (!rx.IsMatch(value)) |
|
15 | if (!rx.IsMatch(value)) | |
| 16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); |
|
16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
| 17 | } |
|
17 | } | |
| 18 |
|
18 | |||
| 19 | public static void ArgumentNotEmpty(string value, string paramName) { |
|
19 | public static void ArgumentNotEmpty(string value, string paramName) { | |
| 20 | if (String.IsNullOrEmpty(value)) |
|
20 | if (String.IsNullOrEmpty(value)) | |
| 21 | throw new ArgumentException("The parameter can't be empty", paramName); |
|
21 | throw new ArgumentException("The parameter can't be empty", paramName); | |
| 22 | } |
|
22 | } | |
| 23 |
|
23 | |||
| 24 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
|
24 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { | |
| 25 | if (value == null || value.Length == 0) |
|
25 | if (value == null || value.Length == 0) | |
| 26 | throw new ArgumentException("The array must be not emty", paramName); |
|
26 | throw new ArgumentException("The array must be not emty", paramName); | |
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public static void ArgumentNotNull(object value, string paramName) { |
|
29 | public static void ArgumentNotNull(object value, string paramName) { | |
| 30 | if (value == null) |
|
30 | if (value == null) | |
| 31 | throw new ArgumentNullException(paramName); |
|
31 | throw new ArgumentNullException(paramName); | |
| 32 | } |
|
32 | } | |
| 33 |
|
33 | |||
| 34 | public static void ArgumentInRange(int value, int min, int max, string paramName) { |
|
34 | public static void ArgumentInRange(int value, int min, int max, string paramName) { | |
| 35 | if (value < min || value > max) |
|
35 | if (value < min || value > max) | |
| 36 | throw new ArgumentOutOfRangeException(paramName); |
|
36 | throw new ArgumentOutOfRangeException(paramName); | |
| 37 | } |
|
37 | } | |
| 38 |
|
38 | |||
| 39 | public static void Dispose(params IDisposable[] objects) { |
|
39 | public static void Dispose(params IDisposable[] objects) { | |
| 40 | foreach (var d in objects) |
|
40 | foreach (var d in objects) | |
| 41 | if (d != null) |
|
41 | if (d != null) | |
| 42 | d.Dispose(); |
|
42 | d.Dispose(); | |
| 43 | } |
|
43 | } | |
| 44 |
|
44 | |||
| 45 | public static void Dispose(params object[] objects) { |
|
45 | public static void Dispose(params object[] objects) { | |
| 46 | foreach (var obj in objects) { |
|
46 | foreach (var obj in objects) { | |
| 47 | var d = obj as IDisposable; |
|
47 | var d = obj as IDisposable; | |
| 48 | if (d != null) |
|
48 | if (d != null) | |
| 49 | d.Dispose(); |
|
49 | d.Dispose(); | |
| 50 | } |
|
50 | } | |
| 51 | } |
|
51 | } | |
| 52 |
|
52 | |||
| 53 | public static void Dispose(object obj) { |
|
53 | public static void Dispose(object obj) { | |
| 54 | var d = obj as IDisposable; |
|
54 | var d = obj as IDisposable; | |
| 55 | if (d != null) |
|
55 | if (d != null) | |
| 56 | d.Dispose(); |
|
56 | d.Dispose(); | |
| 57 | } |
|
57 | } | |
| 58 |
|
58 | |||
| 59 | [DebuggerStepThrough] |
|
59 | [DebuggerStepThrough] | |
| 60 | public static IPromise<T> WrapPromise<T>(Func<T> action) { |
|
60 | public static IPromise<T> WrapPromise<T>(Func<T> action) { | |
| 61 | ArgumentNotNull(action, "action"); |
|
61 | ArgumentNotNull(action, "action"); | |
| 62 |
|
62 | |||
| 63 | var p = new Promise<T>(); |
|
63 | var p = new Promise<T>(); | |
| 64 | try { |
|
64 | try { | |
| 65 | p.Resolve(action()); |
|
65 | p.Resolve(action()); | |
| 66 | } catch (Exception err) { |
|
66 | } catch (Exception err) { | |
| 67 | p.Reject(err); |
|
67 | p.Reject(err); | |
| 68 | } |
|
68 | } | |
| 69 |
|
69 | |||
| 70 | return p; |
|
70 | return p; | |
| 71 | } |
|
71 | } | |
| 72 |
|
72 | |||
| 73 | [DebuggerStepThrough] |
|
73 | [DebuggerStepThrough] | |
| 74 | public static IPromise WrapPromise(Action action) { |
|
74 | public static IPromise WrapPromise(Action action) { | |
| 75 | ArgumentNotNull(action, "action"); |
|
75 | ArgumentNotNull(action, "action"); | |
| 76 |
|
76 | |||
| 77 | var p = new Promise(); |
|
77 | var p = new Promise(); | |
| 78 | try { |
|
78 | try { | |
| 79 | action(); |
|
79 | action(); | |
| 80 | p.Resolve(); |
|
80 | p.Resolve(); | |
| 81 | } catch (Exception err) { |
|
81 | } catch (Exception err) { | |
| 82 | p.Reject(err); |
|
82 | p.Reject(err); | |
| 83 | } |
|
83 | } | |
| 84 |
|
84 | |||
| 85 | return p; |
|
85 | return p; | |
| 86 | } |
|
86 | } | |
| 87 |
|
87 | |||
| 88 | [DebuggerStepThrough] |
|
88 | [DebuggerStepThrough] | |
| 89 | public static IPromise InvokePromise(Func<IPromise> action) { |
|
89 | public static IPromise InvokePromise(Func<IPromise> action) { | |
| 90 | ArgumentNotNull(action, "action"); |
|
90 | ArgumentNotNull(action, "action"); | |
| 91 |
|
91 | |||
| 92 | var p = new Promise(); |
|
|||
| 93 | try { |
|
92 | try { | |
| 94 | action(); |
|
93 | var p = action(); | |
| 95 |
p |
|
94 | if (p == null) { | |
|
|
95 | var d = new Promise(); | |||
|
|
96 | d.Reject(new Exception("The action returned null")); | |||
|
|
97 | p = d; | |||
|
|
98 | } | |||
|
|
99 | return p; | |||
| 96 | } catch (Exception err) { |
|
100 | } catch (Exception err) { | |
|
|
101 | var p = new Promise(); | |||
| 97 | p.Reject(err); |
|
102 | p.Reject(err); | |
|
|
103 | return p; | |||
| 98 | } |
|
104 | } | |
| 99 |
|
||||
| 100 | return p; |
|
|||
| 101 | } |
|
105 | } | |
| 102 |
|
106 | |||
| 103 | [DebuggerStepThrough] |
|
107 | [DebuggerStepThrough] | |
| 104 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
|
108 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { | |
| 105 | ArgumentNotNull(action, "action"); |
|
109 | ArgumentNotNull(action, "action"); | |
| 106 |
|
110 | |||
| 107 | try { |
|
111 | try { | |
| 108 | return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); |
|
112 | return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); | |
| 109 | } catch (Exception err) { |
|
113 | } catch (Exception err) { | |
| 110 | return Promise<T>.ExceptionToPromise(err); |
|
114 | return Promise<T>.ExceptionToPromise(err); | |
| 111 | } |
|
115 | } | |
| 112 | } |
|
116 | } | |
| 113 | } |
|
117 | } | |
| 114 | } |
|
118 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
