Leave STDOUT/STDERR open if not redirected

pull/701/head
NextTurn 2020-10-07 00:00:00 +08:00 committed by Next Turn
parent 483ad6639e
commit 5c526c729a
2 changed files with 26 additions and 24 deletions

View File

@ -44,14 +44,24 @@ namespace WinSW
/// </summary> /// </summary>
public abstract class LogHandler public abstract class LogHandler
{ {
public abstract void Log(StreamReader outputReader, StreamReader errorReader); #pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
protected LogHandler(bool outFileDisabled, bool errFileDisabled)
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
{
this.OutFileDisabled = outFileDisabled;
this.ErrFileDisabled = errFileDisabled;
}
/// <summary> /// <summary>
/// Error and information about logging should be reported here. /// Error and information about logging should be reported here.
/// </summary> /// </summary>
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public IEventLogger EventLogger { get; set; } public IEventLogger EventLogger { get; set; }
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public bool OutFileDisabled { get; }
public bool ErrFileDisabled { get; }
public abstract void Log(StreamReader outputReader, StreamReader errorReader);
/// <summary> /// <summary>
/// Convenience method to copy stuff from StreamReader to StreamWriter /// Convenience method to copy stuff from StreamReader to StreamWriter
@ -91,39 +101,26 @@ namespace WinSW
{ {
protected string BaseLogFileName { get; } protected string BaseLogFileName { get; }
protected bool OutFileDisabled { get; }
protected bool ErrFileDisabled { get; }
protected string OutFilePattern { get; } protected string OutFilePattern { get; }
protected string ErrFilePattern { get; } protected string ErrFilePattern { get; }
protected AbstractFileLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern) protected AbstractFileLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern)
: base(outFileDisabled, errFileDisabled)
{ {
this.BaseLogFileName = Path.Combine(logDirectory, baseName); this.BaseLogFileName = Path.Combine(logDirectory, baseName);
this.OutFileDisabled = outFileDisabled;
this.OutFilePattern = outFilePattern; this.OutFilePattern = outFilePattern;
this.ErrFileDisabled = errFileDisabled;
this.ErrFilePattern = errFilePattern; this.ErrFilePattern = errFilePattern;
} }
public override void Log(StreamReader outputReader, StreamReader errorReader) public override void Log(StreamReader outputReader, StreamReader errorReader)
{ {
if (this.OutFileDisabled) if (!this.OutFileDisabled)
{
outputReader.Dispose();
}
else
{ {
this.SafeLogOutput(outputReader); this.SafeLogOutput(outputReader);
} }
if (this.ErrFileDisabled) if (!this.ErrFileDisabled)
{
errorReader.Dispose();
}
else
{ {
this.SafeLogError(errorReader); this.SafeLogError(errorReader);
} }
@ -208,10 +205,13 @@ namespace WinSW
/// </summary> /// </summary>
public class IgnoreLogAppender : LogHandler public class IgnoreLogAppender : LogHandler
{ {
public IgnoreLogAppender()
: base(true, true)
{
}
public override void Log(StreamReader outputReader, StreamReader errorReader) public override void Log(StreamReader outputReader, StreamReader errorReader)
{ {
outputReader.Dispose();
errorReader.Dispose();
} }
} }

View File

@ -545,8 +545,8 @@ namespace WinSW
UseShellExecute = false, UseShellExecute = false,
WorkingDirectory = this.config.WorkingDirectory, WorkingDirectory = this.config.WorkingDirectory,
CreateNoWindow = this.config.HideWindow, CreateNoWindow = this.config.HideWindow,
RedirectStandardOutput = logHandler != null, RedirectStandardOutput = logHandler?.OutFileDisabled == false,
RedirectStandardError = logHandler != null, RedirectStandardError = logHandler?.ErrFileDisabled == false,
}; };
Dictionary<string, string> environment = this.config.EnvironmentVariables; Dictionary<string, string> environment = this.config.EnvironmentVariables;
@ -598,7 +598,9 @@ namespace WinSW
if (logHandler != null) if (logHandler != null)
{ {
logHandler.Log(process.StandardOutput, process.StandardError); logHandler.Log(
startInfo.RedirectStandardOutput ? process.StandardOutput : StreamReader.Null,
startInfo.RedirectStandardError ? process.StandardError : StreamReader.Null);
} }
if (onExited != null) if (onExited != null)