Skip to content

Commit

Permalink
[TouchRunner] Improve logging output.
Browse files Browse the repository at this point in the history
When asked to write logs to a TCP host:port, and given multiple hostnames, we
try to connect to all of them to see which hostname works.

This logic does not necessarily work when asked to write to a http host:port
(it would have to be implemented), which means that when given multiple
hostnames, we'd enter a special case, not connecting to anything, and soon
throwing a NullReferenceException.

So refactor the logic slightly: we now only try to select the hostname when
asked to write to a TCP host:port; when asked to write to a HTTP host:port we
just select the first hostname (which is still better than throwing a NRE).

We also don't unnecessarily try to resolve hostnames when asked to write logs
to a file, and make absolutely sure there's no way to not have a log to write
to by writing to Console.Out if we can't do anything else.
  • Loading branch information
rolfbjarne committed Oct 4, 2017
1 parent 8f1e19d commit b5b227e
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions NUnitLite/TouchRunner/TouchRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,57 +234,64 @@ public bool OpenWriter (string message)
// let the application provide it's own TextWriter to ease automation with AutoStart property
if (Writer == null) {
if (options.ShowUseNetworkLogger) {
var hostname = SelectHostName (options.HostName.Split (','), options.HostPort);

if (hostname != null) {
Console.WriteLine ("[{0}] Sending '{1}' results to {2}:{3}", now, message, hostname, options.HostPort);
try {
WriterFinishedTask = null;
TextWriter defaultWriter = null;
switch (options.Transport) {
case "FILE":
defaultWriter = new StreamWriter (options.LogFile, true, System.Text.Encoding.UTF8)
{
AutoFlush = true,
};
try {
string hostname = null;
WriterFinishedTask = null;
TextWriter defaultWriter = null;
switch (options.Transport) {
case "FILE":
Console.WriteLine ("[{0}] Sending '{1}' results to the file {2}", now, message, options.LogFile);
defaultWriter = new StreamWriter (options.LogFile, true, System.Text.Encoding.UTF8)
{
AutoFlush = true,
};
break;
case "HTTP":
var hostnames = options.HostName.Split (',');
hostname = hostnames [0];
if (hostnames.Length > 1)
Console.WriteLine ("[{0}] Found multiple host names ({1}); will only try sending to the first ({2})", now, options.HostName, hostname);
Console.WriteLine ("[{0}] Sending '{1}' results to {2}:{3}", now, message, hostname, options.HostPort);
var w = new HttpTextWriter ()
{
HostName = hostname,
Port = options.HostPort,
};
w.Open ();
defaultWriter = w;
WriterFinishedTask = w.FinishedTask;
break;
default:
Console.WriteLine ("Unknown transport '{0}': switching to default (TCP)", options.Transport);
goto case "TCP";
case "TCP":
hostname = SelectHostName (options.HostName.Split (','), options.HostPort);
if (string.IsNullOrEmpty (hostname))
break;
case "HTTP":
var w = new HttpTextWriter ()
{
HostName = hostname,
Port = options.HostPort,
};
w.Open ();
defaultWriter = w;
WriterFinishedTask = w.FinishedTask;
break;
default:
Console.WriteLine ("Unknown transport '{0}': switching to default (TCP)", options.Transport);
goto case "TCP";
case "TCP":
defaultWriter = new TcpTextWriter (hostname, options.HostPort);
break;
}
if (options.EnableXml) {
Writer = new NUnitOutputTextWriter (
this, defaultWriter, new NUnitLite.Runner.NUnit2XmlOutputWriter (DateTime.UtcNow), options.XmlMode);
} else {
Writer = defaultWriter;
}
} catch (Exception ex) {
connection_failure = true;
if (!ShowConnectionErrorAlert (hostname, options.HostPort, ex))
return false;

Console.WriteLine ("Network error: Cannot connect to {0}:{1}: {2}. Continuing on console.", hostname, options.HostPort, ex);
Writer = Console.Out;
Console.WriteLine ("[{0}] Sending '{1}' results to {2}:{3}", now, message, hostname, options.HostPort);
defaultWriter = new TcpTextWriter (hostname, options.HostPort);
break;
}
if (options.EnableXml) {
Writer = new NUnitOutputTextWriter (
this, defaultWriter, new NUnitLite.Runner.NUnit2XmlOutputWriter (DateTime.UtcNow), options.XmlMode);
} else {
Writer = defaultWriter;
}
} catch (Exception ex) {
connection_failure = true;
if (!ShowConnectionErrorAlert (options.HostName, options.HostPort, ex))
return false;

Console.WriteLine ("Network error: Cannot connect to {0}:{1}: {2}. Continuing on console.", options.HostName, options.HostPort, ex);
Writer = Console.Out;
}
} else {
Writer = Console.Out;
}
}

if (Writer == null)
Writer = Console.Out;

Writer.WriteLine ("[Runner executing:\t{0}]", message);
Writer.WriteLine ("[MonoTouch Version:\t{0}]", Constants.Version);
Writer.WriteLine ("[Assembly:\t{0}.dll ({1} bits)]", typeof (NSObject).Assembly.GetName ().Name, IntPtr.Size * 8);
Expand Down

0 comments on commit b5b227e

Please sign in to comment.