annotate Implab/Diagnostics/TraceContext.cs @ 92:4c0e5ef99986 v2

rewritten tracing
author cin
date Wed, 22 Oct 2014 18:37:56 +0400
parents c761fc982e1d
children dc4942d09e74
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) {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
44 // TODO Emit event
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
45 m_stack.Push(m_current);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
46 m_current = new OperationContext(operation, takeOwnership);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
47 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
48
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
49 public void StartLogicalOperation(string name) {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
50 m_current.BeginLogicalOperation(name);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
51 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
52
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
53 public void StartLogicalOperation() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
54 // TODO Emit Event
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
55 m_current.BeginLogicalOperation(String.Empty);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
56 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
57
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
58 public void EndLogicalOperation() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
59 // TODO Emit event
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
60 m_current.EndLogicalOperation();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
61 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
62
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
63 public LogicalOperation DetachLogicalOperation() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
64 // TODO Emit event
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
65 return m_current.DetachLogicalOperation();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
66 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
67
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
68 public void Leave() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
69 // TODO Emit event
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
70 if (m_stack.Count > 0)
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
71 m_current = m_stack.Pop();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
72 else {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
73 TraceLog.TraceWarning("Attemtp to leave the last operation context");
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
74 m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
75 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
76 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
77 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
78 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
79