Skip to content

Commit 27a0bf5

Browse files
author
Stewart Miles
committed
Moved log to text area window redirection into TextAreaDialog.
Moved the log to text area window redirection logic from GradleResolver to TextAreaDialog so that it can be reused by other modules. Bug: 135269831, 135273820 Change-Id: I6a8a1bc4513a13aa4fa38b831517993c6825835e
1 parent 731ed42 commit 27a0bf5

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

source/PlayServicesResolver/src/CommandLineDialog.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public override void Initialize()
230230
progressSummary = "";
231231
UpdateEvent = null;
232232
autoScrollToBottom = false;
233+
logRedirector.ShouldLogDelegate = () => { return !RunningCommand; };
233234
}
234235

235236
/// <summary>

source/PlayServicesResolver/src/GradleResolver.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ private class ResolutionState {
238238
public List<string> missingArtifacts = new List<string>();
239239
public List<Dependency> missingArtifactsAsDependencies = new List<Dependency>();
240240
public List<string> modifiedArtifacts = new List<string>();
241-
public bool errorOrWarningLogged = false;
242241
}
243242

244243
/// <summary>
@@ -293,35 +292,18 @@ private void GradleResolution(
293292
"Resolving Android Dependencies");
294293

295294
// Register an event to redirect log messages to the resolution window.
296-
Google.Logger.LogMessageDelegate logToWindow = (message, logLevel) => {
297-
string messagePrefix;
298-
switch (logLevel) {
299-
case LogLevel.Error:
300-
messagePrefix = "ERROR: ";
301-
resolutionState.errorOrWarningLogged = true;
302-
break;
303-
case LogLevel.Warning:
304-
messagePrefix = "WARNING: ";
305-
resolutionState.errorOrWarningLogged = true;
306-
break;
307-
default:
308-
messagePrefix = "";
309-
break;
310-
}
311-
if (!window.RunningCommand) {
312-
window.AddBodyText(messagePrefix + message + "\n");
313-
}
314-
};
315-
PlayServicesResolver.logger.LogMessage += logToWindow;
295+
var logRedirector = window.Redirector;
296+
PlayServicesResolver.logger.LogMessage += logRedirector.LogToWindow;
316297

317298
// When resolution is complete unregister the log redirection event.
318299
Action resolutionCompleteRestoreLogger = () => {
319-
PlayServicesResolver.logger.LogMessage -= logToWindow;
300+
PlayServicesResolver.logger.LogMessage -= logRedirector.LogToWindow;
320301
// If the command completed successfully or the log level is info or above close
321302
// the window, otherwise leave it open for inspection.
322303
if ((resolutionState.commandLineResult.exitCode == 0 &&
323304
PlayServicesResolver.logger.Level >= LogLevel.Info &&
324-
!resolutionState.errorOrWarningLogged) || closeWindowOnCompletion) {
305+
!(logRedirector.WarningLogged || logRedirector.ErrorLogged)) ||
306+
closeWindowOnCompletion) {
325307
window.Close();
326308
}
327309
resolutionComplete(resolutionState.missingArtifactsAsDependencies);

source/PlayServicesResolver/src/TextAreaDialog.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public class TextAreaDialog : EditorWindow
9999
/// </summary>
100100
private const long REPAINT_PERIOD_IN_MILLISECONDS = 33; // ~30Hz
101101

102+
// Backing store for the Redirector property.
103+
internal LogRedirector logRedirector;
104+
102105
/// <summary>
103106
/// Get the existing text area window or create a new one.
104107
/// </summary>
@@ -124,6 +127,7 @@ public virtual void Initialize()
124127
minSize = new Vector2(300, 200);
125128
position = new Rect(UnityEngine.Screen.width / 3, UnityEngine.Screen.height / 3,
126129
minSize.x * 2, minSize.y * 2);
130+
logRedirector = new LogRedirector(this);
127131
}
128132

129133
// Add to body text.
@@ -222,6 +226,76 @@ protected virtual void OnDestroy() {
222226
if (buttonClicked != null) buttonClicked(this);
223227
}
224228
}
229+
230+
/// <summary>
231+
/// Redirects logs to a TextAreaDialog window.
232+
/// </summary>
233+
internal class LogRedirector {
234+
235+
/// <summary>
236+
/// Window to redirect logs to.
237+
/// </summary>
238+
private TextAreaDialog window;
239+
240+
/// <summary>
241+
/// Create a log redirector associated with this window.
242+
/// </summary>
243+
public LogRedirector(TextAreaDialog window) {
244+
this.window = window;
245+
LogToWindow = (string message, LogLevel level) => LogMessage(message, level);
246+
ErrorLogged = false;
247+
WarningLogged = false;
248+
ShouldLogDelegate = () => { return true; };
249+
}
250+
251+
/// <summary>
252+
/// Delegate that logs to the window associated with this object.
253+
/// </summary>
254+
public Google.Logger.LogMessageDelegate LogToWindow { get; private set; }
255+
256+
/// <summary>
257+
/// Whether an error was logged.
258+
/// </summary>
259+
public bool ErrorLogged { get; private set; }
260+
261+
262+
/// <summary>
263+
/// Whether a warning was logged.
264+
/// </summary>
265+
public bool WarningLogged { get; private set; }
266+
267+
/// <summary>
268+
/// Delegate that determines whether a message should be logged.
269+
/// </summary>
270+
public Func<bool> ShouldLogDelegate { get; set; }
271+
272+
/// <summary>
273+
/// Log a message to the window associated with this object.
274+
/// </summary>
275+
private void LogMessage(string message, LogLevel level) {
276+
string messagePrefix;
277+
switch (level) {
278+
case LogLevel.Error:
279+
messagePrefix = "ERROR: ";
280+
ErrorLogged = true;
281+
break;
282+
case LogLevel.Warning:
283+
messagePrefix = "WARNING: ";
284+
WarningLogged = true;
285+
break;
286+
default:
287+
messagePrefix = "";
288+
break;
289+
}
290+
if (ShouldLogDelegate()) window.AddBodyText(messagePrefix + message + "\n");
291+
292+
}
293+
}
294+
295+
/// <summary>
296+
/// Get an object that can redirect log messages to this window.
297+
/// </summary>
298+
internal LogRedirector Redirector { get { return logRedirector; } }
225299
}
226300

227301
}

0 commit comments

Comments
 (0)