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