annotate Implab/Diagnostics/TraceContext.cs @ 187:dd4a3590f9c6 ref20160224

Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler Any unhandled OperationCanceledException will cause the promise cancelation
author cin
date Tue, 19 Apr 2016 17:35:20 +0300
parents 04d4c92d0f28
children ea485487a424
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
1 using System;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
2 using System.Collections.Generic;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
3 using System.Threading;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
4
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
5 namespace Implab.Diagnostics {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
6 /// <summary>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
7 /// Trace context is bound to the specific thread, each thread has it's own ThreadContext.
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
8 /// </summary>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
9 /// <remarks>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
10 /// ThreadContext manages relations between logical operations and threads.
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
11 /// </remarks>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
12 public class TraceContext {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
13
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
14 [ThreadStatic]
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
15 static TraceContext _instance;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
16
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
17 OperationContext m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
18 readonly Stack<OperationContext> m_stack = new Stack<OperationContext>();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
19 readonly int m_threadId;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
20
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
21 public static TraceContext Instance {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
22 get {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
23 if (_instance == null)
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
24 _instance = new TraceContext();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
25 return _instance;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
26 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
27 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
28
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
29 public TraceContext() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
30 m_threadId = Thread.CurrentThread.ManagedThreadId;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
31 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
32
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
33 public int ThreadId {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
34 get { return m_threadId; }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
35 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
36
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
37 public LogicalOperation CurrentOperation {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
38 get {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
39 return m_current.CurrentOperation;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
40 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
41 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
42
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
43 public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) {
133
cin
parents: 119
diff changeset
44 //var prev = CurrentOperation;
134
04d4c92d0f28 Improved logging
cin
parents: 133
diff changeset
45 //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
46 m_stack.Push(m_current);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
47 m_current = new OperationContext(operation, takeOwnership);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
48 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
49
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
50 public void StartLogicalOperation(string name) {
134
04d4c92d0f28 Improved logging
cin
parents: 133
diff changeset
51 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, name));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
52 m_current.BeginLogicalOperation(name);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
53 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
54
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
55 public void StartLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
56 StartLogicalOperation(String.Empty);
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
57 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
58
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
59 public void EndLogicalOperation() {
134
04d4c92d0f28 Improved logging
cin
parents: 133
diff changeset
60 var op = m_current.EndLogicalOperation();
04d4c92d0f28 Improved logging
cin
parents: 133
diff changeset
61 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",op.Name, op.Duration)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
62 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
63
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
64 public LogicalOperation DetachLogicalOperation() {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
65 var prev = m_current.DetachLogicalOperation();
133
cin
parents: 119
diff changeset
66 //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name)));
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
67 return prev;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
68 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
69
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
70 public void Leave() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
71 if (m_stack.Count > 0) {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
72 m_current.Leave();
133
cin
parents: 119
diff changeset
73 //var prev = CurrentOperation;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
74 m_current = m_stack.Pop();
133
cin
parents: 119
diff changeset
75 //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name)));
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
76 } else {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 94
diff changeset
77 TraceLog.TraceWarning("Attempt to leave the last operation context");
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
78 m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
79 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
80 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
81 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
82 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
83