mirror of https://github.com/winsw/winsw
Issue #218 - ProcessHelper#StartProcessAndCallbackForExit() should redirect STDOUT/STDERR when LogHandler is defined
It restores logging of executables, which has been broken in https://github.com/kohsuke/winsw/pull/220. Not a regression, because the change has not been released yetpull/224/head
parent
cfacfe3879
commit
05092376f3
|
@ -136,7 +136,8 @@ namespace winsw
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handle the creation of the logfiles based on the optional logmode setting.
|
/// Handle the creation of the logfiles based on the optional logmode setting.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void HandleLogfiles()
|
/// <returns>Log Handler, which should be used for the spawned process</returns>
|
||||||
|
private LogHandler CreateExecutableLogHandler()
|
||||||
{
|
{
|
||||||
string logDirectory = _descriptor.LogDirectory;
|
string logDirectory = _descriptor.LogDirectory;
|
||||||
|
|
||||||
|
@ -147,7 +148,7 @@ namespace winsw
|
||||||
|
|
||||||
LogHandler logAppender = _descriptor.LogHandler;
|
LogHandler logAppender = _descriptor.LogHandler;
|
||||||
logAppender.EventLogger = this;
|
logAppender.EventLogger = this;
|
||||||
logAppender.log(_process.StandardOutput.BaseStream, _process.StandardError.BaseStream);
|
return logAppender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LogEvent(String message)
|
public void LogEvent(String message)
|
||||||
|
@ -247,12 +248,10 @@ namespace winsw
|
||||||
LogEvent("Starting " + _descriptor.Executable + ' ' + startarguments);
|
LogEvent("Starting " + _descriptor.Executable + ' ' + startarguments);
|
||||||
Log.Info("Starting " + _descriptor.Executable + ' ' + startarguments);
|
Log.Info("Starting " + _descriptor.Executable + ' ' + startarguments);
|
||||||
|
|
||||||
StartProcess(_process, startarguments, _descriptor.Executable);
|
LogHandler executableLogHandler = CreateExecutableLogHandler();
|
||||||
|
StartProcess(_process, startarguments, _descriptor.Executable, executableLogHandler);
|
||||||
ExtensionManager.FireOnProcessStarted(_process);
|
ExtensionManager.FireOnProcessStarted(_process);
|
||||||
|
|
||||||
// send stdout and stderr to its respective output file.
|
|
||||||
HandleLogfiles();
|
|
||||||
|
|
||||||
_process.StandardInput.Close(); // nothing for you to read!
|
_process.StandardInput.Close(); // nothing for you to read!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,7 +321,8 @@ namespace winsw
|
||||||
executable = _descriptor.Executable;
|
executable = _descriptor.Executable;
|
||||||
}
|
}
|
||||||
|
|
||||||
StartProcess(stopProcess, stoparguments, executable);
|
// TODO: Redirect logging to Log4Net once https://github.com/kohsuke/winsw/pull/213 is integrated
|
||||||
|
StartProcess(stopProcess, stoparguments, executable, null);
|
||||||
|
|
||||||
Log.Debug("WaitForProcessToExit " + _process.Id + "+" + stopProcess.Id);
|
Log.Debug("WaitForProcessToExit " + _process.Id + "+" + stopProcess.Id);
|
||||||
WaitForProcessToExit(_process);
|
WaitForProcessToExit(_process);
|
||||||
|
@ -407,7 +407,7 @@ namespace winsw
|
||||||
Advapi32.SetServiceStatus(handle, ref _wrapperServiceStatus);
|
Advapi32.SetServiceStatus(handle, ref _wrapperServiceStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartProcess(Process processToStart, string arguments, String executable)
|
private void StartProcess(Process processToStart, string arguments, String executable, LogHandler logHandler)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Define handler of the completed process
|
// Define handler of the completed process
|
||||||
|
@ -454,7 +454,8 @@ namespace winsw
|
||||||
envVars: _envs,
|
envVars: _envs,
|
||||||
workingDirectory: _descriptor.WorkingDirectory,
|
workingDirectory: _descriptor.WorkingDirectory,
|
||||||
priority: _descriptor.Priority,
|
priority: _descriptor.Priority,
|
||||||
callback: processCompletionCallback);
|
callback: processCompletionCallback,
|
||||||
|
logHandler: logHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int Main(string[] args)
|
public static int Main(string[] args)
|
||||||
|
|
|
@ -129,8 +129,9 @@ namespace winsw.Util
|
||||||
/// <param name="workingDirectory">Working directory</param>
|
/// <param name="workingDirectory">Working directory</param>
|
||||||
/// <param name="priority">Priority</param>
|
/// <param name="priority">Priority</param>
|
||||||
/// <param name="callback">Completion callback. If null, the completion won't be monitored</param>
|
/// <param name="callback">Completion callback. If null, the completion won't be monitored</param>
|
||||||
|
/// <param name="logHandler">Log handler. If enabled, logs will be redirected to the process and then reported</param>
|
||||||
public static void StartProcessAndCallbackForExit(Process processToStart, String executable = null, string arguments = null, Dictionary<string, string> envVars = null,
|
public static void StartProcessAndCallbackForExit(Process processToStart, String executable = null, string arguments = null, Dictionary<string, string> envVars = null,
|
||||||
string workingDirectory = null, ProcessPriorityClass? priority = null, ProcessCompletionCallback callback = null)
|
string workingDirectory = null, ProcessPriorityClass? priority = null, ProcessCompletionCallback callback = null, LogHandler logHandler = null)
|
||||||
{
|
{
|
||||||
var ps = processToStart.StartInfo;
|
var ps = processToStart.StartInfo;
|
||||||
ps.FileName = executable ?? ps.FileName;
|
ps.FileName = executable ?? ps.FileName;
|
||||||
|
@ -138,9 +139,9 @@ namespace winsw.Util
|
||||||
ps.WorkingDirectory = workingDirectory ?? ps.WorkingDirectory;
|
ps.WorkingDirectory = workingDirectory ?? ps.WorkingDirectory;
|
||||||
ps.CreateNoWindow = false;
|
ps.CreateNoWindow = false;
|
||||||
ps.UseShellExecute = false;
|
ps.UseShellExecute = false;
|
||||||
ps.RedirectStandardInput = false;
|
ps.RedirectStandardInput = false;
|
||||||
ps.RedirectStandardOutput = false;
|
ps.RedirectStandardOutput = logHandler != null;
|
||||||
ps.RedirectStandardError = false;
|
ps.RedirectStandardError = logHandler != null;
|
||||||
|
|
||||||
if (envVars != null)
|
if (envVars != null)
|
||||||
{
|
{
|
||||||
|
@ -160,6 +161,13 @@ namespace winsw.Util
|
||||||
processToStart.PriorityClass = priority.Value;
|
processToStart.PriorityClass = priority.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redirect logs if required
|
||||||
|
if (logHandler != null)
|
||||||
|
{
|
||||||
|
Logger.Debug("Forwarding logs of the process " + processToStart + " to " + logHandler);
|
||||||
|
logHandler.log(processToStart.StandardOutput.BaseStream, processToStart.StandardError.BaseStream);
|
||||||
|
}
|
||||||
|
|
||||||
// monitor the completion of the process
|
// monitor the completion of the process
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue