mirror of https://github.com/winsw/winsw
Support a Logmode to 'reset', 'roll' once or 'append' (default) the out.log and err.log files.
git-svn-id: https://svn.kenai.com/svn/winsw~subversion/trunk@10 c8b2a3fe-9b5b-6a51-a37e-dc31b0e308faremotes/git-svn
parent
9d5c22007f
commit
bd2ce3688a
83
Main.cs
83
Main.cs
|
@ -104,6 +104,26 @@ namespace winsw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logmode to 'reset', 'roll' once or 'append' [default] the out.log and err.log files.
|
||||||
|
/// </summary>
|
||||||
|
public string Logmode
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
XmlNode logmodeNode = dom.SelectSingleNode("//logmode");
|
||||||
|
|
||||||
|
if (logmodeNode == null)
|
||||||
|
{
|
||||||
|
return "append";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return logmodeNode.InnerText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string Id
|
public string Id
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -214,23 +234,15 @@ namespace winsw
|
||||||
string line;
|
string line;
|
||||||
while ((line = tr.ReadLine()) != null)
|
while ((line = tr.ReadLine()) != null)
|
||||||
{
|
{
|
||||||
try
|
EventLog.WriteEntry("Handling copy: " + line);
|
||||||
|
string[] tokens = line.Split('>');
|
||||||
|
if (tokens.Length > 2)
|
||||||
{
|
{
|
||||||
EventLog.WriteEntry("Handling copy: " + line);
|
EventLog.WriteEntry("Too many delimiters in " + line);
|
||||||
string[] tokens = line.Split('>');
|
continue;
|
||||||
if (tokens.Length > 2)
|
}
|
||||||
{
|
|
||||||
EventLog.WriteEntry("Too many delimiters in " + line);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
File.Delete(tokens[1]);
|
CopyFile(tokens[0], tokens[1]);
|
||||||
File.Move(tokens[0], tokens[1]);
|
|
||||||
}
|
|
||||||
catch(IOException e)
|
|
||||||
{
|
|
||||||
EventLog.WriteEntry("Failed to copy :"+line+" because "+e.Message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,6 +253,44 @@ namespace winsw
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void CopyFile(string sourceFileName, string destFileName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.Delete(destFileName);
|
||||||
|
File.Move(sourceFileName, destFileName);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
EventLog.WriteEntry("Failed to copy :" + sourceFileName + " to " + destFileName + " because " + e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle the creation of the logfiles based on the optional logmode setting.
|
||||||
|
/// </summary>
|
||||||
|
private void HandleLogfiles()
|
||||||
|
{
|
||||||
|
string baseName = descriptor.BasePath;
|
||||||
|
string errorLogfilename = baseName + ".err.log";
|
||||||
|
string outputLogfilename = baseName + ".out.log";
|
||||||
|
|
||||||
|
System.IO.FileMode fileMode = FileMode.Append;
|
||||||
|
|
||||||
|
if (descriptor.Logmode == "reset")
|
||||||
|
{
|
||||||
|
fileMode = FileMode.Create;
|
||||||
|
}
|
||||||
|
else if (descriptor.Logmode == "roll")
|
||||||
|
{
|
||||||
|
CopyFile(outputLogfilename, outputLogfilename + ".old");
|
||||||
|
CopyFile(errorLogfilename, errorLogfilename + ".old");
|
||||||
|
}
|
||||||
|
|
||||||
|
new Thread(delegate() { CopyStream(process.StandardOutput, new StreamWriter(new FileStream(outputLogfilename, fileMode))); }).Start();
|
||||||
|
new Thread(delegate() { CopyStream(process.StandardError, new StreamWriter(new FileStream(errorLogfilename, fileMode))); }).Start();
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnStart(string[] args)
|
protected override void OnStart(string[] args)
|
||||||
{
|
{
|
||||||
var envs = descriptor.EnvironmentVariables;
|
var envs = descriptor.EnvironmentVariables;
|
||||||
|
@ -269,8 +319,7 @@ namespace winsw
|
||||||
process.Start();
|
process.Start();
|
||||||
|
|
||||||
// send stdout and stderr to its respective output file.
|
// send stdout and stderr to its respective output file.
|
||||||
new Thread(delegate() { CopyStream(process.StandardOutput, new StreamWriter(new FileStream(baseName + ".out.log", FileMode.Append))); }).Start();
|
HandleLogfiles();
|
||||||
new Thread(delegate() { CopyStream(process.StandardError, new StreamWriter(new FileStream(baseName + ".err.log", FileMode.Append))); }).Start();
|
|
||||||
|
|
||||||
// monitor the completion of the process
|
// monitor the completion of the process
|
||||||
new Thread(delegate()
|
new Thread(delegate()
|
||||||
|
|
Loading…
Reference in New Issue