-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorBuildLogger.cs
176 lines (152 loc) · 5.26 KB
/
EditorBuildLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Microsoft.Build.Framework;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CaravelEditor
{
class EditorBuildLogger : ILogger
{
public LoggerVerbosity Verbosity { get; set; }
public string Parameters { get; set; }
public int MaxLines { get; set; }
public RichTextBox TextBox
{
get; set;
}
private int m_iIndent = 0;
private Timer m_UpdateTimer = new Timer();
private List<string> m_LinesBuffer = new List<string>();
public EditorBuildLogger()
{
MaxLines = 2000;
Verbosity = LoggerVerbosity.Normal;
m_UpdateTimer.Interval = 100;
m_UpdateTimer.Tick += UpdateTimer_Tick;
m_UpdateTimer.Start();
}
public void Initialize(IEventSource eventSource)
{
if (TextBox != null)
{
eventSource.ProjectStarted += EventSource_ProjectStarted;
eventSource.ProjectFinished += EventSource_ProjectFinished;
eventSource.TaskStarted += EventSource_TaskStarted;
eventSource.MessageRaised += EventSource_MessageRaised;
eventSource.WarningRaised += EventSource_WarningRaised;
eventSource.ErrorRaised += EventSource_ErrorRaised;
}
}
private void EventSource_ErrorRaised(object sender, BuildErrorEventArgs e)
{
// BuildErrorEventArgs adds LineNumber, ColumnNumber, File, amongst other parameters
string line = String.Format(": ERROR {0}({1},{2}): ", e.File, e.LineNumber, e.ColumnNumber);
WriteLineWithSenderAndMessage(line, e);
}
private void EventSource_WarningRaised(object sender, BuildWarningEventArgs e)
{
string line = String.Format(": Warning {0}({1},{2}): ", e.File, e.LineNumber, e.ColumnNumber);
WriteLineWithSenderAndMessage(line, e);
}
private void EventSource_MessageRaised(object sender, BuildMessageEventArgs e)
{
if ((e.Importance == MessageImportance.High)
|| (e.Importance == MessageImportance.Normal && (Verbosity == LoggerVerbosity.Normal || Verbosity == LoggerVerbosity.Detailed))
|| (e.Importance == MessageImportance.Low && Verbosity == LoggerVerbosity.Detailed)
)
{
WriteLineWithSenderAndMessage(String.Empty, e);
}
}
private void EventSource_TaskStarted(object sender, TaskStartedEventArgs e)
{
if (TextBox != null)
{
//WriteLine(Environment.NewLine, e);
}
}
private void EventSource_ProjectFinished(object sender, ProjectFinishedEventArgs e)
{
if (TextBox != null)
{
m_iIndent--;
WriteLine("", e);
}
}
private void EventSource_ProjectStarted(object sender, ProjectStartedEventArgs e)
{
if (TextBox != null)
{
WriteLine("", e);
m_iIndent++;
}
}
public void Shutdown()
{
}
public void WriteLine(string line)
{
TextBox.AppendText(Environment.NewLine);
for (int i = m_iIndent; i > 0; i--)
{
TextBox.AppendText("\t");
}
TextBox.AppendText(line);
TrimLines();
}
private void WriteLine(string line, BuildEventArgs e)
{
var toWrite = "";
toWrite += Environment.NewLine;
for (int i = m_iIndent; i > 0; i--)
{
toWrite += "\t";
//TextBox.AppendText("\t");
}
toWrite += line + e.Message;
//TextBox.AppendText(line + e.Message);
lock (m_LinesBuffer)
{
m_LinesBuffer.Add(toWrite);
}
}
private void WriteLineWithSenderAndMessage(string line, BuildEventArgs e)
{
if (0 == String.Compare(e.SenderName, "MSBuild", true /*ignore case*/))
{
// Well, if the sender name is MSBuild, let's leave it out for prettiness
WriteLine(line, e);
}
else
{
WriteLine(e.SenderName + ": " + line, e);
}
}
private void TrimLines()
{
if (TextBox != null)
{
if (TextBox.Lines.Length > MaxLines)
{
TextBox.Select(0, TextBox.GetFirstCharIndexFromLine(TextBox.Lines.Length - MaxLines));
TextBox.SelectedText = "";
}
}
}
private void UpdateTimer_Tick(object sender, EventArgs e)
{
lock (m_LinesBuffer)
{
foreach (var line in m_LinesBuffer)
{
TextBox.AppendText(line);
}
TrimLines();
if (m_LinesBuffer.Count > 0)
{
TextBox.ScrollToCaret();
}
m_LinesBuffer.Clear();
}
}
}
}