@@ -0,0 +1,11 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Linq; | |||
|
4 | using System.Text; | |||
|
5 | using System.Threading.Tasks; | |||
|
6 | ||||
|
7 | namespace Implab.Diagnostics { | |||
|
8 | public interface IEventListener { | |||
|
9 | void TraceEvent(LogContext context, TraceEventType type, string format, params object[] args); | |||
|
10 | } | |||
|
11 | } |
@@ -0,0 +1,50 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Diagnostics; | |||
|
4 | using System.Linq; | |||
|
5 | using System.Text; | |||
|
6 | using System.Threading.Tasks; | |||
|
7 | ||||
|
8 | namespace Implab.Diagnostics { | |||
|
9 | public static class Log { | |||
|
10 | [Conditional("TRACE")] | |||
|
11 | public static void Transfer(LogContext from) { | |||
|
12 | LogContext.Transfer(from); | |||
|
13 | } | |||
|
14 | ||||
|
15 | [Conditional("TRACE")] | |||
|
16 | public static void StartLogicalOperation() { | |||
|
17 | LogContext.Current.StartLogicalOperation(); | |||
|
18 | } | |||
|
19 | ||||
|
20 | [Conditional("TRACE")] | |||
|
21 | public static void StartLogicalOperation(string name) { | |||
|
22 | LogContext.Current.StartLogicalOperation(name); | |||
|
23 | } | |||
|
24 | ||||
|
25 | [Conditional("TRACE")] | |||
|
26 | public static void EndLogicalOperation() { | |||
|
27 | LogContext.Current.EndLogicalOperation(); | |||
|
28 | } | |||
|
29 | ||||
|
30 | [Conditional("TRACE")] | |||
|
31 | public static void TraceInformation(string format, params object[] arguments) { | |||
|
32 | LogContext.Current.TraceEvent(TraceEventType.Information, format, arguments); | |||
|
33 | } | |||
|
34 | ||||
|
35 | [Conditional("TRACE")] | |||
|
36 | public static void TraceWarning(string format, params object[] arguments) { | |||
|
37 | LogContext.Current.TraceEvent(TraceEventType.Warning, format, arguments); | |||
|
38 | } | |||
|
39 | ||||
|
40 | [Conditional("TRACE")] | |||
|
41 | public static void TraceError(string format, params object[] arguments) { | |||
|
42 | LogContext.Current.TraceEvent(TraceEventType.Error, format, arguments); | |||
|
43 | } | |||
|
44 | ||||
|
45 | [Conditional("TRACE")] | |||
|
46 | public static void TraceError(Exception err) { | |||
|
47 | TraceError("{0}", err); | |||
|
48 | } | |||
|
49 | } | |||
|
50 | } |
@@ -0,0 +1,107 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Linq; | |||
|
4 | using System.Text; | |||
|
5 | using System.Threading; | |||
|
6 | using System.Threading.Tasks; | |||
|
7 | ||||
|
8 | namespace Implab.Diagnostics { | |||
|
9 | public class LogContext { | |||
|
10 | LogicalOperation m_currentOperation; | |||
|
11 | readonly LogicalOperation m_traceBound; | |||
|
12 | readonly int m_threadId; | |||
|
13 | readonly LogContext m_parent; | |||
|
14 | ||||
|
15 | readonly static object _consoleLock = new object(); | |||
|
16 | ||||
|
17 | [ThreadStatic] | |||
|
18 | static LogContext _current; | |||
|
19 | ||||
|
20 | public static LogContext Current { | |||
|
21 | get { | |||
|
22 | if (_current == null) | |||
|
23 | _current = new LogContext(); | |||
|
24 | return _current; | |||
|
25 | } | |||
|
26 | } | |||
|
27 | ||||
|
28 | LogContext(LogContext context) { | |||
|
29 | if (context == null) | |||
|
30 | throw new ArgumentNullException("context"); | |||
|
31 | ||||
|
32 | m_parent = context; | |||
|
33 | m_currentOperation = context.CurrentOperation; | |||
|
34 | m_traceBound = context.CurrentOperation; | |||
|
35 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |||
|
36 | ||||
|
37 | TraceEvent(TraceEventType.Transfer, String.Empty); | |||
|
38 | } | |||
|
39 | ||||
|
40 | LogContext() { | |||
|
41 | m_currentOperation = new LogicalOperation(); | |||
|
42 | m_traceBound = m_currentOperation; | |||
|
43 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |||
|
44 | } | |||
|
45 | ||||
|
46 | public static void Transfer(LogContext from) { | |||
|
47 | _current = from == null ? new LogContext() : new LogContext(from); | |||
|
48 | } | |||
|
49 | ||||
|
50 | public LogContext ParentContext { | |||
|
51 | get { | |||
|
52 | return m_parent; | |||
|
53 | } | |||
|
54 | } | |||
|
55 | ||||
|
56 | public LogicalOperation CurrentOperation { | |||
|
57 | get { | |||
|
58 | return m_currentOperation; | |||
|
59 | } | |||
|
60 | } | |||
|
61 | ||||
|
62 | public LogicalOperation TraceBound { | |||
|
63 | get { | |||
|
64 | return m_traceBound; | |||
|
65 | } | |||
|
66 | } | |||
|
67 | ||||
|
68 | public int ThreadId { | |||
|
69 | get { | |||
|
70 | return m_threadId; | |||
|
71 | } | |||
|
72 | } | |||
|
73 | ||||
|
74 | public void StartLogicalOperation() { | |||
|
75 | StartLogicalOperation(null); | |||
|
76 | } | |||
|
77 | ||||
|
78 | public void StartLogicalOperation(string name) { | |||
|
79 | TraceEvent(TraceEventType.OperationStarted, "{0}", name); | |||
|
80 | m_currentOperation = new LogicalOperation(name, m_currentOperation); | |||
|
81 | } | |||
|
82 | ||||
|
83 | public void EndLogicalOperation() { | |||
|
84 | if (m_traceBound == m_currentOperation) { | |||
|
85 | TraceEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); | |||
|
86 | } else { | |||
|
87 | var op = m_currentOperation; | |||
|
88 | m_currentOperation = m_currentOperation.Parent; | |||
|
89 | TraceEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); | |||
|
90 | } | |||
|
91 | } | |||
|
92 | ||||
|
93 | public void TraceEvent(TraceEventType type, string format, params object[] args) { | |||
|
94 | /*var msg = new StringBuilder(); | |||
|
95 | for (int i = 0; i < CurrentOperation.Level; i++) | |||
|
96 | msg.Append(" "); | |||
|
97 | msg.Append(type); | |||
|
98 | msg.AppendFormat("[{0}]: ",m_threadId); | |||
|
99 | msg.AppendFormat(format, args); | |||
|
100 | ||||
|
101 | lock (_consoleLock) { | |||
|
102 | Console.ForegroundColor = (ConsoleColor)(m_threadId % 15 + 1); | |||
|
103 | Console.WriteLine(msg.ToString()); | |||
|
104 | }*/ | |||
|
105 | } | |||
|
106 | } | |||
|
107 | } |
@@ -0,0 +1,47 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Linq; | |||
|
4 | using System.Text; | |||
|
5 | using System.Threading.Tasks; | |||
|
6 | ||||
|
7 | namespace Implab.Diagnostics { | |||
|
8 | public class LogicalOperation { | |||
|
9 | readonly LogicalOperation m_parent; | |||
|
10 | readonly string m_name; | |||
|
11 | readonly int m_level; | |||
|
12 | readonly int m_timestamp; | |||
|
13 | ||||
|
14 | public LogicalOperation() | |||
|
15 | : this(null, null) { | |||
|
16 | } | |||
|
17 | ||||
|
18 | public LogicalOperation(string name, LogicalOperation parent) { | |||
|
19 | m_name = name ?? String.Empty; | |||
|
20 | m_parent = parent; | |||
|
21 | ||||
|
22 | m_level = parent == null ? 0 : parent.Level + 1; | |||
|
23 | m_timestamp = Environment.TickCount; | |||
|
24 | } | |||
|
25 | ||||
|
26 | public int Duration { | |||
|
27 | get { | |||
|
28 | var dt = Environment.TickCount - m_timestamp; | |||
|
29 | return dt < 0 ? int.MaxValue + dt : dt; // handle overflow | |||
|
30 | } | |||
|
31 | } | |||
|
32 | ||||
|
33 | public LogicalOperation Parent { | |||
|
34 | get { | |||
|
35 | return m_parent; | |||
|
36 | } | |||
|
37 | } | |||
|
38 | ||||
|
39 | public int Level { | |||
|
40 | get { return m_level; } | |||
|
41 | } | |||
|
42 | ||||
|
43 | public string Name { | |||
|
44 | get { return m_name; } | |||
|
45 | } | |||
|
46 | } | |||
|
47 | } |
@@ -0,0 +1,16 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Linq; | |||
|
4 | using System.Text; | |||
|
5 | using System.Threading.Tasks; | |||
|
6 | ||||
|
7 | namespace Implab.Diagnostics { | |||
|
8 | public enum TraceEventType { | |||
|
9 | Information = 1, | |||
|
10 | Warning, | |||
|
11 | Error, | |||
|
12 | OperationStarted, | |||
|
13 | OperationCompleted, | |||
|
14 | Transfer | |||
|
15 | } | |||
|
16 | } |
1 | NO CONTENT: modified file, binary diff hidden |
|
NO CONTENT: modified file, binary diff hidden |
@@ -15,7 +15,7 | |||||
15 | <DebugType>full</DebugType> |
|
15 | <DebugType>full</DebugType> | |
16 | <Optimize>false</Optimize> |
|
16 | <Optimize>false</Optimize> | |
17 | <OutputPath>bin\Debug</OutputPath> |
|
17 | <OutputPath>bin\Debug</OutputPath> | |
18 | <DefineConstants>DEBUG;</DefineConstants> |
|
18 | <DefineConstants>TRACE;DEBUG;</DefineConstants> | |
19 | <ErrorReport>prompt</ErrorReport> |
|
19 | <ErrorReport>prompt</ErrorReport> | |
20 | <WarningLevel>4</WarningLevel> |
|
20 | <WarningLevel>4</WarningLevel> | |
21 | <ConsolePause>false</ConsolePause> |
|
21 | <ConsolePause>false</ConsolePause> | |
@@ -32,6 +32,11 | |||||
32 | <Reference Include="System" /> |
|
32 | <Reference Include="System" /> | |
33 | </ItemGroup> |
|
33 | </ItemGroup> | |
34 | <ItemGroup> |
|
34 | <ItemGroup> | |
|
35 | <Compile Include="Diagnostics\IEventListener.cs" /> | |||
|
36 | <Compile Include="Diagnostics\LogicalOperation.cs" /> | |||
|
37 | <Compile Include="Diagnostics\Log.cs" /> | |||
|
38 | <Compile Include="Diagnostics\LogContext.cs" /> | |||
|
39 | <Compile Include="Diagnostics\TraceEventType.cs" /> | |||
35 | <Compile Include="ICancellable.cs" /> |
|
40 | <Compile Include="ICancellable.cs" /> | |
36 | <Compile Include="IProgressHandler.cs" /> |
|
41 | <Compile Include="IProgressHandler.cs" /> | |
37 | <Compile Include="IProgressNotifier.cs" /> |
|
42 | <Compile Include="IProgressNotifier.cs" /> | |
@@ -43,7 +48,6 | |||||
43 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
48 | <Compile Include="Parallels\ArrayTraits.cs" /> | |
44 | <Compile Include="Parallels\MTQueue.cs" /> |
|
49 | <Compile Include="Parallels\MTQueue.cs" /> | |
45 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
50 | <Compile Include="Parallels\WorkerPool.cs" /> | |
46 | <Compile Include="PromiseState.cs" /> |
|
|||
47 | <Compile Include="TaskController.cs" /> |
|
51 | <Compile Include="TaskController.cs" /> | |
48 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
52 | <Compile Include="ProgressInitEventArgs.cs" /> | |
49 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
53 | <Compile Include="Properties\AssemblyInfo.cs" /> |
@@ -1,3 +1,4 | |||||
|
1 | using Implab.Diagnostics; | |||
1 | using System; |
|
2 | using System; | |
2 | using System.Threading; |
|
3 | using System.Threading; | |
3 |
|
4 | |||
@@ -13,8 +14,10 namespace Implab.Parallels { | |||||
13 |
|
14 | |||
14 | public static Promise<T> Invoke<T>(Func<T> func) { |
|
15 | public static Promise<T> Invoke<T>(Func<T> func) { | |
15 | var p = new Promise<T>(); |
|
16 | var p = new Promise<T>(); | |
|
17 | var caller = LogContext.Current; | |||
16 |
|
18 | |||
17 | ThreadPool.QueueUserWorkItem(param => { |
|
19 | ThreadPool.QueueUserWorkItem(param => { | |
|
20 | Log.Transfer(caller); | |||
18 | try { |
|
21 | try { | |
19 | p.Resolve(func()); |
|
22 | p.Resolve(func()); | |
20 | } catch(Exception e) { |
|
23 | } catch(Exception e) { | |
@@ -28,7 +31,10 namespace Implab.Parallels { | |||||
28 | public static Promise<T> InvokeNewThread<T>(Func<T> func) { |
|
31 | public static Promise<T> InvokeNewThread<T>(Func<T> func) { | |
29 | var p = new Promise<T>(); |
|
32 | var p = new Promise<T>(); | |
30 |
|
33 | |||
|
34 | var caller = LogContext.Current; | |||
|
35 | ||||
31 | var worker = new Thread(() => { |
|
36 | var worker = new Thread(() => { | |
|
37 | Log.Transfer(caller); | |||
32 | try { |
|
38 | try { | |
33 | p.Resolve(func()); |
|
39 | p.Resolve(func()); | |
34 | } catch (Exception e) { |
|
40 | } catch (Exception e) { |
@@ -4,6 +4,7 using System.Linq; | |||||
4 | using System.Text; |
|
4 | using System.Text; | |
5 | using System.Threading; |
|
5 | using System.Threading; | |
6 | using System.Diagnostics; |
|
6 | using System.Diagnostics; | |
|
7 | using Implab.Diagnostics; | |||
7 |
|
8 | |||
8 | namespace Implab.Parallels { |
|
9 | namespace Implab.Parallels { | |
9 | public class WorkerPool : DispatchPool<Action> { |
|
10 | public class WorkerPool : DispatchPool<Action> { | |
@@ -41,7 +42,10 namespace Implab.Parallels { | |||||
41 |
|
42 | |||
42 | var promise = new Promise<T>(); |
|
43 | var promise = new Promise<T>(); | |
43 |
|
44 | |||
|
45 | var caller = LogContext.Current; | |||
|
46 | ||||
44 | EnqueueTask(delegate() { |
|
47 | EnqueueTask(delegate() { | |
|
48 | Log.Transfer(caller); | |||
45 | try { |
|
49 | try { | |
46 | promise.Resolve(task()); |
|
50 | promise.Resolve(task()); | |
47 | } catch (Exception e) { |
|
51 | } catch (Exception e) { |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now