annotate Implab/Diagnostics/LogContext.cs @ 35:2880242f987a diagnostics

initial log capabilities
author cin
date Mon, 14 Apr 2014 18:25:26 +0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
2880242f987a initial log capabilities
cin
parents:
diff changeset
1 using System;
2880242f987a initial log capabilities
cin
parents:
diff changeset
2 using System.Collections.Generic;
2880242f987a initial log capabilities
cin
parents:
diff changeset
3 using System.Linq;
2880242f987a initial log capabilities
cin
parents:
diff changeset
4 using System.Text;
2880242f987a initial log capabilities
cin
parents:
diff changeset
5 using System.Threading;
2880242f987a initial log capabilities
cin
parents:
diff changeset
6 using System.Threading.Tasks;
2880242f987a initial log capabilities
cin
parents:
diff changeset
7
2880242f987a initial log capabilities
cin
parents:
diff changeset
8 namespace Implab.Diagnostics {
2880242f987a initial log capabilities
cin
parents:
diff changeset
9 public class LogContext {
2880242f987a initial log capabilities
cin
parents:
diff changeset
10 LogicalOperation m_currentOperation;
2880242f987a initial log capabilities
cin
parents:
diff changeset
11 readonly LogicalOperation m_traceBound;
2880242f987a initial log capabilities
cin
parents:
diff changeset
12 readonly int m_threadId;
2880242f987a initial log capabilities
cin
parents:
diff changeset
13 readonly LogContext m_parent;
2880242f987a initial log capabilities
cin
parents:
diff changeset
14
2880242f987a initial log capabilities
cin
parents:
diff changeset
15 readonly static object _consoleLock = new object();
2880242f987a initial log capabilities
cin
parents:
diff changeset
16
2880242f987a initial log capabilities
cin
parents:
diff changeset
17 [ThreadStatic]
2880242f987a initial log capabilities
cin
parents:
diff changeset
18 static LogContext _current;
2880242f987a initial log capabilities
cin
parents:
diff changeset
19
2880242f987a initial log capabilities
cin
parents:
diff changeset
20 public static LogContext Current {
2880242f987a initial log capabilities
cin
parents:
diff changeset
21 get {
2880242f987a initial log capabilities
cin
parents:
diff changeset
22 if (_current == null)
2880242f987a initial log capabilities
cin
parents:
diff changeset
23 _current = new LogContext();
2880242f987a initial log capabilities
cin
parents:
diff changeset
24 return _current;
2880242f987a initial log capabilities
cin
parents:
diff changeset
25 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
26 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
27
2880242f987a initial log capabilities
cin
parents:
diff changeset
28 LogContext(LogContext context) {
2880242f987a initial log capabilities
cin
parents:
diff changeset
29 if (context == null)
2880242f987a initial log capabilities
cin
parents:
diff changeset
30 throw new ArgumentNullException("context");
2880242f987a initial log capabilities
cin
parents:
diff changeset
31
2880242f987a initial log capabilities
cin
parents:
diff changeset
32 m_parent = context;
2880242f987a initial log capabilities
cin
parents:
diff changeset
33 m_currentOperation = context.CurrentOperation;
2880242f987a initial log capabilities
cin
parents:
diff changeset
34 m_traceBound = context.CurrentOperation;
2880242f987a initial log capabilities
cin
parents:
diff changeset
35 m_threadId = Thread.CurrentThread.ManagedThreadId;
2880242f987a initial log capabilities
cin
parents:
diff changeset
36
2880242f987a initial log capabilities
cin
parents:
diff changeset
37 TraceEvent(TraceEventType.Transfer, String.Empty);
2880242f987a initial log capabilities
cin
parents:
diff changeset
38 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
39
2880242f987a initial log capabilities
cin
parents:
diff changeset
40 LogContext() {
2880242f987a initial log capabilities
cin
parents:
diff changeset
41 m_currentOperation = new LogicalOperation();
2880242f987a initial log capabilities
cin
parents:
diff changeset
42 m_traceBound = m_currentOperation;
2880242f987a initial log capabilities
cin
parents:
diff changeset
43 m_threadId = Thread.CurrentThread.ManagedThreadId;
2880242f987a initial log capabilities
cin
parents:
diff changeset
44 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
45
2880242f987a initial log capabilities
cin
parents:
diff changeset
46 public static void Transfer(LogContext from) {
2880242f987a initial log capabilities
cin
parents:
diff changeset
47 _current = from == null ? new LogContext() : new LogContext(from);
2880242f987a initial log capabilities
cin
parents:
diff changeset
48 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
49
2880242f987a initial log capabilities
cin
parents:
diff changeset
50 public LogContext ParentContext {
2880242f987a initial log capabilities
cin
parents:
diff changeset
51 get {
2880242f987a initial log capabilities
cin
parents:
diff changeset
52 return m_parent;
2880242f987a initial log capabilities
cin
parents:
diff changeset
53 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
54 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
55
2880242f987a initial log capabilities
cin
parents:
diff changeset
56 public LogicalOperation CurrentOperation {
2880242f987a initial log capabilities
cin
parents:
diff changeset
57 get {
2880242f987a initial log capabilities
cin
parents:
diff changeset
58 return m_currentOperation;
2880242f987a initial log capabilities
cin
parents:
diff changeset
59 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
60 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
61
2880242f987a initial log capabilities
cin
parents:
diff changeset
62 public LogicalOperation TraceBound {
2880242f987a initial log capabilities
cin
parents:
diff changeset
63 get {
2880242f987a initial log capabilities
cin
parents:
diff changeset
64 return m_traceBound;
2880242f987a initial log capabilities
cin
parents:
diff changeset
65 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
66 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
67
2880242f987a initial log capabilities
cin
parents:
diff changeset
68 public int ThreadId {
2880242f987a initial log capabilities
cin
parents:
diff changeset
69 get {
2880242f987a initial log capabilities
cin
parents:
diff changeset
70 return m_threadId;
2880242f987a initial log capabilities
cin
parents:
diff changeset
71 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
72 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
73
2880242f987a initial log capabilities
cin
parents:
diff changeset
74 public void StartLogicalOperation() {
2880242f987a initial log capabilities
cin
parents:
diff changeset
75 StartLogicalOperation(null);
2880242f987a initial log capabilities
cin
parents:
diff changeset
76 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
77
2880242f987a initial log capabilities
cin
parents:
diff changeset
78 public void StartLogicalOperation(string name) {
2880242f987a initial log capabilities
cin
parents:
diff changeset
79 TraceEvent(TraceEventType.OperationStarted, "{0}", name);
2880242f987a initial log capabilities
cin
parents:
diff changeset
80 m_currentOperation = new LogicalOperation(name, m_currentOperation);
2880242f987a initial log capabilities
cin
parents:
diff changeset
81 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
82
2880242f987a initial log capabilities
cin
parents:
diff changeset
83 public void EndLogicalOperation() {
2880242f987a initial log capabilities
cin
parents:
diff changeset
84 if (m_traceBound == m_currentOperation) {
2880242f987a initial log capabilities
cin
parents:
diff changeset
85 TraceEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace");
2880242f987a initial log capabilities
cin
parents:
diff changeset
86 } else {
2880242f987a initial log capabilities
cin
parents:
diff changeset
87 var op = m_currentOperation;
2880242f987a initial log capabilities
cin
parents:
diff changeset
88 m_currentOperation = m_currentOperation.Parent;
2880242f987a initial log capabilities
cin
parents:
diff changeset
89 TraceEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration);
2880242f987a initial log capabilities
cin
parents:
diff changeset
90 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
91 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
92
2880242f987a initial log capabilities
cin
parents:
diff changeset
93 public void TraceEvent(TraceEventType type, string format, params object[] args) {
2880242f987a initial log capabilities
cin
parents:
diff changeset
94 /*var msg = new StringBuilder();
2880242f987a initial log capabilities
cin
parents:
diff changeset
95 for (int i = 0; i < CurrentOperation.Level; i++)
2880242f987a initial log capabilities
cin
parents:
diff changeset
96 msg.Append(" ");
2880242f987a initial log capabilities
cin
parents:
diff changeset
97 msg.Append(type);
2880242f987a initial log capabilities
cin
parents:
diff changeset
98 msg.AppendFormat("[{0}]: ",m_threadId);
2880242f987a initial log capabilities
cin
parents:
diff changeset
99 msg.AppendFormat(format, args);
2880242f987a initial log capabilities
cin
parents:
diff changeset
100
2880242f987a initial log capabilities
cin
parents:
diff changeset
101 lock (_consoleLock) {
2880242f987a initial log capabilities
cin
parents:
diff changeset
102 Console.ForegroundColor = (ConsoleColor)(m_threadId % 15 + 1);
2880242f987a initial log capabilities
cin
parents:
diff changeset
103 Console.WriteLine(msg.ToString());
2880242f987a initial log capabilities
cin
parents:
diff changeset
104 }*/
2880242f987a initial log capabilities
cin
parents:
diff changeset
105 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
106 }
2880242f987a initial log capabilities
cin
parents:
diff changeset
107 }