mirror of https://github.com/winsw/winsw
Added file sharing mode and protected thread body.
parent
8d1deb2ae4
commit
91382d4851
45
Main.cs
45
Main.cs
|
@ -94,7 +94,7 @@ namespace winsw
|
||||||
int THRESHOLD = 10 * 1024 * 1024; // rotate every 10MB. should be made configurable.
|
int THRESHOLD = 10 * 1024 * 1024; // rotate every 10MB. should be made configurable.
|
||||||
|
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
FileStream w = new FileStream(baseName + ext,FileMode.Append);
|
FileStream w = new FileStream(baseName + ext, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
|
||||||
long sz = new FileInfo(baseName + ext).Length;
|
long sz = new FileInfo(baseName + ext).Length;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
|
@ -141,7 +141,7 @@ namespace winsw
|
||||||
|
|
||||||
// even if the log rotation fails, create a new one, or else
|
// even if the log rotation fails, create a new one, or else
|
||||||
// we'll infinitely try to rotate.
|
// we'll infinitely try to rotate.
|
||||||
w = new FileStream(baseName + ext, FileMode.Create);
|
w = new FileStream(baseName + ext, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
|
||||||
sz = new FileInfo(baseName + ext).Length;
|
sz = new FileInfo(baseName + ext).Length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,6 +188,9 @@ namespace winsw
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// File replacement.
|
||||||
|
/// </summary>
|
||||||
private void CopyFile(string sourceFileName, string destFileName)
|
private void CopyFile(string sourceFileName, string destFileName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -201,6 +204,25 @@ namespace winsw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Starts a thread that protects the execution with a try/catch block.
|
||||||
|
/// It appears that in .NET, unhandled exception in any thread causes the app to terminate
|
||||||
|
/// http://msdn.microsoft.com/en-us/library/ms228965.aspx
|
||||||
|
/// </summary>
|
||||||
|
private void StartThread(ThreadStart main)
|
||||||
|
{
|
||||||
|
new Thread(delegate() {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
main();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
WriteEvent("Thread failed unexpectedly",e);
|
||||||
|
}
|
||||||
|
}).Start();
|
||||||
|
}
|
||||||
|
|
||||||
/// <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>
|
||||||
|
@ -220,12 +242,12 @@ namespace winsw
|
||||||
if (descriptor.Logmode == "rotate")
|
if (descriptor.Logmode == "rotate")
|
||||||
{
|
{
|
||||||
string logName = Path.Combine(logDirectory, baseName);
|
string logName = Path.Combine(logDirectory, baseName);
|
||||||
new Thread(delegate() { CopyStreamWithRotation(process.StandardOutput.BaseStream, logName, ".out.log"); }).Start();
|
StartThread(delegate() { CopyStreamWithRotation(process.StandardOutput.BaseStream, logName, ".out.log"); });
|
||||||
new Thread(delegate() { CopyStreamWithRotation(process.StandardError.BaseStream, logName, ".err.log"); }).Start();
|
StartThread(delegate() { CopyStreamWithRotation(process.StandardError.BaseStream, logName, ".err.log"); });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.IO.FileMode fileMode = FileMode.Append;
|
FileMode fileMode = FileMode.Append;
|
||||||
|
|
||||||
if (descriptor.Logmode == "reset")
|
if (descriptor.Logmode == "reset")
|
||||||
{
|
{
|
||||||
|
@ -237,8 +259,8 @@ namespace winsw
|
||||||
CopyFile(errorLogfilename, errorLogfilename + ".old");
|
CopyFile(errorLogfilename, errorLogfilename + ".old");
|
||||||
}
|
}
|
||||||
|
|
||||||
new Thread(delegate() { CopyStream(process.StandardOutput.BaseStream, new FileStream(outputLogfilename, fileMode)); }).Start();
|
StartThread(delegate() { CopyStream(process.StandardOutput.BaseStream, new FileStream(outputLogfilename, fileMode, FileAccess.Write, FileShare.ReadWrite)); });
|
||||||
new Thread(delegate() { CopyStream(process.StandardError.BaseStream, new FileStream(errorLogfilename, fileMode)); }).Start();
|
StartThread(delegate() { CopyStream(process.StandardError.BaseStream, new FileStream(errorLogfilename, fileMode, FileAccess.Write, FileShare.ReadWrite)); });
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LogEvent(String message)
|
private void LogEvent(String message)
|
||||||
|
@ -265,6 +287,11 @@ namespace winsw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void WriteEvent(Exception exception)
|
||||||
|
{
|
||||||
|
WriteEvent(exception.Message + "\nStacktrace:" + exception.StackTrace);
|
||||||
|
}
|
||||||
|
|
||||||
private void WriteEvent(String message, Exception exception)
|
private void WriteEvent(String message, Exception exception)
|
||||||
{
|
{
|
||||||
WriteEvent(message + "\nMessage:" + exception.Message + "\nStacktrace:" + exception.StackTrace);
|
WriteEvent(message + "\nMessage:" + exception.Message + "\nStacktrace:" + exception.StackTrace);
|
||||||
|
@ -466,7 +493,7 @@ namespace winsw
|
||||||
WriteEvent("Started " + process.Id);
|
WriteEvent("Started " + process.Id);
|
||||||
|
|
||||||
// monitor the completion of the process
|
// monitor the completion of the process
|
||||||
new Thread(delegate()
|
StartThread(delegate()
|
||||||
{
|
{
|
||||||
string msg = process.Id + " - " + process.StartInfo.FileName + " " + process.StartInfo.Arguments;
|
string msg = process.Id + " - " + process.StartInfo.FileName + " " + process.StartInfo.Arguments;
|
||||||
process.WaitForExit();
|
process.WaitForExit();
|
||||||
|
@ -496,7 +523,7 @@ namespace winsw
|
||||||
{
|
{
|
||||||
LogEvent("Dispose " + ioe.Message);
|
LogEvent("Dispose " + ioe.Message);
|
||||||
}
|
}
|
||||||
}).Start();
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int Main(string[] args)
|
public static int Main(string[] args)
|
||||||
|
|
Loading…
Reference in New Issue