# HG changeset patch # User cin # Date 1397810268 -14400 # Node ID f8cbe84cfdb16cd2867ceadda940642625800d2e # Parent e5ec543feee336b44fc00caeeab1ffcfaf557916# Parent 6f335e65b75f2aad960c6d23ab637fea7301c639 Слияние с interactive logger diff -r e5ec543feee3 -r f8cbe84cfdb1 .hgignore --- a/.hgignore Wed Apr 16 19:02:58 2014 +0400 +++ b/.hgignore Fri Apr 18 12:37:48 2014 +0400 @@ -11,3 +11,5 @@ Implab.Fx.Test/bin/ Implab.Fx.Test/obj/ _ReSharper.Implab/ +Implab.Diagnostics.Interactive/bin/ +Implab.Diagnostics.Interactive/obj/ diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/Implab.Diagnostics.Interactive.csproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/Implab.Diagnostics.Interactive.csproj Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + {1DB7DB0C-8AA9-484B-A681-33AE94038391} + Library + Properties + Implab.Diagnostics.Interactive + Implab.Diagnostics.Interactive + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + Form + + + TraceForm.cs + + + + + + {f550f1f8-8746-4ad0-9614-855f4c4b7f05} + Implab + + + + + TraceForm.cs + + + + + + + + \ No newline at end of file diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/InteractiveListener.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/InteractiveListener.cs Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,122 @@ +using Implab.Parallels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Implab.Diagnostics.Interactive +{ + public class InteractiveListener: TextListenerBase + { + TraceForm m_form; + + SynchronizationContext m_syncGuiThread; + readonly Promise m_guiStarted = new Promise(); + + readonly IPromiseBase m_guiFinished; + readonly IPromiseBase m_workerFinished = new Promise(); + + readonly MTQueue m_queue = new MTQueue(); + readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); + + int m_queueLength; + bool m_exitPending; + + readonly object m_pauseLock = new object(); + bool m_paused; + readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true); + + public InteractiveListener(bool global) : base(global) { + m_guiFinished = AsyncPool.InvokeNewThread(GuiThread); + m_workerFinished = AsyncPool.InvokeNewThread(QueueThread); + + m_guiStarted.Join(); + } + + void GuiThread() { + m_form = new TraceForm(); // will create SynchronizationContext + + m_form.PauseEvents += (s,a) => Pause(); + m_form.ResumeEvents += (s, a) => Resume(); + + m_syncGuiThread = SynchronizationContext.Current; + m_guiStarted.Resolve(); + Application.Run(); + } + + void QueueThread() { + while (!m_exitPending) { + if (m_paused) + m_pauseEvent.WaitOne(); + + TraceViewItem item; + if (m_queue.TryDequeue(out item)) { + Interlocked.Decrement(ref m_queueLength); + + m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null); + } else { + m_queueEvent.WaitOne(); + } + } + } + + public void Pause() { + // for consistency we need to set this properties atomically + lock (m_pauseLock) { + m_pauseEvent.Reset(); + m_paused = true; + } + } + + public void Resume() { + // for consistency we need to set this properties atomically + lock (m_pauseLock) { + m_paused = false; + m_pauseEvent.Set(); + } + } + + void Enqueue(TraceViewItem item) { + m_queue.Enqueue(item); + if (Interlocked.Increment(ref m_queueLength) == 1) + m_queueEvent.Set(); + } + + public void ShowForm() { + m_syncGuiThread.Post(x => m_form.Show(), null); + } + + public void HideForm() { + m_syncGuiThread.Post(x => m_form.Hide(), null); + } + + void Terminate() { + m_exitPending = true; + Resume(); + m_syncGuiThread.Post(x => Application.ExitThread(), null); + } + + protected override void Dispose(bool disposing) { + if (disposing) { + Terminate(); + m_guiFinished.Join(); + } + base.Dispose(disposing); + } + + protected override void WriteEntry(TraceContext context, EventText text, string channel) { + var item = new TraceViewItem { + Indent = text.indent, + Message = text.content, + Thread = context.ThreadId, + Channel = channel, + Timestamp = Environment.TickCount + }; + + Enqueue(item); + } + } +} diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/Properties/AssemblyInfo.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/Properties/AssemblyInfo.cs Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Implab.Diagnostics.Interactive")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Implab.Diagnostics.Interactive")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1c156c51-4884-43b2-a823-d86313872e82")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/Properties/DataSources/TraceViewItem.datasource --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/Properties/DataSources/TraceViewItem.datasource Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,10 @@ + + + + Implab.Diagnostics.Interactive.TraceViewItem, Implab.Diagnostics.Interactive + \ No newline at end of file diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/TextStyle.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/TextStyle.cs Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Implab.Diagnostics.Interactive { + class TextStyle { + public Color TextColor { + get; + set; + } + + public FontStyle FontStyle { + get; + set; + } + + public int PaddingLeft { + get; + set; + } + } +} diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/TraceForm.Designer.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/TraceForm.Designer.cs Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,134 @@ +namespace Implab.Diagnostics.Interactive { + partial class TraceForm { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); + this.eventsDataGrid = new System.Windows.Forms.DataGridView(); + this.traceViewItemBindingSource = new System.Windows.Forms.BindingSource(this.components); + this.threadDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Channel = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.messageDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)(this.eventsDataGrid)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.traceViewItemBindingSource)).BeginInit(); + this.SuspendLayout(); + // + // eventsDataGrid + // + this.eventsDataGrid.AllowUserToAddRows = false; + this.eventsDataGrid.AllowUserToDeleteRows = false; + this.eventsDataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.eventsDataGrid.AutoGenerateColumns = false; + this.eventsDataGrid.BackgroundColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))); + dataGridViewCellStyle1.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText; + dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; + dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; + dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True; + this.eventsDataGrid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; + this.eventsDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.eventsDataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.threadDataGridViewTextBoxColumn, + this.Channel, + this.messageDataGridViewTextBoxColumn}); + this.eventsDataGrid.DataSource = this.traceViewItemBindingSource; + dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle3.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight; + dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText; + dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False; + this.eventsDataGrid.DefaultCellStyle = dataGridViewCellStyle3; + this.eventsDataGrid.Location = new System.Drawing.Point(12, 12); + this.eventsDataGrid.Name = "eventsDataGrid"; + this.eventsDataGrid.ReadOnly = true; + this.eventsDataGrid.RowHeadersWidth = 17; + this.eventsDataGrid.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False; + this.eventsDataGrid.Size = new System.Drawing.Size(939, 480); + this.eventsDataGrid.TabIndex = 1; + this.eventsDataGrid.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.eventsDataGrid_CellFormatting); + // + // traceViewItemBindingSource + // + this.traceViewItemBindingSource.DataSource = typeof(Implab.Diagnostics.Interactive.TraceViewItem); + // + // threadDataGridViewTextBoxColumn + // + this.threadDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; + this.threadDataGridViewTextBoxColumn.DataPropertyName = "Thread"; + this.threadDataGridViewTextBoxColumn.HeaderText = "TID"; + this.threadDataGridViewTextBoxColumn.Name = "threadDataGridViewTextBoxColumn"; + this.threadDataGridViewTextBoxColumn.ReadOnly = true; + this.threadDataGridViewTextBoxColumn.Width = 5; + // + // Channel + // + this.Channel.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells; + this.Channel.DataPropertyName = "Channel"; + this.Channel.HeaderText = "Channel"; + this.Channel.Name = "Channel"; + this.Channel.ReadOnly = true; + this.Channel.Width = 79; + // + // messageDataGridViewTextBoxColumn + // + this.messageDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.messageDataGridViewTextBoxColumn.DataPropertyName = "FormattedMessage"; + dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False; + this.messageDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle2; + this.messageDataGridViewTextBoxColumn.HeaderText = "Message"; + this.messageDataGridViewTextBoxColumn.Name = "messageDataGridViewTextBoxColumn"; + this.messageDataGridViewTextBoxColumn.ReadOnly = true; + // + // TraceForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(963, 504); + this.Controls.Add(this.eventsDataGrid); + this.Name = "TraceForm"; + this.Text = "TraceForm"; + ((System.ComponentModel.ISupportInitialize)(this.eventsDataGrid)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.traceViewItemBindingSource)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.DataGridView eventsDataGrid; + private System.Windows.Forms.BindingSource traceViewItemBindingSource; + private System.Windows.Forms.DataGridViewTextBoxColumn threadDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn Channel; + private System.Windows.Forms.DataGridViewTextBoxColumn messageDataGridViewTextBoxColumn; + + } +} \ No newline at end of file diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/TraceForm.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/TraceForm.cs Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Implab.Diagnostics.Interactive { + public partial class TraceForm : Form { + readonly Dictionary m_threadColors = new Dictionary(); + readonly Random m_rand = new Random(); + + public event EventHandler PauseEvents; + + public event EventHandler ResumeEvents; + + public TraceForm() { + InitializeComponent(); + } + + protected override void OnFormClosing(FormClosingEventArgs e) { + base.OnFormClosing(e); + if (!e.Cancel && e.CloseReason == CloseReason.UserClosing) { + e.Cancel = true; + Hide(); + } + } + + public void AddTraceEvent(TraceViewItem item) { + traceViewItemBindingSource.Add(item); + eventsDataGrid.FirstDisplayedScrollingRowIndex = eventsDataGrid.RowCount - 1; + } + + Color GetThreadColor(int thread) { + Color result; + if (!m_threadColors.TryGetValue(thread, out result)) { + result = Color.FromArgb(m_rand.Next(4)*64, m_rand.Next(4)*64, m_rand.Next(4)*64); + m_threadColors[thread] = result; + } + return result; + } + + private void eventsDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { + var data = (TraceViewItem)traceViewItemBindingSource[e.RowIndex]; + if (e.ColumnIndex == messageDataGridViewTextBoxColumn.Index) + e.CellStyle.Padding = new Padding(data.Indent * 10,0,0,0); + e.CellStyle.ForeColor = GetThreadColor(data.Thread); + } + } +} diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/TraceForm.resx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/TraceForm.resx Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + True + + + 17, 17 + + \ No newline at end of file diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab.Diagnostics.Interactive/TraceViewItem.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Diagnostics.Interactive/TraceViewItem.cs Fri Apr 18 12:37:48 2014 +0400 @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Implab.Diagnostics.Interactive { + public class TraceViewItem { + string m_formattedValue; + + public string Message { get; set; } + public int Timestamp { get; set; } + public int Indent { get; set; } + public int Thread { get; set; } + public string Channel { get; set; } + + public string FormattedMessage { + get { + if (m_formattedValue == null) { + m_formattedValue = Message.Replace("\r",String.Empty).Replace("\n", " | "); + } + return m_formattedValue; + } + } + } +} diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Diagnostics/ConsoleTraceListener.cs --- a/Implab/Diagnostics/ConsoleTraceListener.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Diagnostics/ConsoleTraceListener.cs Fri Apr 18 12:37:48 2014 +0400 @@ -13,17 +13,17 @@ } - public ConsoleTraceListener(bool local) - : base(local) { + public ConsoleTraceListener(bool global) + : base(global) { } - protected override void WriteEntry(TraceContext context, EventText text) { + protected override void WriteEntry(TraceContext context, EventText text, string channel) { var msg = new StringBuilder(); for (int i = 0; i < text.indent; i++) msg.Append(" "); - msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content); + msg.AppendFormat("[{0}]:{1}: {2}", context.ThreadId, channel, text.content); lock (_consoleLock) { Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1); diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Diagnostics/LogChannel.cs --- a/Implab/Diagnostics/LogChannel.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Diagnostics/LogChannel.cs Fri Apr 18 12:37:48 2014 +0400 @@ -4,23 +4,74 @@ using System.Text; namespace Implab.Diagnostics { + /// + /// Канал, через который публикуются события журнала. + /// + /// Тип событий в канале + /// + /// Событиями журнала могут быть любые типы, например строки, в которых будет передаваться + /// информация, или структуры с набором полей, описывающих важность, текст и другую информацию. + /// public class LogChannel { static LogChannel _default = new LogChannel(); + /// + /// Канал по-умолчанию для событий типа . + /// public static LogChannel Default { get { return _default; } } + /// + /// Событие появление новой записи в журнале, на это событие подписываются слушатели. + /// public event EventHandler> Events; + + /// + /// Имя канала, полезно для отображения в журнале + /// + public string Name { + get; + private set; + } + /// + /// Создает журнал, имя типа событий назначается в качетве имени канала. + /// + public LogChannel() + : this(null) { + } + + /// + /// Содает канал с указанным именем. + /// + /// Имя канала. + public LogChannel(string name) { + if (String.IsNullOrEmpty(name)) + name = typeof(TEvent).Name; + Name = name; + } + + /// + /// Отправляет запись журнала через канал подписчикам. + /// + /// Запись журнала. + /// + /// Контекст трассировки от которого рассылается сообщение определяется автоматически из текущего потока. + /// public void LogEvent(TEvent data) { var t = Events; if (t!= null) t(TraceContext.Current,new ValueEventArgs(data)); } + /// + /// Отправляет запись журнала через канал подписчикам. + /// + /// Запись журнала. + /// Контекст трассировки от которого рассылается сообщение/ public void LogEvent(TraceContext context,TEvent data) { var t = Events; if (t != null) diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Diagnostics/TextFileListener.cs --- a/Implab/Diagnostics/TextFileListener.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Diagnostics/TextFileListener.cs Fri Apr 18 12:37:48 2014 +0400 @@ -8,21 +8,23 @@ public class TextFileListener: TextListenerBase { readonly TextWriter m_textWriter; - public TextFileListener(string fileName, bool local) : base(local) { + public TextFileListener(string fileName, bool global) + : base(global) { m_textWriter = File.CreateText(fileName); m_textWriter.WriteLine("LOG {0}", DateTime.Now); Register(this); } - protected override void WriteEntry(TraceContext context, EventText text) { + protected override void WriteEntry(TraceContext context, EventText text, string channel) { var msg = new StringBuilder(); for (int i = 0; i < text.indent; i++) msg.Append(" "); - msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content); + msg.AppendFormat("[{0}]:{1}: {2}", context.ThreadId, channel, text.content); lock (m_textWriter) { if (!IsDisposed) { + // тут гарантировано еще не освобожден m_textWriter m_textWriter.WriteLine(msg.ToString()); m_textWriter.Flush(); } @@ -33,6 +35,7 @@ protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { + // IsDisposed = true lock (m_textWriter) { Safe.Dispose(m_textWriter); } diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Diagnostics/TextListenerBase.cs --- a/Implab/Diagnostics/TextListenerBase.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Diagnostics/TextListenerBase.cs Fri Apr 18 12:37:48 2014 +0400 @@ -10,9 +10,9 @@ readonly LogicalOperation m_boundOperation; readonly int m_baseIndent; - protected TextListenerBase(bool local) { + protected TextListenerBase(bool global) { Register(this); - if (local) { + if (!global) { m_boundOperation = TraceContext.Current.CurrentOperation; m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); } @@ -36,6 +36,7 @@ AssertNotDisposed(); var formatter = GetService>(); + var channelName = channel.Name; EventHandler> handler = (sender, args) => { TraceContext context = (TraceContext)sender; @@ -43,7 +44,7 @@ text.indent -= m_baseIndent; if (IsRelated(context.CurrentOperation)) - WriteEntry(context, text); + WriteEntry(context, text, channelName); }; if (m_subscriptions.ContainsKey(channel)) @@ -89,7 +90,16 @@ } } - protected abstract void WriteEntry(TraceContext context, EventText text); + /// + /// Вызывается для записи текста сообщения, в журнал. + /// + /// + /// Данный метод может вызваться из разных потоков одновременно. Возможна ситуация, когда + /// данный метод вызывается уже после освобождения ообъекта методом . + /// + /// Контекст трассировки. + /// Текст сообщения. + protected abstract void WriteEntry(TraceContext context, EventText text, string channel); public EventText Format(TraceContext context, object data) { return new EventText { @@ -110,10 +120,10 @@ } protected override void Dispose(bool disposing) { + base.Dispose(disposing); if (disposing) { UnsubscribeAll(); } - base.Dispose(disposing); } } } diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Diagnostics/TraceContext.cs Fri Apr 18 12:37:48 2014 +0400 @@ -25,18 +25,24 @@ /// public static TraceContext Current { get { - if (_current == null) + if (_current == null) { _current = new TraceContext(); + _current.LogEvent(TraceEventType.Created,"[{0}]", _current.ThreadId); + } return _current; } } - TraceContext(TraceContext context) { + TraceContext(TraceContext context) + : this(context, false) { + } + + TraceContext(TraceContext context, bool attach) { if (context == null) throw new ArgumentNullException("context"); m_currentOperation = context.CurrentOperation; - m_bound = context.CurrentOperation; + m_bound = attach ? context.BoundOperation : context.CurrentOperation; m_threadId = Thread.CurrentThread.ManagedThreadId; } @@ -57,15 +63,15 @@ /// контексте ранее начатые логические операции не могут быть завершены. /// /// - /// Если передача состояния состоялась, то вызывается событие трассировки . + /// Если передача состояния состоялась, то вызывается событие трассировки . /// /// - public static void Transfer(TraceContext from) { + public static void Fork(TraceContext from) { if (_current == from) return; if (from != null) { var context = new TraceContext(from); - context.LogEvent(TraceEventType.Transfer, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); + context.LogEvent(TraceEventType.Fork, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); _current = context; } else { _current = new TraceContext(); @@ -73,7 +79,40 @@ } /// - /// Создает постоянную копию текущего контекста, данную копию можно хранить и использовать для передачи через + /// Задает текущему потоку указанный контекст, текущей поток может заканчивать ранее начатые + /// логические операции в указанном контексте. + /// + /// + public static void Attach(TraceContext source) { + if (_current == source) + return; + if (source != null) { + var context = new TraceContext(source, true); + context.LogEvent(TraceEventType.Attach, "[{0}]-->[{1}]", source.ThreadId, context.ThreadId); + _current = context; + } else { + _current = new TraceContext(); + } + } + + /// + /// Отсоединяет текущий контекст трассировки от потока, для дальнейшей его передачи другому потоку + /// . + /// + /// Контекст трассировки потока + /// + /// После отсоединения контекста трассировки от потока, при первом обращении к трассировке в этом + /// потоке будет создан новый контекст. + /// + public static TraceContext Detach() { + var context = Current; + context.LogEvent(TraceEventType.Detach, null); + _current = null; + return context; + } + + /// + /// Создает постоянную копию текущего контекста, данную копию можно хранить и использовать для передачи через /// /// Копия текущего контекста трассировки. public static TraceContext Snapshot() { @@ -88,7 +127,7 @@ if (action == null) throw new ArgumentNullException("action"); var old = _current; - Transfer(this); + Fork(this); try { action(); } finally { diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Diagnostics/TraceEventType.cs --- a/Implab/Diagnostics/TraceEventType.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Diagnostics/TraceEventType.cs Fri Apr 18 12:37:48 2014 +0400 @@ -11,6 +11,9 @@ Error, OperationStarted, OperationCompleted, - Transfer + Fork, + Attach, + Detach, + Created } } diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Disposable.cs --- a/Implab/Disposable.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Disposable.cs Fri Apr 18 12:37:48 2014 +0400 @@ -1,10 +1,14 @@ -using System; +using Implab.Diagnostics; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Web; namespace Implab { + /// + /// Объект, поддерживающий освобождение ресурсов. + /// public class Disposable : IDisposable { bool m_disposed; @@ -19,7 +23,16 @@ if (m_disposed) throw new ObjectDisposedException(this.ToString()); } - + /// + /// Переводит объект в состояние Disposed и вызывает событие + /// + /// Признак того, что нужно освободить ресурсы, иначе данный метод + /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого + /// объекта. + /// + /// Данный метод осуществляет проверку того, что объект уже был освобожден, чтобы не вызывать + /// событие . Не поддерживает многопоточность. + /// protected virtual void Dispose(bool disposing) { if (disposing && !m_disposed) { m_disposed = true; @@ -34,8 +47,11 @@ GC.SuppressFinalize(this); } + /// + /// Записывает сообщение об утечке объекта. + /// protected virtual void ReportObjectLeaks() { - Trace.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); + TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); } ~Disposable() { diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Implab.csproj --- a/Implab/Implab.csproj Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Implab.csproj Fri Apr 18 12:37:48 2014 +0400 @@ -52,7 +52,6 @@ - diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/ManagedPromise.cs --- a/Implab/ManagedPromise.cs Wed Apr 16 19:02:58 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; - -namespace Implab { - - /*public class ManagedPromise: Promise, ITaskController, IProgressNotifier { - - }*/ -} diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Parallels/ArrayTraits.cs --- a/Implab/Parallels/ArrayTraits.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Parallels/ArrayTraits.cs Fri Apr 18 12:37:48 2014 +0400 @@ -42,7 +42,7 @@ } protected override void Worker() { - TraceContext.Transfer(m_traceContext); + TraceContext.Fork(m_traceContext); base.Worker(); } @@ -99,7 +99,7 @@ } protected override void Worker() { - TraceContext.Transfer(m_traceContext); + TraceContext.Fork(m_traceContext); base.Worker(); } diff -r e5ec543feee3 -r f8cbe84cfdb1 Implab/Parallels/AsyncPool.cs --- a/Implab/Parallels/AsyncPool.cs Wed Apr 16 19:02:58 2014 +0400 +++ b/Implab/Parallels/AsyncPool.cs Fri Apr 18 12:37:48 2014 +0400 @@ -12,12 +12,12 @@ /// public static class AsyncPool { - public static Promise Invoke(Func func) { + public static IPromise Invoke(Func func) { var p = new Promise(); var caller = TraceContext.Snapshot(); ThreadPool.QueueUserWorkItem(param => { - TraceContext.Transfer(caller); + TraceContext.Fork(caller); try { p.Resolve(func()); } catch(Exception e) { @@ -28,13 +28,13 @@ return p; } - public static Promise InvokeNewThread(Func func) { + public static IPromise InvokeNewThread(Func func) { var p = new Promise(); var caller = TraceContext.Snapshot(); var worker = new Thread(() => { - TraceContext.Transfer(caller); + TraceContext.Fork(caller); try { p.Resolve(func()); } catch (Exception e) { @@ -46,5 +46,26 @@ return p; } + + + public static IPromiseBase InvokeNewThread(Action func) { + var p = new Promise(); + + var caller = TraceContext.Snapshot(); + + var worker = new Thread(() => { + TraceContext.Fork(caller); + try { + func(); + p.Resolve(); + } catch (Exception e) { + p.Reject(e); + } + }); + worker.IsBackground = true; + worker.Start(); + + return p; + } } }