Mercurial > pub > ImplabNet
changeset 36:313f708a50e9 diagnostics
improved log concept
author | cin |
---|---|
date | Tue, 15 Apr 2014 02:00:09 +0400 |
parents | 2880242f987a |
children | c2c043520724 |
files | Implab.v11.suo Implab/Diagnostics/ConsoleTraceListener.cs Implab/Diagnostics/IEventListener.cs Implab/Diagnostics/Log.cs Implab/Diagnostics/LogChannel.cs Implab/Diagnostics/LogContext.cs Implab/Diagnostics/TraceContext.cs Implab/Diagnostics/TraceEvent.cs Implab/Diagnostics/TraceLog.cs Implab/Implab.csproj Implab/Parallels/AsyncPool.cs Implab/Parallels/WorkerPool.cs |
diffstat | 12 files changed, 249 insertions(+), 177 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Diagnostics/ConsoleTraceListener.cs Tue Apr 15 02:00:09 2014 +0400 @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Implab.Diagnostics { + public class ConsoleTraceListener { + + static readonly object _consoleLock = new object(); + + public void Subscribe() { + LogChannel<TraceEvent>.Default.Events += Default_Events; + } + + public void Unsubscribe() { + LogChannel<TraceEvent>.Default.Events -= Default_Events; + } + + void Default_Events(object sender, ValueEventArgs<TraceEvent> e) { + LogEvent((TraceContext)sender, e.Value); + } + + void LogEvent(TraceContext context, TraceEvent evt) { + var msg = new StringBuilder(); + for (int i = 0; i < context.CurrentOperation.Level; i++) + msg.Append(" "); + msg.Append(evt.EventType); + msg.AppendFormat("[{0}]: ",context.ThreadId); + msg.Append(evt.Message); + + lock (_consoleLock) { + Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1); + Console.WriteLine(msg.ToString()); + } + } + } +}
--- a/Implab/Diagnostics/IEventListener.cs Mon Apr 14 18:25:26 2014 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Implab.Diagnostics { - public interface IEventListener { - void TraceEvent(LogContext context, TraceEventType type, string format, params object[] args); - } -}
--- a/Implab/Diagnostics/Log.cs Mon Apr 14 18:25:26 2014 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Implab.Diagnostics { - public static class Log { - [Conditional("TRACE")] - public static void Transfer(LogContext from) { - LogContext.Transfer(from); - } - - [Conditional("TRACE")] - public static void StartLogicalOperation() { - LogContext.Current.StartLogicalOperation(); - } - - [Conditional("TRACE")] - public static void StartLogicalOperation(string name) { - LogContext.Current.StartLogicalOperation(name); - } - - [Conditional("TRACE")] - public static void EndLogicalOperation() { - LogContext.Current.EndLogicalOperation(); - } - - [Conditional("TRACE")] - public static void TraceInformation(string format, params object[] arguments) { - LogContext.Current.TraceEvent(TraceEventType.Information, format, arguments); - } - - [Conditional("TRACE")] - public static void TraceWarning(string format, params object[] arguments) { - LogContext.Current.TraceEvent(TraceEventType.Warning, format, arguments); - } - - [Conditional("TRACE")] - public static void TraceError(string format, params object[] arguments) { - LogContext.Current.TraceEvent(TraceEventType.Error, format, arguments); - } - - [Conditional("TRACE")] - public static void TraceError(Exception err) { - TraceError("{0}", err); - } - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Diagnostics/LogChannel.cs Tue Apr 15 02:00:09 2014 +0400 @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Implab.Diagnostics { + public class LogChannel<TEvent> { + static LogChannel<TEvent> _default = new LogChannel<TEvent>(); + + public static LogChannel<TEvent> Default { + get { + return _default; + } + } + + public event EventHandler<ValueEventArgs<TEvent>> Events; + + public void LogEvent(TEvent data) { + var t = Events; + if (t!= null) + t(TraceContext.Current,new ValueEventArgs<TEvent>(data)); + } + } +}
--- a/Implab/Diagnostics/LogContext.cs Mon Apr 14 18:25:26 2014 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace Implab.Diagnostics { - public class LogContext { - LogicalOperation m_currentOperation; - readonly LogicalOperation m_traceBound; - readonly int m_threadId; - readonly LogContext m_parent; - - readonly static object _consoleLock = new object(); - - [ThreadStatic] - static LogContext _current; - - public static LogContext Current { - get { - if (_current == null) - _current = new LogContext(); - return _current; - } - } - - LogContext(LogContext context) { - if (context == null) - throw new ArgumentNullException("context"); - - m_parent = context; - m_currentOperation = context.CurrentOperation; - m_traceBound = context.CurrentOperation; - m_threadId = Thread.CurrentThread.ManagedThreadId; - - TraceEvent(TraceEventType.Transfer, String.Empty); - } - - LogContext() { - m_currentOperation = new LogicalOperation(); - m_traceBound = m_currentOperation; - m_threadId = Thread.CurrentThread.ManagedThreadId; - } - - public static void Transfer(LogContext from) { - _current = from == null ? new LogContext() : new LogContext(from); - } - - public LogContext ParentContext { - get { - return m_parent; - } - } - - public LogicalOperation CurrentOperation { - get { - return m_currentOperation; - } - } - - public LogicalOperation TraceBound { - get { - return m_traceBound; - } - } - - public int ThreadId { - get { - return m_threadId; - } - } - - public void StartLogicalOperation() { - StartLogicalOperation(null); - } - - public void StartLogicalOperation(string name) { - TraceEvent(TraceEventType.OperationStarted, "{0}", name); - m_currentOperation = new LogicalOperation(name, m_currentOperation); - } - - public void EndLogicalOperation() { - if (m_traceBound == m_currentOperation) { - TraceEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); - } else { - var op = m_currentOperation; - m_currentOperation = m_currentOperation.Parent; - TraceEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); - } - } - - public void TraceEvent(TraceEventType type, string format, params object[] args) { - /*var msg = new StringBuilder(); - for (int i = 0; i < CurrentOperation.Level; i++) - msg.Append(" "); - msg.Append(type); - msg.AppendFormat("[{0}]: ",m_threadId); - msg.AppendFormat(format, args); - - lock (_consoleLock) { - Console.ForegroundColor = (ConsoleColor)(m_threadId % 15 + 1); - Console.WriteLine(msg.ToString()); - }*/ - } - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Diagnostics/TraceContext.cs Tue Apr 15 02:00:09 2014 +0400 @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Implab.Diagnostics { + public class TraceContext { + LogicalOperation m_currentOperation; + readonly LogicalOperation m_traceBound; + readonly int m_threadId; + readonly TraceContext m_parent; + + readonly static object _consoleLock = new object(); + + [ThreadStatic] + static TraceContext _current; + + public static TraceContext Current { + get { + if (_current == null) + _current = new TraceContext(); + return _current; + } + } + + TraceContext(TraceContext context) { + if (context == null) + throw new ArgumentNullException("context"); + + m_parent = context; + m_currentOperation = context.CurrentOperation; + m_traceBound = context.CurrentOperation; + m_threadId = Thread.CurrentThread.ManagedThreadId; + + LogEvent(TraceEventType.Transfer, String.Empty); + } + + TraceContext() { + m_currentOperation = new LogicalOperation(); + m_traceBound = m_currentOperation; + m_threadId = Thread.CurrentThread.ManagedThreadId; + } + + public static void Transfer(TraceContext from) { + _current = from == null ? new TraceContext() : new TraceContext(from); + } + + public TraceContext ParentContext { + get { + return m_parent; + } + } + + public LogicalOperation CurrentOperation { + get { + return m_currentOperation; + } + } + + public LogicalOperation TraceBound { + get { + return m_traceBound; + } + } + + public int ThreadId { + get { + return m_threadId; + } + } + + public void StartLogicalOperation() { + StartLogicalOperation(null); + } + + public void StartLogicalOperation(string name) { + LogEvent(TraceEventType.OperationStarted, "{0}", name); + m_currentOperation = new LogicalOperation(name, m_currentOperation); + } + + public void EndLogicalOperation() { + if (m_traceBound == m_currentOperation) { + LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); + } else { + var op = m_currentOperation; + m_currentOperation = m_currentOperation.Parent; + LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); + } + } + + void LogEvent(TraceEventType type, string format, params object[] args) { + LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(type, format, args)); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Diagnostics/TraceEvent.cs Tue Apr 15 02:00:09 2014 +0400 @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Implab.Diagnostics { + public class TraceEvent { + public string Message { + get; + private set; + } + + public TraceEventType EventType { + get; + private set; + } + + public TraceEvent(TraceEventType type, string message) { + EventType = type; + Message = message; + } + + public static TraceEvent Create(TraceEventType type, string format, params object[] args) { + return new TraceEvent(type, String.Format(format, args)); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Diagnostics/TraceLog.cs Tue Apr 15 02:00:09 2014 +0400 @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Implab.Diagnostics { + /// <summary> + /// Класс для публикации событий выполнения программы, события публикуются через <see cref="LogChannel{TraceEvent}"/> + /// </summary> + public static class TraceLog { + [Conditional("TRACE")] + public static void Transfer(TraceContext from) { + TraceContext.Transfer(from); + } + + [Conditional("TRACE")] + public static void StartLogicalOperation() { + TraceContext.Current.StartLogicalOperation(); + } + + [Conditional("TRACE")] + public static void StartLogicalOperation(string name) { + TraceContext.Current.StartLogicalOperation(name); + } + + [Conditional("TRACE")] + public static void EndLogicalOperation() { + TraceContext.Current.EndLogicalOperation(); + } + + [Conditional("TRACE")] + public static void TraceInformation(string format, params object[] arguments) { + LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Information, format, arguments)); + } + + [Conditional("TRACE")] + public static void TraceWarning(string format, params object[] arguments) { + LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Warning, format, arguments)); + } + + [Conditional("TRACE")] + public static void TraceError(string format, params object[] arguments) { + LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Error, format, arguments)); + } + + [Conditional("TRACE")] + public static void TraceError(Exception err) { + TraceError("{0}", err); + } + } +}
--- a/Implab/Implab.csproj Mon Apr 14 18:25:26 2014 +0400 +++ b/Implab/Implab.csproj Tue Apr 15 02:00:09 2014 +0400 @@ -32,10 +32,12 @@ <Reference Include="System" /> </ItemGroup> <ItemGroup> - <Compile Include="Diagnostics\IEventListener.cs" /> + <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> + <Compile Include="Diagnostics\LogChannel.cs" /> <Compile Include="Diagnostics\LogicalOperation.cs" /> - <Compile Include="Diagnostics\Log.cs" /> - <Compile Include="Diagnostics\LogContext.cs" /> + <Compile Include="Diagnostics\TraceLog.cs" /> + <Compile Include="Diagnostics\TraceContext.cs" /> + <Compile Include="Diagnostics\TraceEvent.cs" /> <Compile Include="Diagnostics\TraceEventType.cs" /> <Compile Include="ICancellable.cs" /> <Compile Include="IProgressHandler.cs" />
--- a/Implab/Parallels/AsyncPool.cs Mon Apr 14 18:25:26 2014 +0400 +++ b/Implab/Parallels/AsyncPool.cs Tue Apr 15 02:00:09 2014 +0400 @@ -14,10 +14,10 @@ public static Promise<T> Invoke<T>(Func<T> func) { var p = new Promise<T>(); - var caller = LogContext.Current; + var caller = TraceContext.Current; ThreadPool.QueueUserWorkItem(param => { - Log.Transfer(caller); + TraceLog.Transfer(caller); try { p.Resolve(func()); } catch(Exception e) { @@ -31,10 +31,10 @@ public static Promise<T> InvokeNewThread<T>(Func<T> func) { var p = new Promise<T>(); - var caller = LogContext.Current; + var caller = TraceContext.Current; var worker = new Thread(() => { - Log.Transfer(caller); + TraceLog.Transfer(caller); try { p.Resolve(func()); } catch (Exception e) {
--- a/Implab/Parallels/WorkerPool.cs Mon Apr 14 18:25:26 2014 +0400 +++ b/Implab/Parallels/WorkerPool.cs Tue Apr 15 02:00:09 2014 +0400 @@ -42,10 +42,10 @@ var promise = new Promise<T>(); - var caller = LogContext.Current; + var caller = TraceContext.Current; EnqueueTask(delegate() { - Log.Transfer(caller); + TraceLog.Transfer(caller); try { promise.Resolve(task()); } catch (Exception e) {