92
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Threading;
|
|
4
|
|
5 namespace Implab.Diagnostics {
|
|
6 /// <summary>
|
|
7 /// Trace context is bound to the specific thread, each thread has it's own ThreadContext.
|
|
8 /// </summary>
|
|
9 /// <remarks>
|
|
10 /// ThreadContext manages relations between logical operations and threads.
|
|
11 /// </remarks>
|
|
12 public class TraceContext {
|
|
13
|
|
14 [ThreadStatic]
|
|
15 static TraceContext _instance;
|
|
16
|
|
17 OperationContext m_current = OperationContext.EMPTY;
|
|
18 readonly Stack<OperationContext> m_stack = new Stack<OperationContext>();
|
|
19 readonly int m_threadId;
|
|
20
|
|
21 public static TraceContext Instance {
|
|
22 get {
|
|
23 if (_instance == null)
|
|
24 _instance = new TraceContext();
|
|
25 return _instance;
|
|
26 }
|
|
27 }
|
|
28
|
|
29 public TraceContext() {
|
|
30 m_threadId = Thread.CurrentThread.ManagedThreadId;
|
|
31 }
|
|
32
|
|
33 public int ThreadId {
|
|
34 get { return m_threadId; }
|
|
35 }
|
|
36
|
|
37 public LogicalOperation CurrentOperation {
|
|
38 get {
|
|
39 return m_current.CurrentOperation;
|
|
40 }
|
|
41 }
|
|
42
|
|
43 public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) {
|
|
44 // TODO Emit event
|
|
45 m_stack.Push(m_current);
|
|
46 m_current = new OperationContext(operation, takeOwnership);
|
|
47 }
|
|
48
|
|
49 public void StartLogicalOperation(string name) {
|
|
50 m_current.BeginLogicalOperation(name);
|
|
51 }
|
|
52
|
|
53 public void StartLogicalOperation() {
|
|
54 // TODO Emit Event
|
|
55 m_current.BeginLogicalOperation(String.Empty);
|
|
56 }
|
|
57
|
|
58 public void EndLogicalOperation() {
|
|
59 // TODO Emit event
|
|
60 m_current.EndLogicalOperation();
|
|
61 }
|
|
62
|
|
63 public LogicalOperation DetachLogicalOperation() {
|
|
64 // TODO Emit event
|
|
65 return m_current.DetachLogicalOperation();
|
|
66 }
|
|
67
|
|
68 public void Leave() {
|
|
69 // TODO Emit event
|
|
70 if (m_stack.Count > 0)
|
|
71 m_current = m_stack.Pop();
|
|
72 else {
|
|
73 TraceLog.TraceWarning("Attemtp to leave the last operation context");
|
|
74 m_current = OperationContext.EMPTY;
|
|
75 }
|
|
76 }
|
|
77 }
|
|
78 }
|
|
79
|