##// END OF EJS Templates
sync
cin -
r133:6c49d02a9a05 v2
parent child
Show More
@@ -1,34 +1,34
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 ConsoleTraceListener: TextListenerBase {
8 8
9 9 static readonly object _consoleLock = new object();
10 10
11 11 public ConsoleTraceListener()
12 12 : base(true) {
13 13
14 14 }
15 15
16 16 public ConsoleTraceListener(bool global)
17 17 : base(global) {
18 18
19 19 }
20 20
21 21 protected override void WriteEntry(LogEventArgs args, EventText text, string channel) {
22 22 var msg = new StringBuilder();
23 23
24 24 for (int i = 0; i < text.indent; i++)
25 25 msg.Append(" ");
26 msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content);
26 msg.AppendFormat("[{0}]: {1}", args.ThreadId, text.content);
27 27
28 28 lock (_consoleLock) {
29 //Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1);
29 Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1);
30 30 Console.WriteLine(msg);
31 31 }
32 32 }
33 33 }
34 34 }
@@ -1,83 +1,83
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Threading;
4 4
5 5 namespace Implab.Diagnostics {
6 6 /// <summary>
7 7 /// Trace context is bound to the specific thread, each thread has it's own ThreadContext.
8 8 /// </summary>
9 9 /// <remarks>
10 10 /// ThreadContext manages relations between logical operations and threads.
11 11 /// </remarks>
12 12 public class TraceContext {
13 13
14 14 [ThreadStatic]
15 15 static TraceContext _instance;
16 16
17 17 OperationContext m_current = OperationContext.EMPTY;
18 18 readonly Stack<OperationContext> m_stack = new Stack<OperationContext>();
19 19 readonly int m_threadId;
20 20
21 21 public static TraceContext Instance {
22 22 get {
23 23 if (_instance == null)
24 24 _instance = new TraceContext();
25 25 return _instance;
26 26 }
27 27 }
28 28
29 29 public TraceContext() {
30 30 m_threadId = Thread.CurrentThread.ManagedThreadId;
31 31 }
32 32
33 33 public int ThreadId {
34 34 get { return m_threadId; }
35 35 }
36 36
37 37 public LogicalOperation CurrentOperation {
38 38 get {
39 39 return m_current.CurrentOperation;
40 40 }
41 41 }
42 42
43 43 public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) {
44 var prev = CurrentOperation;
44 //var prev = CurrentOperation;
45 45 m_stack.Push(m_current);
46 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 50 public void StartLogicalOperation(string name) {
51 51 m_current.BeginLogicalOperation(name);
52 52 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name)));
53 53 }
54 54
55 55 public void StartLogicalOperation() {
56 56 StartLogicalOperation(String.Empty);
57 57 }
58 58
59 59 public void EndLogicalOperation() {
60 60 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration)));
61 61 m_current.EndLogicalOperation();
62 62 }
63 63
64 64 public LogicalOperation DetachLogicalOperation() {
65 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 67 return prev;
68 68 }
69 69
70 70 public void Leave() {
71 71 if (m_stack.Count > 0) {
72 72 m_current.Leave();
73 var prev = CurrentOperation;
73 //var prev = CurrentOperation;
74 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 76 } else {
77 77 TraceLog.TraceWarning("Attempt to leave the last operation context");
78 78 m_current = OperationContext.EMPTY;
79 79 }
80 80 }
81 81 }
82 82 }
83 83
@@ -1,28 +1,29
1 1 using System;
2 2
3 3 namespace Implab.Diagnostics {
4 4 public class TraceEvent {
5 5 public string Message {
6 6 get;
7 7 private set;
8 8 }
9 9
10 10 public TraceEventType EventType {
11 11 get;
12 12 private set;
13 13 }
14 14
15 15 public TraceEvent(TraceEventType type, string message) {
16 16 EventType = type;
17 17 Message = message;
18 18 }
19 19
20 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 25 public static TraceEvent Create(TraceEventType type, string format, params object[] args) {
25 26 return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args));
26 27 }
27 28 }
28 29 }
@@ -1,114 +1,118
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Linq;
4 4 using System.Text;
5 5 using System.Text.RegularExpressions;
6 6 using System.Diagnostics;
7 7
8 8 namespace Implab
9 9 {
10 10 public static class Safe
11 11 {
12 12 public static void ArgumentMatch(string value, string paramName, Regex rx) {
13 13 if (rx == null)
14 14 throw new ArgumentNullException("rx");
15 15 if (!rx.IsMatch(value))
16 16 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
17 17 }
18 18
19 19 public static void ArgumentNotEmpty(string value, string paramName) {
20 20 if (String.IsNullOrEmpty(value))
21 21 throw new ArgumentException("The parameter can't be empty", paramName);
22 22 }
23 23
24 24 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
25 25 if (value == null || value.Length == 0)
26 26 throw new ArgumentException("The array must be not emty", paramName);
27 27 }
28 28
29 29 public static void ArgumentNotNull(object value, string paramName) {
30 30 if (value == null)
31 31 throw new ArgumentNullException(paramName);
32 32 }
33 33
34 34 public static void ArgumentInRange(int value, int min, int max, string paramName) {
35 35 if (value < min || value > max)
36 36 throw new ArgumentOutOfRangeException(paramName);
37 37 }
38 38
39 39 public static void Dispose(params IDisposable[] objects) {
40 40 foreach (var d in objects)
41 41 if (d != null)
42 42 d.Dispose();
43 43 }
44 44
45 45 public static void Dispose(params object[] objects) {
46 46 foreach (var obj in objects) {
47 47 var d = obj as IDisposable;
48 48 if (d != null)
49 49 d.Dispose();
50 50 }
51 51 }
52 52
53 53 public static void Dispose(object obj) {
54 54 var d = obj as IDisposable;
55 55 if (d != null)
56 56 d.Dispose();
57 57 }
58 58
59 59 [DebuggerStepThrough]
60 60 public static IPromise<T> WrapPromise<T>(Func<T> action) {
61 61 ArgumentNotNull(action, "action");
62 62
63 63 var p = new Promise<T>();
64 64 try {
65 65 p.Resolve(action());
66 66 } catch (Exception err) {
67 67 p.Reject(err);
68 68 }
69 69
70 70 return p;
71 71 }
72 72
73 73 [DebuggerStepThrough]
74 74 public static IPromise WrapPromise(Action action) {
75 75 ArgumentNotNull(action, "action");
76 76
77 77 var p = new Promise();
78 78 try {
79 79 action();
80 80 p.Resolve();
81 81 } catch (Exception err) {
82 82 p.Reject(err);
83 83 }
84 84
85 85 return p;
86 86 }
87 87
88 88 [DebuggerStepThrough]
89 89 public static IPromise InvokePromise(Func<IPromise> action) {
90 90 ArgumentNotNull(action, "action");
91 91
92 var p = new Promise();
93 92 try {
94 action();
95 p.Resolve();
93 var p = action();
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 100 } catch (Exception err) {
101 var p = new Promise();
97 102 p.Reject(err);
103 return p;
98 104 }
99
100 return p;
101 105 }
102 106
103 107 [DebuggerStepThrough]
104 108 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
105 109 ArgumentNotNull(action, "action");
106 110
107 111 try {
108 112 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null"));
109 113 } catch (Exception err) {
110 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