@@ -1,107 +1,122 | |||
|
1 | 1 | using Implab.Parallels; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Collections.Generic; |
|
4 | 4 | using System.Linq; |
|
5 | 5 | using System.Text; |
|
6 | 6 | using System.Threading; |
|
7 | 7 | using System.Threading.Tasks; |
|
8 | 8 | using System.Windows.Forms; |
|
9 | 9 | |
|
10 | 10 | namespace Implab.Diagnostics.Interactive |
|
11 | 11 | { |
|
12 |
public class InteractiveListener: |
|
|
12 | public class InteractiveListener: TextListenerBase | |
|
13 | 13 | { |
|
14 | 14 | TraceForm m_form; |
|
15 | 15 | |
|
16 | 16 | SynchronizationContext m_syncGuiThread; |
|
17 | readonly Promise<object> m_guiStarted = new Promise<object>(); | |
|
17 | 18 | |
|
18 | 19 | readonly IPromiseBase m_guiFinished; |
|
19 | readonly Promise<object> m_guiStarted = new Promise<object>(); | |
|
20 | ||
|
21 | 20 | readonly IPromiseBase m_workerFinished = new Promise<object>(); |
|
22 | 21 | |
|
23 | 22 | readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>(); |
|
24 | 23 | readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); |
|
25 | 24 | |
|
26 | 25 | int m_queueLength; |
|
27 | 26 | bool m_exitPending; |
|
28 | 27 | |
|
29 | 28 | readonly object m_pauseLock = new object(); |
|
30 | 29 | bool m_paused; |
|
31 | 30 | readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true); |
|
32 | 31 | |
|
33 | public InteractiveListener() { | |
|
34 |
m_guiFinished = AsyncPool.InvokeNewThread( |
|
|
35 | GuiThread(); | |
|
36 | return 0; | |
|
37 | }); | |
|
32 | public InteractiveListener(bool global) : base(global) { | |
|
33 | m_guiFinished = AsyncPool.InvokeNewThread(GuiThread); | |
|
34 | m_workerFinished = AsyncPool.InvokeNewThread(QueueThread); | |
|
38 | 35 | |
|
39 | 36 | m_guiStarted.Join(); |
|
40 | 37 | } |
|
41 | 38 | |
|
42 | 39 | void GuiThread() { |
|
43 | 40 | m_form = new TraceForm(); // will create SynchronizationContext |
|
41 | ||
|
42 | m_form.PauseEvents += (s,a) => Pause(); | |
|
43 | m_form.ResumeEvents += (s, a) => Resume(); | |
|
44 | ||
|
44 | 45 | m_syncGuiThread = SynchronizationContext.Current; |
|
45 | 46 | m_guiStarted.Resolve(); |
|
46 | 47 | Application.Run(); |
|
47 | 48 | } |
|
48 | 49 | |
|
49 | 50 | void QueueThread() { |
|
50 | 51 | while (!m_exitPending) { |
|
51 | 52 | if (m_paused) |
|
52 | 53 | m_pauseEvent.WaitOne(); |
|
53 | 54 | |
|
54 | 55 | TraceViewItem item; |
|
55 | 56 | if (m_queue.TryDequeue(out item)) { |
|
56 | 57 | Interlocked.Decrement(ref m_queueLength); |
|
57 | 58 | |
|
58 | 59 | m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null); |
|
59 | 60 | } else { |
|
60 | 61 | m_queueEvent.WaitOne(); |
|
61 | 62 | } |
|
62 | 63 | } |
|
63 | 64 | } |
|
64 | 65 | |
|
65 | 66 | public void Pause() { |
|
66 | 67 | // for consistency we need to set this properties atomically |
|
67 | 68 | lock (m_pauseLock) { |
|
68 | 69 | m_pauseEvent.Reset(); |
|
69 | 70 | m_paused = true; |
|
70 | 71 | } |
|
71 | 72 | } |
|
72 | 73 | |
|
73 | 74 | public void Resume() { |
|
74 | 75 | // for consistency we need to set this properties atomically |
|
75 | 76 | lock (m_pauseLock) { |
|
76 | 77 | m_paused = false; |
|
77 | 78 | m_pauseEvent.Set(); |
|
78 | 79 | } |
|
79 | 80 | } |
|
80 | 81 | |
|
81 | 82 | void Enqueue(TraceViewItem item) { |
|
82 | 83 | m_queue.Enqueue(item); |
|
83 | 84 | if (Interlocked.Increment(ref m_queueLength) == 1) |
|
84 | 85 | m_queueEvent.Set(); |
|
85 | 86 | } |
|
86 | 87 | |
|
87 | 88 | public void ShowForm() { |
|
88 | 89 | m_syncGuiThread.Post(x => m_form.Show(), null); |
|
89 | 90 | } |
|
90 | 91 | |
|
91 | 92 | public void HideForm() { |
|
92 | 93 | m_syncGuiThread.Post(x => m_form.Hide(), null); |
|
93 | 94 | } |
|
94 | 95 | |
|
95 | 96 | void Terminate() { |
|
97 | m_exitPending = true; | |
|
98 | Resume(); | |
|
96 | 99 | m_syncGuiThread.Post(x => Application.ExitThread(), null); |
|
97 | 100 | } |
|
98 | 101 | |
|
99 | 102 | protected override void Dispose(bool disposing) { |
|
100 | 103 | if (disposing) { |
|
101 | 104 | Terminate(); |
|
102 | 105 | m_guiFinished.Join(); |
|
103 | 106 | } |
|
104 | 107 | base.Dispose(disposing); |
|
105 | 108 | } |
|
109 | ||
|
110 | protected override void WriteEntry(TraceContext context, EventText text, string channel) { | |
|
111 | var item = new TraceViewItem { | |
|
112 | Indent = text.indent, | |
|
113 | Message = text.content, | |
|
114 | Thread = context.ThreadId, | |
|
115 | Channel = channel, | |
|
116 | Timestamp = Environment.TickCount | |
|
117 | }; | |
|
118 | ||
|
119 | Enqueue(item); | |
|
120 | } | |
|
106 | 121 | } |
|
107 | 122 | } |
@@ -1,122 +1,134 | |||
|
1 | 1 | namespace Implab.Diagnostics.Interactive { |
|
2 | 2 | partial class TraceForm { |
|
3 | 3 | /// <summary> |
|
4 | 4 | /// Required designer variable. |
|
5 | 5 | /// </summary> |
|
6 | 6 | private System.ComponentModel.IContainer components = null; |
|
7 | 7 | |
|
8 | 8 | /// <summary> |
|
9 | 9 | /// Clean up any resources being used. |
|
10 | 10 | /// </summary> |
|
11 | 11 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
12 | 12 | protected override void Dispose(bool disposing) { |
|
13 | 13 | if (disposing && (components != null)) { |
|
14 | 14 | components.Dispose(); |
|
15 | 15 | } |
|
16 | 16 | base.Dispose(disposing); |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | #region Windows Form Designer generated code |
|
20 | 20 | |
|
21 | 21 | /// <summary> |
|
22 | 22 | /// Required method for Designer support - do not modify |
|
23 | 23 | /// the contents of this method with the code editor. |
|
24 | 24 | /// </summary> |
|
25 | 25 | private void InitializeComponent() { |
|
26 | 26 | this.components = new System.ComponentModel.Container(); |
|
27 | 27 | System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); |
|
28 | 28 | System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); |
|
29 | 29 | System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); |
|
30 | 30 | this.eventsDataGrid = new System.Windows.Forms.DataGridView(); |
|
31 | 31 | this.traceViewItemBindingSource = new System.Windows.Forms.BindingSource(this.components); |
|
32 | 32 | this.threadDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); |
|
33 | this.Channel = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |
|
33 | 34 | this.messageDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); |
|
34 | 35 | ((System.ComponentModel.ISupportInitialize)(this.eventsDataGrid)).BeginInit(); |
|
35 | 36 | ((System.ComponentModel.ISupportInitialize)(this.traceViewItemBindingSource)).BeginInit(); |
|
36 | 37 | this.SuspendLayout(); |
|
37 | 38 | // |
|
38 | 39 | // eventsDataGrid |
|
39 | 40 | // |
|
40 | 41 | this.eventsDataGrid.AllowUserToAddRows = false; |
|
41 | 42 | this.eventsDataGrid.AllowUserToDeleteRows = false; |
|
42 | 43 | this.eventsDataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) |
|
43 | 44 | | System.Windows.Forms.AnchorStyles.Left) |
|
44 | 45 | | System.Windows.Forms.AnchorStyles.Right))); |
|
45 | 46 | this.eventsDataGrid.AutoGenerateColumns = false; |
|
46 | 47 | this.eventsDataGrid.BackgroundColor = System.Drawing.SystemColors.Window; |
|
47 | 48 | dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; |
|
48 | 49 | dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))); |
|
49 | 50 | dataGridViewCellStyle1.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); |
|
50 | 51 | dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText; |
|
51 | 52 | dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; |
|
52 | 53 | dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; |
|
53 | 54 | dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True; |
|
54 | 55 | this.eventsDataGrid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; |
|
55 | 56 | this.eventsDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; |
|
56 | 57 | this.eventsDataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { |
|
57 | 58 | this.threadDataGridViewTextBoxColumn, |
|
59 | this.Channel, | |
|
58 | 60 | this.messageDataGridViewTextBoxColumn}); |
|
59 | 61 | this.eventsDataGrid.DataSource = this.traceViewItemBindingSource; |
|
60 | 62 | dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; |
|
61 | 63 | dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window; |
|
62 | 64 | dataGridViewCellStyle3.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); |
|
63 | 65 | dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText; |
|
64 | 66 | dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight; |
|
65 | 67 | dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText; |
|
66 | 68 | dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False; |
|
67 | 69 | this.eventsDataGrid.DefaultCellStyle = dataGridViewCellStyle3; |
|
68 | 70 | this.eventsDataGrid.Location = new System.Drawing.Point(12, 12); |
|
69 | 71 | this.eventsDataGrid.Name = "eventsDataGrid"; |
|
70 | 72 | this.eventsDataGrid.ReadOnly = true; |
|
71 | 73 | this.eventsDataGrid.RowHeadersWidth = 17; |
|
72 | 74 | this.eventsDataGrid.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False; |
|
73 | 75 | this.eventsDataGrid.Size = new System.Drawing.Size(939, 480); |
|
74 | 76 | this.eventsDataGrid.TabIndex = 1; |
|
75 | 77 | this.eventsDataGrid.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.eventsDataGrid_CellFormatting); |
|
76 | 78 | // |
|
77 | 79 | // traceViewItemBindingSource |
|
78 | 80 | // |
|
79 | 81 | this.traceViewItemBindingSource.DataSource = typeof(Implab.Diagnostics.Interactive.TraceViewItem); |
|
80 | 82 | // |
|
81 | 83 | // threadDataGridViewTextBoxColumn |
|
82 | 84 | // |
|
83 | 85 | this.threadDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; |
|
84 | 86 | this.threadDataGridViewTextBoxColumn.DataPropertyName = "Thread"; |
|
85 | 87 | this.threadDataGridViewTextBoxColumn.HeaderText = "TID"; |
|
86 | 88 | this.threadDataGridViewTextBoxColumn.Name = "threadDataGridViewTextBoxColumn"; |
|
87 | 89 | this.threadDataGridViewTextBoxColumn.ReadOnly = true; |
|
88 | 90 | this.threadDataGridViewTextBoxColumn.Width = 5; |
|
89 | 91 | // |
|
92 | // Channel | |
|
93 | // | |
|
94 | this.Channel.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells; | |
|
95 | this.Channel.DataPropertyName = "Channel"; | |
|
96 | this.Channel.HeaderText = "Channel"; | |
|
97 | this.Channel.Name = "Channel"; | |
|
98 | this.Channel.ReadOnly = true; | |
|
99 | this.Channel.Width = 79; | |
|
100 | // | |
|
90 | 101 | // messageDataGridViewTextBoxColumn |
|
91 | 102 | // |
|
92 | 103 | this.messageDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; |
|
93 | 104 | this.messageDataGridViewTextBoxColumn.DataPropertyName = "FormattedMessage"; |
|
94 | 105 | dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False; |
|
95 | 106 | this.messageDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle2; |
|
96 | 107 | this.messageDataGridViewTextBoxColumn.HeaderText = "Message"; |
|
97 | 108 | this.messageDataGridViewTextBoxColumn.Name = "messageDataGridViewTextBoxColumn"; |
|
98 | 109 | this.messageDataGridViewTextBoxColumn.ReadOnly = true; |
|
99 | 110 | // |
|
100 | 111 | // TraceForm |
|
101 | 112 | // |
|
102 | 113 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); |
|
103 | 114 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; |
|
104 | 115 | this.ClientSize = new System.Drawing.Size(963, 504); |
|
105 | 116 | this.Controls.Add(this.eventsDataGrid); |
|
106 | 117 | this.Name = "TraceForm"; |
|
107 | 118 | this.Text = "TraceForm"; |
|
108 | 119 | ((System.ComponentModel.ISupportInitialize)(this.eventsDataGrid)).EndInit(); |
|
109 | 120 | ((System.ComponentModel.ISupportInitialize)(this.traceViewItemBindingSource)).EndInit(); |
|
110 | 121 | this.ResumeLayout(false); |
|
111 | 122 | |
|
112 | 123 | } |
|
113 | 124 | |
|
114 | 125 | #endregion |
|
115 | 126 | |
|
116 | 127 | private System.Windows.Forms.DataGridView eventsDataGrid; |
|
117 | 128 | private System.Windows.Forms.BindingSource traceViewItemBindingSource; |
|
118 | 129 | private System.Windows.Forms.DataGridViewTextBoxColumn threadDataGridViewTextBoxColumn; |
|
130 | private System.Windows.Forms.DataGridViewTextBoxColumn Channel; | |
|
119 | 131 | private System.Windows.Forms.DataGridViewTextBoxColumn messageDataGridViewTextBoxColumn; |
|
120 | 132 | |
|
121 | 133 | } |
|
122 | 134 | } No newline at end of file |
@@ -1,58 +1,53 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.ComponentModel; |
|
4 | 4 | using System.Data; |
|
5 | 5 | using System.Drawing; |
|
6 | 6 | using System.Linq; |
|
7 | 7 | using System.Text; |
|
8 | 8 | using System.Threading.Tasks; |
|
9 | 9 | using System.Windows.Forms; |
|
10 | 10 | |
|
11 | 11 | namespace Implab.Diagnostics.Interactive { |
|
12 | 12 | public partial class TraceForm : Form { |
|
13 | 13 | readonly Dictionary<int, Color> m_threadColors = new Dictionary<int,Color>(); |
|
14 | 14 | readonly Random m_rand = new Random(); |
|
15 | 15 | |
|
16 | public event EventHandler PauseEvents; | |
|
17 | ||
|
18 | public event EventHandler ResumeEvents; | |
|
19 | ||
|
16 | 20 | public TraceForm() { |
|
17 | 21 | InitializeComponent(); |
|
18 | ||
|
19 | 22 | } |
|
20 | 23 | |
|
21 | 24 | protected override void OnFormClosing(FormClosingEventArgs e) { |
|
22 | 25 | base.OnFormClosing(e); |
|
23 | 26 | if (!e.Cancel && e.CloseReason == CloseReason.UserClosing) { |
|
24 | 27 | e.Cancel = true; |
|
25 | 28 | Hide(); |
|
26 | 29 | } |
|
27 | 30 | } |
|
28 | 31 | |
|
29 | public void AddTraceEvent(int indent, int thread, string message) { | |
|
30 | traceViewItemBindingSource.Add(new TraceViewItem { | |
|
31 | Indent = indent, | |
|
32 | Thread = thread, | |
|
33 | Message = message, | |
|
34 | Timestamp = Environment.TickCount | |
|
35 | }); | |
|
36 | ||
|
37 | } | |
|
38 | ||
|
39 | 32 | public void AddTraceEvent(TraceViewItem item) { |
|
40 | 33 | traceViewItemBindingSource.Add(item); |
|
34 | eventsDataGrid.FirstDisplayedScrollingRowIndex = eventsDataGrid.RowCount - 1; | |
|
41 | 35 | } |
|
42 | 36 | |
|
43 | 37 | Color GetThreadColor(int thread) { |
|
44 | 38 | Color result; |
|
45 | 39 | if (!m_threadColors.TryGetValue(thread, out result)) { |
|
46 | 40 | result = Color.FromArgb(m_rand.Next(4)*64, m_rand.Next(4)*64, m_rand.Next(4)*64); |
|
47 | 41 | m_threadColors[thread] = result; |
|
48 | 42 | } |
|
49 | 43 | return result; |
|
50 | 44 | } |
|
51 | 45 | |
|
52 | 46 | private void eventsDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { |
|
53 | 47 | var data = (TraceViewItem)traceViewItemBindingSource[e.RowIndex]; |
|
54 | e.CellStyle.Padding = new Padding(data.Indent * 10,0,0,0); | |
|
48 | if (e.ColumnIndex == messageDataGridViewTextBoxColumn.Index) | |
|
49 | e.CellStyle.Padding = new Padding(data.Indent * 10,0,0,0); | |
|
55 | 50 | e.CellStyle.ForeColor = GetThreadColor(data.Thread); |
|
56 | 51 | } |
|
57 | 52 | } |
|
58 | 53 | } |
@@ -1,123 +1,126 | |||
|
1 | 1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <root> |
|
3 | 3 | <!-- |
|
4 | 4 | Microsoft ResX Schema |
|
5 | 5 | |
|
6 | 6 | Version 2.0 |
|
7 | 7 | |
|
8 | 8 | The primary goals of this format is to allow a simple XML format |
|
9 | 9 | that is mostly human readable. The generation and parsing of the |
|
10 | 10 | various data types are done through the TypeConverter classes |
|
11 | 11 | associated with the data types. |
|
12 | 12 | |
|
13 | 13 | Example: |
|
14 | 14 | |
|
15 | 15 | ... ado.net/XML headers & schema ... |
|
16 | 16 | <resheader name="resmimetype">text/microsoft-resx</resheader> |
|
17 | 17 | <resheader name="version">2.0</resheader> |
|
18 | 18 | <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> |
|
19 | 19 | <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> |
|
20 | 20 | <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data> |
|
21 | 21 | <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> |
|
22 | 22 | <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"> |
|
23 | 23 | <value>[base64 mime encoded serialized .NET Framework object]</value> |
|
24 | 24 | </data> |
|
25 | 25 | <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
|
26 | 26 | <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> |
|
27 | 27 | <comment>This is a comment</comment> |
|
28 | 28 | </data> |
|
29 | 29 | |
|
30 | 30 | There are any number of "resheader" rows that contain simple |
|
31 | 31 | name/value pairs. |
|
32 | 32 | |
|
33 | 33 | Each data row contains a name, and value. The row also contains a |
|
34 | 34 | type or mimetype. Type corresponds to a .NET class that support |
|
35 | 35 | text/value conversion through the TypeConverter architecture. |
|
36 | 36 | Classes that don't support this are serialized and stored with the |
|
37 | 37 | mimetype set. |
|
38 | 38 | |
|
39 | 39 | The mimetype is used for serialized objects, and tells the |
|
40 | 40 | ResXResourceReader how to depersist the object. This is currently not |
|
41 | 41 | extensible. For a given mimetype the value must be set accordingly: |
|
42 | 42 | |
|
43 | 43 | Note - application/x-microsoft.net.object.binary.base64 is the format |
|
44 | 44 | that the ResXResourceWriter will generate, however the reader can |
|
45 | 45 | read any of the formats listed below. |
|
46 | 46 | |
|
47 | 47 | mimetype: application/x-microsoft.net.object.binary.base64 |
|
48 | 48 | value : The object must be serialized with |
|
49 | 49 | : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter |
|
50 | 50 | : and then encoded with base64 encoding. |
|
51 | 51 | |
|
52 | 52 | mimetype: application/x-microsoft.net.object.soap.base64 |
|
53 | 53 | value : The object must be serialized with |
|
54 | 54 | : System.Runtime.Serialization.Formatters.Soap.SoapFormatter |
|
55 | 55 | : and then encoded with base64 encoding. |
|
56 | 56 | |
|
57 | 57 | mimetype: application/x-microsoft.net.object.bytearray.base64 |
|
58 | 58 | value : The object must be serialized into a byte array |
|
59 | 59 | : using a System.ComponentModel.TypeConverter |
|
60 | 60 | : and then encoded with base64 encoding. |
|
61 | 61 | --> |
|
62 | 62 | <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> |
|
63 | 63 | <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> |
|
64 | 64 | <xsd:element name="root" msdata:IsDataSet="true"> |
|
65 | 65 | <xsd:complexType> |
|
66 | 66 | <xsd:choice maxOccurs="unbounded"> |
|
67 | 67 | <xsd:element name="metadata"> |
|
68 | 68 | <xsd:complexType> |
|
69 | 69 | <xsd:sequence> |
|
70 | 70 | <xsd:element name="value" type="xsd:string" minOccurs="0" /> |
|
71 | 71 | </xsd:sequence> |
|
72 | 72 | <xsd:attribute name="name" use="required" type="xsd:string" /> |
|
73 | 73 | <xsd:attribute name="type" type="xsd:string" /> |
|
74 | 74 | <xsd:attribute name="mimetype" type="xsd:string" /> |
|
75 | 75 | <xsd:attribute ref="xml:space" /> |
|
76 | 76 | </xsd:complexType> |
|
77 | 77 | </xsd:element> |
|
78 | 78 | <xsd:element name="assembly"> |
|
79 | 79 | <xsd:complexType> |
|
80 | 80 | <xsd:attribute name="alias" type="xsd:string" /> |
|
81 | 81 | <xsd:attribute name="name" type="xsd:string" /> |
|
82 | 82 | </xsd:complexType> |
|
83 | 83 | </xsd:element> |
|
84 | 84 | <xsd:element name="data"> |
|
85 | 85 | <xsd:complexType> |
|
86 | 86 | <xsd:sequence> |
|
87 | 87 | <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> |
|
88 | 88 | <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> |
|
89 | 89 | </xsd:sequence> |
|
90 | 90 | <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> |
|
91 | 91 | <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> |
|
92 | 92 | <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> |
|
93 | 93 | <xsd:attribute ref="xml:space" /> |
|
94 | 94 | </xsd:complexType> |
|
95 | 95 | </xsd:element> |
|
96 | 96 | <xsd:element name="resheader"> |
|
97 | 97 | <xsd:complexType> |
|
98 | 98 | <xsd:sequence> |
|
99 | 99 | <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> |
|
100 | 100 | </xsd:sequence> |
|
101 | 101 | <xsd:attribute name="name" type="xsd:string" use="required" /> |
|
102 | 102 | </xsd:complexType> |
|
103 | 103 | </xsd:element> |
|
104 | 104 | </xsd:choice> |
|
105 | 105 | </xsd:complexType> |
|
106 | 106 | </xsd:element> |
|
107 | 107 | </xsd:schema> |
|
108 | 108 | <resheader name="resmimetype"> |
|
109 | 109 | <value>text/microsoft-resx</value> |
|
110 | 110 | </resheader> |
|
111 | 111 | <resheader name="version"> |
|
112 | 112 | <value>2.0</value> |
|
113 | 113 | </resheader> |
|
114 | 114 | <resheader name="reader"> |
|
115 | 115 | <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> |
|
116 | 116 | </resheader> |
|
117 | 117 | <resheader name="writer"> |
|
118 | 118 | <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> |
|
119 | 119 | </resheader> |
|
120 | <metadata name="Channel.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |
|
121 | <value>True</value> | |
|
122 | </metadata> | |
|
120 | 123 | <metadata name="traceViewItemBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> |
|
121 | 124 | <value>17, 17</value> |
|
122 | 125 | </metadata> |
|
123 | 126 | </root> No newline at end of file |
@@ -1,25 +1,26 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading.Tasks; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Diagnostics.Interactive { |
|
8 | 8 | public class TraceViewItem { |
|
9 | 9 | string m_formattedValue; |
|
10 | 10 | |
|
11 | 11 | public string Message { get; set; } |
|
12 | 12 | public int Timestamp { get; set; } |
|
13 | 13 | public int Indent { get; set; } |
|
14 | 14 | public int Thread { get; set; } |
|
15 | public string Channel { get; set; } | |
|
15 | 16 | |
|
16 | 17 | public string FormattedMessage { |
|
17 | 18 | get { |
|
18 | 19 | if (m_formattedValue == null) { |
|
19 | 20 | m_formattedValue = Message.Replace("\r",String.Empty).Replace("\n", " | "); |
|
20 | 21 | } |
|
21 | 22 | return m_formattedValue; |
|
22 | 23 | } |
|
23 | 24 | } |
|
24 | 25 | } |
|
25 | 26 | } |
@@ -1,34 +1,34 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Diagnostics { |
|
7 | 7 | public class ConsoleTraceListener: TextListenerBase { |
|
8 | 8 | |
|
9 | 9 | static readonly object _consoleLock = new object(); |
|
10 | 10 | |
|
11 | 11 | public ConsoleTraceListener() |
|
12 | 12 | : base(true) { |
|
13 | 13 | |
|
14 | 14 | } |
|
15 | 15 | |
|
16 |
public ConsoleTraceListener(bool |
|
|
17 |
: base( |
|
|
16 | public ConsoleTraceListener(bool global) | |
|
17 | : base(global) { | |
|
18 | 18 | |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | protected override void WriteEntry(TraceContext context, EventText text) { | |
|
21 | protected override void WriteEntry(TraceContext context, EventText text, string channel) { | |
|
22 | 22 | var msg = new StringBuilder(); |
|
23 | 23 | |
|
24 | 24 | for (int i = 0; i < text.indent; i++) |
|
25 | 25 | msg.Append(" "); |
|
26 |
msg.AppendFormat("[{0}]: |
|
|
26 | msg.AppendFormat("[{0}]:{1}: {2}", context.ThreadId, channel, text.content); | |
|
27 | 27 | |
|
28 | 28 | lock (_consoleLock) { |
|
29 | 29 | Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1); |
|
30 | 30 | Console.WriteLine(msg.ToString()); |
|
31 | 31 | } |
|
32 | 32 | } |
|
33 | 33 | } |
|
34 | 34 | } |
@@ -1,30 +1,81 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Diagnostics { |
|
7 | /// <summary> | |
|
8 | /// ΠΠ°Π½Π°Π», ΡΠ΅ΡΠ΅Π· ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠ±Π»ΠΈΠΊΡΡΡΡΡ ΡΠΎΠ±ΡΡΠΈΡ ΠΆΡΡΠ½Π°Π»Π°. | |
|
9 | /// </summary> | |
|
10 | /// <typeparam name="TEvent">Π’ΠΈΠΏ ΡΠΎΠ±ΡΡΠΈΠΉ Π² ΠΊΠ°Π½Π°Π»Π΅</typeparam> | |
|
11 | /// <remarks> | |
|
12 | /// Π‘ΠΎΠ±ΡΡΠΈΡΠΌΠΈ ΠΆΡΡΠ½Π°Π»Π° ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π»ΡΠ±ΡΠ΅ ΡΠΈΠΏΡ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΡΡΡΠΎΠΊΠΈ, Π² ΠΊΠΎΡΠΎΡΡΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π²Π°ΡΡΡΡ | |
|
13 | /// ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ, ΠΈΠ»ΠΈ ΡΡΡΡΠΊΡΡΡΡ Ρ Π½Π°Π±ΠΎΡΠΎΠΌ ΠΏΠΎΠ»Π΅ΠΉ, ΠΎΠΏΠΈΡΡΠ²Π°ΡΡΠΈΡ Π²Π°ΠΆΠ½ΠΎΡΡΡ, ΡΠ΅ΠΊΡΡ ΠΈ Π΄ΡΡΠ³ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ. | |
|
14 | /// </remarks> | |
|
7 | 15 | public class LogChannel<TEvent> { |
|
8 | 16 | static LogChannel<TEvent> _default = new LogChannel<TEvent>(); |
|
9 | 17 | |
|
18 | /// <summary> | |
|
19 | /// ΠΠ°Π½Π°Π» ΠΏΠΎ-ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ Π΄Π»Ρ ΡΠΎΠ±ΡΡΠΈΠΉ ΡΠΈΠΏΠ° <typeparam name="TEvent"/>. | |
|
20 | /// </summary> | |
|
10 | 21 | public static LogChannel<TEvent> Default { |
|
11 | 22 | get { |
|
12 | 23 | return _default; |
|
13 | 24 | } |
|
14 | 25 | } |
|
15 | 26 | |
|
27 | /// <summary> | |
|
28 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΡΠ²Π»Π΅Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠΉ Π·Π°ΠΏΠΈΡΠΈ Π² ΠΆΡΡΠ½Π°Π»Π΅, Π½Π° ΡΡΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΡΠ²Π°ΡΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΠΈ. | |
|
29 | /// </summary> | |
|
16 | 30 | public event EventHandler<ValueEventArgs<TEvent>> Events; |
|
31 | ||
|
32 | /// <summary> | |
|
33 | /// ΠΠΌΡ ΠΊΠ°Π½Π°Π»Π°, ΠΏΠΎΠ»Π΅Π·Π½ΠΎ Π΄Π»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΆΡΡΠ½Π°Π»Π΅ | |
|
34 | /// </summary> | |
|
35 | public string Name { | |
|
36 | get; | |
|
37 | private set; | |
|
38 | } | |
|
17 | 39 | |
|
40 | /// <summary> | |
|
41 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΆΡΡΠ½Π°Π», ΠΈΠΌΡ ΡΠΈΠΏΠ° ΡΠΎΠ±ΡΡΠΈΠΉ Π½Π°Π·Π½Π°ΡΠ°Π΅ΡΡΡ Π² ΠΊΠ°ΡΠ΅ΡΠ²Π΅ ΠΈΠΌΠ΅Π½ΠΈ ΠΊΠ°Π½Π°Π»Π°. | |
|
42 | /// </summary> | |
|
43 | public LogChannel() | |
|
44 | : this(null) { | |
|
45 | } | |
|
46 | ||
|
47 | /// <summary> | |
|
48 | /// Π‘ΠΎΠ΄Π°Π΅Ρ ΠΊΠ°Π½Π°Π» Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ. | |
|
49 | /// </summary> | |
|
50 | /// <param name="name">ΠΠΌΡ ΠΊΠ°Π½Π°Π»Π°.</param> | |
|
51 | public LogChannel(string name) { | |
|
52 | if (String.IsNullOrEmpty(name)) | |
|
53 | name = typeof(TEvent).Name; | |
|
54 | Name = name; | |
|
55 | } | |
|
56 | ||
|
57 | /// <summary> | |
|
58 | /// ΠΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ Π·Π°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π° ΡΠ΅ΡΠ΅Π· ΠΊΠ°Π½Π°Π» ΠΏΠΎΠ΄ΠΏΠΈΡΡΠΈΠΊΠ°ΠΌ. | |
|
59 | /// </summary> | |
|
60 | /// <param name="data">ΠΠ°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π°.</param> | |
|
61 | /// <remarks> | |
|
62 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ°ΡΡΡΠ»Π°Π΅ΡΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΡΡΡ Π°Π²ΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΈΠ· ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΏΠΎΡΠΎΠΊΠ°. | |
|
63 | /// </remarks> | |
|
18 | 64 | public void LogEvent(TEvent data) { |
|
19 | 65 | var t = Events; |
|
20 | 66 | if (t!= null) |
|
21 | 67 | t(TraceContext.Current,new ValueEventArgs<TEvent>(data)); |
|
22 | 68 | } |
|
23 | 69 | |
|
70 | /// <summary> | |
|
71 | /// ΠΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ Π·Π°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π° ΡΠ΅ΡΠ΅Π· ΠΊΠ°Π½Π°Π» ΠΏΠΎΠ΄ΠΏΠΈΡΡΠΈΠΊΠ°ΠΌ. | |
|
72 | /// </summary> | |
|
73 | /// <param name="data">ΠΠ°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π°.</param> | |
|
74 | /// <param name="context">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ°ΡΡΡΠ»Π°Π΅ΡΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅/</param> | |
|
24 | 75 | public void LogEvent(TraceContext context,TEvent data) { |
|
25 | 76 | var t = Events; |
|
26 | 77 | if (t != null) |
|
27 | 78 | t(context, new ValueEventArgs<TEvent>(data)); |
|
28 | 79 | } |
|
29 | 80 | } |
|
30 | 81 | } |
@@ -1,46 +1,47 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.IO; |
|
4 | 4 | using System.Linq; |
|
5 | 5 | using System.Text; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Diagnostics { |
|
8 | 8 | public class TextFileListener: TextListenerBase { |
|
9 | 9 | readonly TextWriter m_textWriter; |
|
10 | 10 | |
|
11 |
public TextFileListener(string fileName, bool |
|
|
11 | public TextFileListener(string fileName, bool global) | |
|
12 | : base(global) { | |
|
12 | 13 | m_textWriter = File.CreateText(fileName); |
|
13 | 14 | |
|
14 | 15 | m_textWriter.WriteLine("LOG {0}", DateTime.Now); |
|
15 | 16 | Register(this); |
|
16 | 17 | } |
|
17 | 18 | |
|
18 | protected override void WriteEntry(TraceContext context, EventText text) { | |
|
19 | protected override void WriteEntry(TraceContext context, EventText text, string channel) { | |
|
19 | 20 | var msg = new StringBuilder(); |
|
20 | 21 | for (int i = 0; i < text.indent; i++) |
|
21 | 22 | msg.Append(" "); |
|
22 |
msg.AppendFormat("[{0}]: |
|
|
23 | msg.AppendFormat("[{0}]:{1}: {2}", context.ThreadId, channel, text.content); | |
|
23 | 24 | |
|
24 | 25 | lock (m_textWriter) { |
|
25 | 26 | if (!IsDisposed) { |
|
26 | 27 | // ΡΡΡ Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°Π½ΠΎ Π΅ΡΠ΅ Π½Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ m_textWriter |
|
27 | 28 | m_textWriter.WriteLine(msg.ToString()); |
|
28 | 29 | m_textWriter.Flush(); |
|
29 | 30 | } |
|
30 | 31 | } |
|
31 | 32 | } |
|
32 | 33 | |
|
33 | 34 | |
|
34 | 35 | protected override void Dispose(bool disposing) { |
|
35 | 36 | base.Dispose(disposing); |
|
36 | 37 | if (disposing) { |
|
37 | 38 | // IsDisposed = true |
|
38 | 39 | lock (m_textWriter) { |
|
39 | 40 | Safe.Dispose(m_textWriter); |
|
40 | 41 | } |
|
41 | 42 | } |
|
42 | 43 | } |
|
43 | 44 | |
|
44 | 45 | |
|
45 | 46 | } |
|
46 | 47 | } |
@@ -1,128 +1,129 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Diagnostics { |
|
7 | 7 | public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> { |
|
8 | 8 | |
|
9 | 9 | readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>(); |
|
10 | 10 | readonly LogicalOperation m_boundOperation; |
|
11 | 11 | readonly int m_baseIndent; |
|
12 | 12 | |
|
13 |
protected TextListenerBase(bool |
|
|
13 | protected TextListenerBase(bool global) { | |
|
14 | 14 | Register(this); |
|
15 |
if ( |
|
|
15 | if (!global) { | |
|
16 | 16 | m_boundOperation = TraceContext.Current.CurrentOperation; |
|
17 | 17 | m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); |
|
18 | 18 | } |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | public void Subscribe(Type eventType) { |
|
22 | 22 | if (eventType == null) |
|
23 | 23 | throw new ArgumentNullException("eventType"); |
|
24 | 24 | GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null); |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | public void Subscribe<TEvent>() { |
|
28 | 28 | Subscribe<TEvent>(LogChannel<TEvent>.Default); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | public void Subscribe<TEvent>(LogChannel<TEvent> channel) { |
|
32 | 32 | if (channel == null) |
|
33 | 33 | throw new ArgumentNullException("channel"); |
|
34 | 34 | |
|
35 | 35 | lock (m_subscriptions) { |
|
36 | 36 | AssertNotDisposed(); |
|
37 | 37 | |
|
38 | 38 | var formatter = GetService<IEventTextFormatter<TEvent>>(); |
|
39 | var channelName = channel.Name; | |
|
39 | 40 | |
|
40 | 41 | EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => { |
|
41 | 42 | TraceContext context = (TraceContext)sender; |
|
42 | 43 | var text = formatter.Format(context, args.Value); |
|
43 | 44 | text.indent -= m_baseIndent; |
|
44 | 45 | |
|
45 | 46 | if (IsRelated(context.CurrentOperation)) |
|
46 | WriteEntry(context, text); | |
|
47 | WriteEntry(context, text, channelName); | |
|
47 | 48 | }; |
|
48 | 49 | |
|
49 | 50 | if (m_subscriptions.ContainsKey(channel)) |
|
50 | 51 | return; |
|
51 | 52 | |
|
52 | 53 | channel.Events += handler; |
|
53 | 54 | |
|
54 | 55 | Action unsubscribe = () => { |
|
55 | 56 | channel.Events -= handler; |
|
56 | 57 | }; |
|
57 | 58 | |
|
58 | 59 | m_subscriptions.Add(channel, unsubscribe); |
|
59 | 60 | } |
|
60 | 61 | } |
|
61 | 62 | |
|
62 | 63 | public bool IsRelated(LogicalOperation op) { |
|
63 | 64 | if (m_boundOperation == null) |
|
64 | 65 | return true; |
|
65 | 66 | |
|
66 | 67 | while (op != m_boundOperation && op.Level > m_boundOperation.Level) |
|
67 | 68 | op = op.Parent; |
|
68 | 69 | return op == m_boundOperation; |
|
69 | 70 | } |
|
70 | 71 | |
|
71 | 72 | public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) { |
|
72 | 73 | if (channel == null) |
|
73 | 74 | throw new ArgumentNullException("channel"); |
|
74 | 75 | |
|
75 | 76 | lock (m_subscriptions) { |
|
76 | 77 | Action subscription; |
|
77 | 78 | if (m_subscriptions.TryGetValue(channel, out subscription)) { |
|
78 | 79 | subscription(); |
|
79 | 80 | m_subscriptions.Remove(channel); |
|
80 | 81 | } |
|
81 | 82 | } |
|
82 | 83 | } |
|
83 | 84 | |
|
84 | 85 | public void UnsubscribeAll() { |
|
85 | 86 | lock (m_subscriptions) { |
|
86 | 87 | foreach (var subscription in m_subscriptions.Values) |
|
87 | 88 | subscription(); |
|
88 | 89 | m_subscriptions.Clear(); |
|
89 | 90 | } |
|
90 | 91 | } |
|
91 | 92 | |
|
92 | 93 | /// <summary> |
|
93 | 94 | /// ΠΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π΄Π»Ρ Π·Π°ΠΏΠΈΡΠΈ ΡΠ΅ΠΊΡΡΠ° ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ, Π² ΠΆΡΡΠ½Π°Π». |
|
94 | 95 | /// </summary> |
|
95 | 96 | /// <remarks> |
|
96 | 97 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ·Π²Π°ΡΡΡΡ ΠΈΠ· ΡΠ°Π·Π½ΡΡ ΠΏΠΎΡΠΎΠΊΠΎΠ² ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ. ΠΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΡΠΈΡΡΠ°ΡΠΈΡ, ΠΊΠΎΠ³Π΄Π° |
|
97 | 98 | /// Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΆΠ΅ ΠΏΠΎΡΠ»Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΎΠΎΠ±ΡΠ΅ΠΊΡΠ° ΠΌΠ΅ΡΠΎΠ΄ΠΎΠΌ <see cref="Dispose()"/>. |
|
98 | 99 | /// </remarks> |
|
99 | 100 | /// <param name="context">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</param> |
|
100 | 101 | /// <param name="text">Π’Π΅ΠΊΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ.</param> |
|
101 | protected abstract void WriteEntry(TraceContext context, EventText text); | |
|
102 | protected abstract void WriteEntry(TraceContext context, EventText text, string channel); | |
|
102 | 103 | |
|
103 | 104 | public EventText Format(TraceContext context, object data) { |
|
104 | 105 | return new EventText { |
|
105 | 106 | indent = context.CurrentOperation.Level, |
|
106 | 107 | content = data.ToString() |
|
107 | 108 | }; |
|
108 | 109 | } |
|
109 | 110 | |
|
110 | 111 | public EventText Format(TraceContext context, TraceEvent data) { |
|
111 | 112 | var level = context.CurrentOperation.Level; |
|
112 | 113 | if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) |
|
113 | 114 | level--; |
|
114 | 115 | |
|
115 | 116 | return new EventText { |
|
116 | 117 | indent = level, |
|
117 | 118 | content = data.ToString() |
|
118 | 119 | }; |
|
119 | 120 | } |
|
120 | 121 | |
|
121 | 122 | protected override void Dispose(bool disposing) { |
|
122 | 123 | base.Dispose(disposing); |
|
123 | 124 | if (disposing) { |
|
124 | 125 | UnsubscribeAll(); |
|
125 | 126 | } |
|
126 | 127 | } |
|
127 | 128 | } |
|
128 | 129 | } |
@@ -1,172 +1,211 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Threading.Tasks; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Diagnostics { |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΊ ΠΏΠΎΡΠΎΠΊΡ ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. |
|
11 | 11 | /// </summary> |
|
12 | 12 | /// <remarks> |
|
13 | 13 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΡΠΌ ΡΠΎΠ±ΡΡΠΈΠΉ Π΄Π»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠ΅ΡΡΠ°, Π³Π΄Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅. |
|
14 | 14 | /// </remarks> |
|
15 | 15 | public class TraceContext { |
|
16 | 16 | LogicalOperation m_currentOperation; |
|
17 | 17 | readonly LogicalOperation m_bound; |
|
18 | 18 | readonly int m_threadId; |
|
19 | 19 | |
|
20 | 20 | [ThreadStatic] |
|
21 | 21 | static TraceContext _current; |
|
22 | 22 | |
|
23 | 23 | /// <summary> |
|
24 | 24 | /// Π’Π΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ Π΄Π»Ρ ΠΏΠΎΡΠΎΠΊΠ°, ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ Π°ΡΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ. |
|
25 | 25 | /// </summary> |
|
26 | 26 | public static TraceContext Current { |
|
27 | 27 | get { |
|
28 | if (_current == null) | |
|
28 | if (_current == null) { | |
|
29 | 29 | _current = new TraceContext(); |
|
30 | _current.LogEvent(TraceEventType.Created,"[{0}]", _current.ThreadId); | |
|
31 | } | |
|
30 | 32 | return _current; |
|
31 | 33 | } |
|
32 | 34 | } |
|
33 | 35 | |
|
34 |
TraceContext(TraceContext context) |
|
|
36 | TraceContext(TraceContext context) | |
|
37 | : this(context, false) { | |
|
38 | } | |
|
39 | ||
|
40 | TraceContext(TraceContext context, bool attach) { | |
|
35 | 41 | if (context == null) |
|
36 | 42 | throw new ArgumentNullException("context"); |
|
37 | 43 | |
|
38 | 44 | m_currentOperation = context.CurrentOperation; |
|
39 | m_bound = context.CurrentOperation; | |
|
45 | m_bound = attach ? context.BoundOperation : context.CurrentOperation; | |
|
40 | 46 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
41 | 47 | } |
|
42 | 48 | |
|
43 | 49 | TraceContext() { |
|
44 | 50 | m_currentOperation = new LogicalOperation(); |
|
45 | 51 | m_bound = m_currentOperation; |
|
46 | 52 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
47 | 53 | } |
|
48 | 54 | |
|
49 | 55 | /// <summary> |
|
50 | 56 | /// ΠΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ ΠΊΠΎΠΏΠΈΡΡΠ΅Ρ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΠ²ΡΠΎΠ²ΠΊΠΈ Π² ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ. |
|
51 | 57 | /// </summary> |
|
52 | 58 | /// <param name="from">ΠΡΡ ΠΎΠ΄Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ.</param> |
|
53 | 59 | /// <remarks> |
|
54 | 60 | /// <para> |
|
55 | 61 | /// ΠΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ Π·Π° ΡΡΠ΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΈ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ Π΅Π³ΠΎ |
|
56 | 62 | /// ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°. ΠΡΠΈ ΡΡΠΎΠΌ ΠΊΠΎΠΏΠΈΡΡΠ΅ΡΡΡ ΡΡΠ΅ΠΊ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π½ΠΎΠ²ΠΎΠΌ |
|
57 | 63 | /// ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Ρ. |
|
58 | 64 | /// </para> |
|
59 | 65 | /// <para> |
|
60 |
/// ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΡΠΎΡΡΠΎΡΠ»Π°ΡΡ, ΡΠΎ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ <see cref="TraceEventType. |
|
|
66 | /// ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΡΠΎΡΡΠΎΡΠ»Π°ΡΡ, ΡΠΎ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ <see cref="TraceEventType.Fork"/>. | |
|
61 | 67 | /// </para> |
|
62 | 68 | /// </remarks> |
|
63 |
public static void |
|
|
69 | public static void Fork(TraceContext from) { | |
|
64 | 70 | if (_current == from) |
|
65 | 71 | return; |
|
66 | 72 | if (from != null) { |
|
67 | 73 | var context = new TraceContext(from); |
|
68 |
context.LogEvent(TraceEventType. |
|
|
74 | context.LogEvent(TraceEventType.Fork, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); | |
|
69 | 75 | _current = context; |
|
70 | 76 | } else { |
|
71 | 77 | _current = new TraceContext(); |
|
72 | 78 | } |
|
73 | 79 | } |
|
74 | 80 | |
|
75 | 81 | /// <summary> |
|
76 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΏΠΎΡΡΠΎΡΠ½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π΄Π°Π½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ Ρ ΡΠ°Π½ΠΈΡΡ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ ΡΠ΅ΡΠ΅Π· <see cref="Transfer(TraceContext)"/> | |
|
82 | /// ΠΠ°Π΄Π°Π΅Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΌΡ ΠΏΠΎΡΠΎΠΊΡ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ, ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΏΠΎΡΠΎΠΊ ΠΌΠΎΠΆΠ΅Ρ Π·Π°ΠΊΠ°Π½ΡΠΈΠ²Π°ΡΡ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ | |
|
83 | /// Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
84 | /// </summary> | |
|
85 | /// <param name="source"></param> | |
|
86 | public static void Attach(TraceContext source) { | |
|
87 | if (_current == source) | |
|
88 | return; | |
|
89 | if (source != null) { | |
|
90 | var context = new TraceContext(source, true); | |
|
91 | context.LogEvent(TraceEventType.Attach, "[{0}]-->[{1}]", source.ThreadId, context.ThreadId); | |
|
92 | _current = context; | |
|
93 | } else { | |
|
94 | _current = new TraceContext(); | |
|
95 | } | |
|
96 | } | |
|
97 | ||
|
98 | /// <summary> | |
|
99 | /// ΠΡΡΠΎΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΏΠΎΡΠΎΠΊΠ°, Π΄Π»Ρ Π΄Π°Π»ΡΠ½Π΅ΠΉΡΠ΅ΠΉ Π΅Π³ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ Π΄ΡΡΠ³ΠΎΠΌΡ ΠΏΠΎΡΠΎΠΊΡ | |
|
100 | /// <see cref="Attach(TraceContext)"/>. | |
|
101 | /// </summary> | |
|
102 | /// <returns>ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°</returns> | |
|
103 | /// <remarks> | |
|
104 | /// ΠΠΎΡΠ»Π΅ ΠΎΡΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΏΠΎΡΠΎΠΊΠ°, ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠ΅ Π² ΡΡΠΎΠΌ | |
|
105 | /// ΠΏΠΎΡΠΎΠΊΠ΅ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ Π½ΠΎΠ²ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ. | |
|
106 | /// </remarks> | |
|
107 | public static TraceContext Detach() { | |
|
108 | var context = Current; | |
|
109 | context.LogEvent(TraceEventType.Detach, null); | |
|
110 | _current = null; | |
|
111 | return context; | |
|
112 | } | |
|
113 | ||
|
114 | /// <summary> | |
|
115 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΏΠΎΡΡΠΎΡΠ½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π΄Π°Π½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ Ρ ΡΠ°Π½ΠΈΡΡ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ ΡΠ΅ΡΠ΅Π· <see cref="Fork(TraceContext)"/> | |
|
77 | 116 | /// </summary> |
|
78 | 117 | /// <returns>ΠΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</returns> |
|
79 | 118 | public static TraceContext Snapshot() { |
|
80 | 119 | return _current == null ? new TraceContext() : new TraceContext(_current); |
|
81 | 120 | } |
|
82 | 121 | |
|
83 | 122 | /// <summary> |
|
84 | 123 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΠΈ Π²ΠΎΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°. |
|
85 | 124 | /// </summary> |
|
86 | 125 | /// <param name="action"></param> |
|
87 | 126 | public void Invoke(Action action) { |
|
88 | 127 | if (action == null) |
|
89 | 128 | throw new ArgumentNullException("action"); |
|
90 | 129 | var old = _current; |
|
91 |
|
|
|
130 | Fork(this); | |
|
92 | 131 | try { |
|
93 | 132 | action(); |
|
94 | 133 | } finally { |
|
95 | 134 | _current.EndAllOperations(); |
|
96 | 135 | _current = old; |
|
97 | 136 | } |
|
98 | 137 | } |
|
99 | 138 | |
|
100 | 139 | /// <summary> |
|
101 | 140 | /// Π’Π΅ΠΊΡΡΠ°Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
102 | 141 | /// </summary> |
|
103 | 142 | public LogicalOperation CurrentOperation { |
|
104 | 143 | get { |
|
105 | 144 | return m_currentOperation; |
|
106 | 145 | } |
|
107 | 146 | } |
|
108 | 147 | |
|
109 | 148 | /// <summary> |
|
110 | 149 | /// ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π½ΠΈΠΆΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π΅Π»ΡΠ·Ρ ΠΎΠΏΡΡΠΊΠ°ΡΡΡΡ Π² ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, Ρ.Π΅. ΠΎΠ½Π° Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Π° Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
111 | 150 | /// </summary> |
|
112 | 151 | public LogicalOperation BoundOperation { |
|
113 | 152 | get { |
|
114 | 153 | return m_bound; |
|
115 | 154 | } |
|
116 | 155 | } |
|
117 | 156 | |
|
118 | 157 | /// <summary> |
|
119 | 158 | /// ΠΠΎΡΠΎΠΊ, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ. |
|
120 | 159 | /// </summary> |
|
121 | 160 | public int ThreadId { |
|
122 | 161 | get { |
|
123 | 162 | return m_threadId; |
|
124 | 163 | } |
|
125 | 164 | } |
|
126 | 165 | |
|
127 | 166 | /// <summary> |
|
128 | 167 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π±Π΅Π·ΡΠΌΡΠ½Π½ΡΡ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
129 | 168 | /// </summary> |
|
130 | 169 | public void StartLogicalOperation() { |
|
131 | 170 | StartLogicalOperation(null); |
|
132 | 171 | } |
|
133 | 172 | |
|
134 | 173 | /// <summary> |
|
135 | 174 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ. Π‘ΠΎΠ·Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π΄ΠΎΠ±Π²Π°Π»Π΅Π½Π° Π² ΡΡΠ΅ΠΊ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π·Π°ΡΠ΅ΠΌ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΎ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠ΅Π΅ ΡΠΎΠ±ΡΡΠΈΠ΅. |
|
136 | 175 | /// </summary> |
|
137 | 176 | /// <param name="name">ΠΠΌΡ Π½Π°ΡΠΈΠ½Π°Π΅ΠΌΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
138 | 177 | public void StartLogicalOperation(string name) { |
|
139 | 178 | m_currentOperation = new LogicalOperation(name, m_currentOperation); |
|
140 | 179 | LogEvent(TraceEventType.OperationStarted, name); |
|
141 | 180 | } |
|
142 | 181 | |
|
143 | 182 | /// <summary> |
|
144 | 183 | /// ΠΠ°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π½Π°ΡΠ°ΡΡΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π°ΡΠ°ΡΡΠ΅ Π² Π΄ΡΡΠ³ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°Ρ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Ρ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
145 | 184 | /// </summary> |
|
146 | 185 | /// <remarks> |
|
147 | 186 | /// ΠΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΆΡΡΠ½Π°Π»Π° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, Π»ΠΈΠ±ΠΎ ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π»ΠΈΠ±ΠΎ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ |
|
148 | 187 | /// Π½Π°ΡΠ°ΡΠ° Π² Π΄ΡΡΠ³ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
149 | 188 | /// </remarks> |
|
150 | 189 | public void EndLogicalOperation() { |
|
151 | 190 | if (m_bound == m_currentOperation) { |
|
152 | 191 | LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); |
|
153 | 192 | } else { |
|
154 | 193 | var op = m_currentOperation; |
|
155 | 194 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); |
|
156 | 195 | m_currentOperation = m_currentOperation.Parent; |
|
157 | 196 | } |
|
158 | 197 | } |
|
159 | 198 | |
|
160 | 199 | /// <summary> |
|
161 | 200 | /// ΠΠ°Π²ΡΠ΅ΡΠ°Π΅Ρ Π²ΡΠ΅ Π½Π°ΡΠ°ΡΡΠ΅ Π² ΡΡΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
162 | 201 | /// </summary> |
|
163 | 202 | public void EndAllOperations() { |
|
164 | 203 | while (m_bound != m_currentOperation) |
|
165 | 204 | EndLogicalOperation(); |
|
166 | 205 | } |
|
167 | 206 | |
|
168 | 207 | void LogEvent(TraceEventType type, string format, params object[] args) { |
|
169 | 208 | LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); |
|
170 | 209 | } |
|
171 | 210 | } |
|
172 | 211 | } |
@@ -1,16 +1,19 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading.Tasks; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Diagnostics { |
|
8 | 8 | public enum TraceEventType { |
|
9 | 9 | Information = 1, |
|
10 | 10 | Warning, |
|
11 | 11 | Error, |
|
12 | 12 | OperationStarted, |
|
13 | 13 | OperationCompleted, |
|
14 |
|
|
|
14 | Fork, | |
|
15 | Attach, | |
|
16 | Detach, | |
|
17 | Created | |
|
15 | 18 | } |
|
16 | 19 | } |
@@ -1,46 +1,62 | |||
|
1 | using System; | |
|
1 | using Implab.Diagnostics; | |
|
2 | using System; | |
|
2 | 3 | using System.Collections.Generic; |
|
3 | 4 | using System.Diagnostics; |
|
4 | 5 | using System.Linq; |
|
5 | 6 | using System.Web; |
|
6 | 7 | |
|
7 | 8 | namespace Implab { |
|
9 | /// <summary> | |
|
10 | /// ΠΠ±ΡΠ΅ΠΊΡ, ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡΠΈΠΉ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΡΠ΅ΡΡΡΡΠΎΠ². | |
|
11 | /// </summary> | |
|
8 | 12 | public class Disposable : IDisposable { |
|
9 | 13 | |
|
10 | 14 | bool m_disposed; |
|
11 | 15 | |
|
12 | 16 | public event EventHandler Disposed; |
|
13 | 17 | |
|
14 | 18 | public bool IsDisposed { |
|
15 | 19 | get { return m_disposed; } |
|
16 | 20 | } |
|
17 | 21 | |
|
18 | 22 | protected void AssertNotDisposed() { |
|
19 | 23 | if (m_disposed) |
|
20 | 24 | throw new ObjectDisposedException(this.ToString()); |
|
21 | 25 | } |
|
22 | ||
|
26 | /// <summary> | |
|
27 | /// ΠΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡ Π² ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ <c>Disposed</c> ΠΈ Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΡΠΎΠ±ΡΡΠΈΠ΅ <see cref="Disposed"/> | |
|
28 | /// </summary> | |
|
29 | /// <param name="disposing">ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π½ΡΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ ΡΠ΅ΡΡΡΡΡ, ΠΈΠ½Π°ΡΠ΅ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ | |
|
30 | /// Π²ΡΠ·Π²Π°Π½ ΡΠ±ΠΎΡΡΠΈΠΊΠΎΠΌ ΠΌΡΡΠΎΡΠ° ΠΈ Π½ΡΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°ΡΡ Π’ΠΠΠ¬ΠΠ Π½Π΅ΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌΡΠ΅ ΡΠ΅ΡΡΡΡΡ Π’ΠΠΠ¬ΠΠ ΡΡΠΎΠ³ΠΎ | |
|
31 | /// ΠΎΠ±ΡΠ΅ΠΊΡΠ°.</param> | |
|
32 | /// <remarks> | |
|
33 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΎΡΡΡΠ΅ΡΡΠ²Π»ΡΠ΅Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΡ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ΅ΠΊΡ ΡΠΆΠ΅ Π±ΡΠ» ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½, ΡΡΠΎΠ±Ρ Π½Π΅ Π²ΡΠ·ΡΠ²Π°ΡΡ | |
|
34 | /// ΡΠΎΠ±ΡΡΠΈΠ΅ <see cref="Disposed"/>. ΠΠ΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΠΌΠ½ΠΎΠ³ΠΎΠΏΠΎΡΠΎΡΠ½ΠΎΡΡΡ. | |
|
35 | /// </remarks> | |
|
23 | 36 | protected virtual void Dispose(bool disposing) { |
|
24 | 37 | if (disposing && !m_disposed) { |
|
25 | 38 | m_disposed = true; |
|
26 | 39 | |
|
27 | 40 | EventHandler temp = Disposed; |
|
28 | 41 | if (temp != null) |
|
29 | 42 | temp(this,EventArgs.Empty); |
|
30 | 43 | } |
|
31 | 44 | } |
|
32 | 45 | public void Dispose() { |
|
33 | 46 | Dispose(true); |
|
34 | 47 | GC.SuppressFinalize(this); |
|
35 | 48 | } |
|
36 | 49 | |
|
50 | /// <summary> | |
|
51 | /// ΠΠ°ΠΏΠΈΡΡΠ²Π°Π΅Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΎΠ± ΡΡΠ΅ΡΠΊΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΠ°. | |
|
52 | /// </summary> | |
|
37 | 53 | protected virtual void ReportObjectLeaks() { |
|
38 | Trace.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); | |
|
54 | TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); | |
|
39 | 55 | } |
|
40 | 56 | |
|
41 | 57 | ~Disposable() { |
|
42 | 58 | Dispose(false); |
|
43 | 59 | ReportObjectLeaks(); |
|
44 | 60 | } |
|
45 | 61 | } |
|
46 | 62 | } No newline at end of file |
@@ -1,191 +1,191 | |||
|
1 | 1 | using Implab.Diagnostics; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Collections.Generic; |
|
4 | 4 | using System.Diagnostics; |
|
5 | 5 | using System.Linq; |
|
6 | 6 | using System.Text; |
|
7 | 7 | using System.Threading; |
|
8 | 8 | |
|
9 | 9 | namespace Implab.Parallels { |
|
10 | 10 | public static class ArrayTraits { |
|
11 | 11 | class ArrayIterator<TSrc> : DispatchPool<int> { |
|
12 | 12 | readonly Action<TSrc> m_action; |
|
13 | 13 | readonly TSrc[] m_source; |
|
14 | 14 | readonly Promise<int> m_promise = new Promise<int>(); |
|
15 | 15 | readonly TraceContext m_traceContext; |
|
16 | 16 | |
|
17 | 17 | int m_pending; |
|
18 | 18 | int m_next; |
|
19 | 19 | |
|
20 | 20 | public ArrayIterator(TSrc[] source, Action<TSrc> action, int threads) |
|
21 | 21 | : base(threads) { |
|
22 | 22 | |
|
23 | 23 | Debug.Assert(source != null); |
|
24 | 24 | Debug.Assert(action != null); |
|
25 | 25 | |
|
26 | 26 | m_traceContext = TraceContext.Snapshot(); |
|
27 | 27 | m_next = 0; |
|
28 | 28 | m_source = source; |
|
29 | 29 | m_pending = source.Length; |
|
30 | 30 | m_action = action; |
|
31 | 31 | |
|
32 | 32 | m_promise.Anyway(() => Dispose()); |
|
33 | 33 | m_promise.Cancelled(() => Dispose()); |
|
34 | 34 | |
|
35 | 35 | InitPool(); |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | public Promise<int> Promise { |
|
39 | 39 | get { |
|
40 | 40 | return m_promise; |
|
41 | 41 | } |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | protected override void Worker() { |
|
45 |
TraceContext. |
|
|
45 | TraceContext.Fork(m_traceContext); | |
|
46 | 46 | base.Worker(); |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | protected override bool TryDequeue(out int unit) { |
|
50 | 50 | unit = Interlocked.Increment(ref m_next) - 1; |
|
51 | 51 | return unit >= m_source.Length ? false : true; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | protected override void InvokeUnit(int unit) { |
|
55 | 55 | try { |
|
56 | 56 | m_action(m_source[unit]); |
|
57 | 57 | var pending = Interlocked.Decrement(ref m_pending); |
|
58 | 58 | if (pending == 0) |
|
59 | 59 | m_promise.Resolve(m_source.Length); |
|
60 | 60 | } catch (Exception e) { |
|
61 | 61 | m_promise.Reject(e); |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | class ArrayMapper<TSrc, TDst>: DispatchPool<int> { |
|
67 | 67 | readonly Func<TSrc, TDst> m_transform; |
|
68 | 68 | readonly TSrc[] m_source; |
|
69 | 69 | readonly TDst[] m_dest; |
|
70 | 70 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); |
|
71 | 71 | readonly TraceContext m_traceContext; |
|
72 | 72 | |
|
73 | 73 | int m_pending; |
|
74 | 74 | int m_next; |
|
75 | 75 | |
|
76 | 76 | public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads) |
|
77 | 77 | : base(threads) { |
|
78 | 78 | |
|
79 | 79 | Debug.Assert (source != null); |
|
80 | 80 | Debug.Assert( transform != null); |
|
81 | 81 | |
|
82 | 82 | m_next = 0; |
|
83 | 83 | m_source = source; |
|
84 | 84 | m_dest = new TDst[source.Length]; |
|
85 | 85 | m_pending = source.Length; |
|
86 | 86 | m_transform = transform; |
|
87 | 87 | m_traceContext = TraceContext.Snapshot(); |
|
88 | 88 | |
|
89 | 89 | m_promise.Anyway(() => Dispose()); |
|
90 | 90 | m_promise.Cancelled(() => Dispose()); |
|
91 | 91 | |
|
92 | 92 | InitPool(); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | public Promise<TDst[]> Promise { |
|
96 | 96 | get { |
|
97 | 97 | return m_promise; |
|
98 | 98 | } |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | protected override void Worker() { |
|
102 |
TraceContext. |
|
|
102 | TraceContext.Fork(m_traceContext); | |
|
103 | 103 | base.Worker(); |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | protected override bool TryDequeue(out int unit) { |
|
107 | 107 | unit = Interlocked.Increment(ref m_next) - 1; |
|
108 | 108 | return unit >= m_source.Length ? false : true; |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | protected override void InvokeUnit(int unit) { |
|
112 | 112 | try { |
|
113 | 113 | m_dest[unit] = m_transform(m_source[unit]); |
|
114 | 114 | var pending = Interlocked.Decrement(ref m_pending); |
|
115 | 115 | if (pending == 0) |
|
116 | 116 | m_promise.Resolve(m_dest); |
|
117 | 117 | } catch (Exception e) { |
|
118 | 118 | m_promise.Reject(e); |
|
119 | 119 | } |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | public static IPromise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) { |
|
124 | 124 | if (source == null) |
|
125 | 125 | throw new ArgumentNullException("source"); |
|
126 | 126 | if (transform == null) |
|
127 | 127 | throw new ArgumentNullException("transform"); |
|
128 | 128 | |
|
129 | 129 | var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads); |
|
130 | 130 | return mapper.Promise; |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | public static IPromise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) { |
|
134 | 134 | if (source == null) |
|
135 | 135 | throw new ArgumentNullException("source"); |
|
136 | 136 | if (action == null) |
|
137 | 137 | throw new ArgumentNullException("action"); |
|
138 | 138 | |
|
139 | 139 | var iter = new ArrayIterator<TSrc>(source, action, threads); |
|
140 | 140 | return iter.Promise; |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) { |
|
144 | 144 | if (source == null) |
|
145 | 145 | throw new ArgumentNullException("source"); |
|
146 | 146 | if (transform == null) |
|
147 | 147 | throw new ArgumentNullException("transform"); |
|
148 | 148 | if (threads <= 0) |
|
149 | 149 | throw new ArgumentOutOfRangeException("Threads number must be greater then zero"); |
|
150 | 150 | |
|
151 | 151 | if (source.Length == 0) |
|
152 | 152 | return Promise<TDst[]>.ResultToPromise(new TDst[0]); |
|
153 | 153 | |
|
154 | 154 | var promise = new Promise<TDst[]>(); |
|
155 | 155 | var res = new TDst[source.Length]; |
|
156 | 156 | var pending = source.Length; |
|
157 | 157 | |
|
158 | 158 | var semaphore = new Semaphore(threads, threads); |
|
159 | 159 | |
|
160 | 160 | AsyncPool.InvokeNewThread(() => { |
|
161 | 161 | for (int i = 0; i < source.Length; i++) { |
|
162 | 162 | if(promise.IsResolved) |
|
163 | 163 | break; // stop processing in case of error or cancellation |
|
164 | 164 | var idx = i; |
|
165 | 165 | semaphore.WaitOne(); |
|
166 | 166 | try { |
|
167 | 167 | var p1 = transform(source[i]); |
|
168 | 168 | p1.Anyway(() => semaphore.Release()); |
|
169 | 169 | p1.Cancelled(() => semaphore.Release()); |
|
170 | 170 | p1.Then( |
|
171 | 171 | x => { |
|
172 | 172 | res[idx] = x; |
|
173 | 173 | var left = Interlocked.Decrement(ref pending); |
|
174 | 174 | if (left == 0) |
|
175 | 175 | promise.Resolve(res); |
|
176 | 176 | }, |
|
177 | 177 | e => promise.Reject(e) |
|
178 | 178 | ); |
|
179 | 179 | |
|
180 | 180 | } catch (Exception e) { |
|
181 | 181 | promise.Reject(e); |
|
182 | 182 | } |
|
183 | 183 | } |
|
184 | 184 | return 0; |
|
185 | 185 | }); |
|
186 | 186 | |
|
187 | 187 | return promise.Anyway(() => semaphore.Dispose()); |
|
188 | 188 | } |
|
189 | 189 | |
|
190 | 190 | } |
|
191 | 191 | } |
@@ -1,50 +1,71 | |||
|
1 | 1 | using Implab.Diagnostics; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Parallels { |
|
6 | 6 | /// <summary> |
|
7 | 7 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ ΡΠ°ΡΠΏΠ°ΡΠ°Π»Π΅Π»ΠΈΠ²Π°Π½ΠΈΡ Π·Π°Π΄Π°Ρ. |
|
8 | 8 | /// </summary> |
|
9 | 9 | /// <remarks> |
|
10 | 10 | /// ΠΡΠΏΠΎΠ»ΡΠ·ΡΡ Π΄Π°Π½Π½ΡΠΉ ΠΊΠ»Π°ΡΡ ΠΈ Π»ΡΠΌΠ΄Π° Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°ΡΠΏΠ°ΡΠ°Π»Π»Π΅Π»ΠΈΡΡ |
|
11 | 11 | /// Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ, Π΄Π»Ρ ΡΡΠΎΠ³ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΠΊΠΎΠ½ΡΠ΅ΠΏΡΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
12 | 12 | /// </remarks> |
|
13 | 13 | public static class AsyncPool { |
|
14 | 14 | |
|
15 | 15 | public static IPromise<T> Invoke<T>(Func<T> func) { |
|
16 | 16 | var p = new Promise<T>(); |
|
17 | 17 | var caller = TraceContext.Snapshot(); |
|
18 | 18 | |
|
19 | 19 | ThreadPool.QueueUserWorkItem(param => { |
|
20 |
TraceContext. |
|
|
20 | TraceContext.Fork(caller); | |
|
21 | 21 | try { |
|
22 | 22 | p.Resolve(func()); |
|
23 | 23 | } catch(Exception e) { |
|
24 | 24 | p.Reject(e); |
|
25 | 25 | } |
|
26 | 26 | }); |
|
27 | 27 | |
|
28 | 28 | return p; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | public static IPromise<T> InvokeNewThread<T>(Func<T> func) { |
|
32 | 32 | var p = new Promise<T>(); |
|
33 | 33 | |
|
34 | 34 | var caller = TraceContext.Snapshot(); |
|
35 | 35 | |
|
36 | 36 | var worker = new Thread(() => { |
|
37 |
TraceContext. |
|
|
37 | TraceContext.Fork(caller); | |
|
38 | 38 | try { |
|
39 | 39 | p.Resolve(func()); |
|
40 | 40 | } catch (Exception e) { |
|
41 | 41 | p.Reject(e); |
|
42 | 42 | } |
|
43 | 43 | }); |
|
44 | 44 | worker.IsBackground = true; |
|
45 | 45 | worker.Start(); |
|
46 | 46 | |
|
47 | 47 | return p; |
|
48 | 48 | } |
|
49 | ||
|
50 | ||
|
51 | public static IPromiseBase InvokeNewThread(Action func) { | |
|
52 | var p = new Promise<object>(); | |
|
53 | ||
|
54 | var caller = TraceContext.Snapshot(); | |
|
55 | ||
|
56 | var worker = new Thread(() => { | |
|
57 | TraceContext.Fork(caller); | |
|
58 | try { | |
|
59 | func(); | |
|
60 | p.Resolve(); | |
|
61 | } catch (Exception e) { | |
|
62 | p.Reject(e); | |
|
63 | } | |
|
64 | }); | |
|
65 | worker.IsBackground = true; | |
|
66 | worker.Start(); | |
|
67 | ||
|
68 | return p; | |
|
69 | } | |
|
49 | 70 | } |
|
50 | 71 | } |
General Comments 0
You need to be logged in to leave comments.
Login now