Add Flush() method to LogWriter. Also correct line endings to Linux form.

connector_plugin
Robert Adams 2012-10-02 10:48:16 -07:00
parent f2c78281ce
commit 1e5869dcf6
1 changed files with 170 additions and 161 deletions

View File

@ -1,161 +1,170 @@
/* /*
* Copyright (c) Contributors, http://opensimulator.org/ * Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the * * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products * names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using log4net; using log4net;
namespace OpenSim.Region.CoreModules.Framework.Statistics.Logging namespace OpenSim.Region.CoreModules.Framework.Statistics.Logging
{ {
/// <summary> /// <summary>
/// Class for writing a high performance, high volume log file. /// Class for writing a high performance, high volume log file.
/// Sometimes, to debug, one has a high volume logging to do and the regular /// Sometimes, to debug, one has a high volume logging to do and the regular
/// log file output is not appropriate. /// log file output is not appropriate.
/// Create a new instance with the parameters needed and /// Create a new instance with the parameters needed and
/// call Write() to output a line. Call Close() when finished. /// call Write() to output a line. Call Close() when finished.
/// If created with no parameters, it will not log anything. /// If created with no parameters, it will not log anything.
/// </summary> /// </summary>
public class LogWriter : IDisposable public class LogWriter : IDisposable
{ {
public bool Enabled { get; private set; } public bool Enabled { get; private set; }
private string m_logDirectory = "."; private string m_logDirectory = ".";
private int m_logMaxFileTimeMin = 5; // 5 minutes private int m_logMaxFileTimeMin = 5; // 5 minutes
public String LogFileHeader { get; set; } public String LogFileHeader { get; set; }
private StreamWriter m_logFile = null; private StreamWriter m_logFile = null;
private TimeSpan m_logFileLife; private TimeSpan m_logFileLife;
private DateTime m_logFileEndTime; private DateTime m_logFileEndTime;
private Object m_logFileWriteLock = new Object(); private Object m_logFileWriteLock = new Object();
// set externally when debugging. If let 'null', this does not write any error messages. // set externally when debugging. If let 'null', this does not write any error messages.
public ILog ErrorLogger = null; public ILog ErrorLogger = null;
private string LogHeader = "[LOG WRITER]"; private string LogHeader = "[LOG WRITER]";
/// <summary> /// <summary>
/// Create a log writer that will not write anything. Good for when not enabled /// Create a log writer that will not write anything. Good for when not enabled
/// but the write statements are still in the code. /// but the write statements are still in the code.
/// </summary> /// </summary>
public LogWriter() public LogWriter()
{ {
Enabled = false; Enabled = false;
m_logFile = null; m_logFile = null;
} }
/// <summary> /// <summary>
/// Create a log writer instance. /// Create a log writer instance.
/// </summary> /// </summary>
/// <param name="dir">The directory to create the log file in. May be 'null' for default.</param> /// <param name="dir">The directory to create the log file in. May be 'null' for default.</param>
/// <param name="headr">The characters that begin the log file name. May be 'null' for default.</param> /// <param name="headr">The characters that begin the log file name. May be 'null' for default.</param>
/// <param name="maxFileTime">Maximum age of a log file in minutes. If zero, will set default.</param> /// <param name="maxFileTime">Maximum age of a log file in minutes. If zero, will set default.</param>
public LogWriter(string dir, string headr, int maxFileTime) public LogWriter(string dir, string headr, int maxFileTime)
{ {
m_logDirectory = dir == null ? "." : dir; m_logDirectory = dir == null ? "." : dir;
LogFileHeader = headr == null ? "log-" : headr; LogFileHeader = headr == null ? "log-" : headr;
m_logMaxFileTimeMin = maxFileTime; m_logMaxFileTimeMin = maxFileTime;
if (m_logMaxFileTimeMin < 1) if (m_logMaxFileTimeMin < 1)
m_logMaxFileTimeMin = 5; m_logMaxFileTimeMin = 5;
m_logFileLife = new TimeSpan(0, m_logMaxFileTimeMin, 0); m_logFileLife = new TimeSpan(0, m_logMaxFileTimeMin, 0);
m_logFileEndTime = DateTime.Now + m_logFileLife; m_logFileEndTime = DateTime.Now + m_logFileLife;
Enabled = true; Enabled = true;
} }
public void Dispose() public void Dispose()
{ {
this.Close(); this.Close();
} }
public void Close() public void Close()
{ {
Enabled = false; Enabled = false;
if (m_logFile != null) if (m_logFile != null)
{ {
m_logFile.Close(); m_logFile.Close();
m_logFile.Dispose(); m_logFile.Dispose();
m_logFile = null; m_logFile = null;
} }
} }
public void Write(string line, params object[] args) public void Write(string line, params object[] args)
{ {
if (!Enabled) return; if (!Enabled) return;
Write(String.Format(line, args)); Write(String.Format(line, args));
} }
public void Write(string line) public void Flush()
{ {
if (!Enabled) return; if (!Enabled) return;
try if (m_logFile != null)
{ {
lock (m_logFileWriteLock) m_logFile.Flush();
{ }
DateTime now = DateTime.Now; }
if (m_logFile == null || now > m_logFileEndTime)
{ public void Write(string line)
if (m_logFile != null) {
{ if (!Enabled) return;
m_logFile.Close(); try
m_logFile.Dispose(); {
m_logFile = null; lock (m_logFileWriteLock)
} {
DateTime now = DateTime.Now;
// First log file or time has expired, start writing to a new log file if (m_logFile == null || now > m_logFileEndTime)
m_logFileEndTime = now + m_logFileLife; {
string path = (m_logDirectory.Length > 0 ? m_logDirectory if (m_logFile != null)
+ System.IO.Path.DirectorySeparatorChar.ToString() : "") {
+ String.Format("{0}{1}.log", LogFileHeader, now.ToString("yyyyMMddHHmmss")); m_logFile.Close();
m_logFile = new StreamWriter(File.Open(path, FileMode.Append, FileAccess.Write)); m_logFile.Dispose();
} m_logFile = null;
if (m_logFile != null) }
{
StringBuilder buff = new StringBuilder(line.Length + 25); // First log file or time has expired, start writing to a new log file
buff.Append(now.ToString("yyyyMMddHHmmssfff")); m_logFileEndTime = now + m_logFileLife;
// buff.Append(now.ToString("yyyyMMddHHmmss")); string path = (m_logDirectory.Length > 0 ? m_logDirectory
buff.Append(","); + System.IO.Path.DirectorySeparatorChar.ToString() : "")
buff.Append(line); + String.Format("{0}{1}.log", LogFileHeader, now.ToString("yyyyMMddHHmmss"));
buff.Append("\r\n"); m_logFile = new StreamWriter(File.Open(path, FileMode.Append, FileAccess.Write));
m_logFile.Write(buff.ToString()); }
} if (m_logFile != null)
} {
} StringBuilder buff = new StringBuilder(line.Length + 25);
catch (Exception e) buff.Append(now.ToString("yyyyMMddHHmmssfff"));
{ // buff.Append(now.ToString("yyyyMMddHHmmss"));
if (ErrorLogger != null) buff.Append(",");
{ buff.Append(line);
ErrorLogger.ErrorFormat("{0}: FAILURE WRITING TO LOGFILE: {1}", LogHeader, e); buff.Append("\r\n");
} m_logFile.Write(buff.ToString());
Enabled = false; }
} }
return; }
} catch (Exception e)
} {
} if (ErrorLogger != null)
{
ErrorLogger.ErrorFormat("{0}: FAILURE WRITING TO LOGFILE: {1}", LogHeader, e);
}
Enabled = false;
}
return;
}
}
}