diff options
Diffstat (limited to 'OpenSim/Framework/LogWriter.cs')
-rwxr-xr-x | OpenSim/Framework/LogWriter.cs | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/OpenSim/Framework/LogWriter.cs b/OpenSim/Framework/LogWriter.cs new file mode 100755 index 0000000..2e0bf4a --- /dev/null +++ b/OpenSim/Framework/LogWriter.cs | |||
@@ -0,0 +1,181 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.IO; | ||
30 | using System.Text; | ||
31 | using log4net; | ||
32 | |||
33 | namespace OpenSim.Framework | ||
34 | { | ||
35 | /// <summary> | ||
36 | /// Class for writing a high performance, high volume log file. | ||
37 | /// Sometimes, to debug, one has a high volume logging to do and the regular | ||
38 | /// log file output is not appropriate. | ||
39 | /// Create a new instance with the parameters needed and | ||
40 | /// call Write() to output a line. Call Close() when finished. | ||
41 | /// If created with no parameters, it will not log anything. | ||
42 | /// </summary> | ||
43 | public class LogWriter : IDisposable | ||
44 | { | ||
45 | public bool Enabled { get; private set; } | ||
46 | |||
47 | private string m_logDirectory = "."; | ||
48 | private int m_logMaxFileTimeMin = 5; // 5 minutes | ||
49 | public String LogFileHeader { get; set; } | ||
50 | |||
51 | private StreamWriter m_logFile = null; | ||
52 | private TimeSpan m_logFileLife; | ||
53 | private DateTime m_logFileEndTime; | ||
54 | private Object m_logFileWriteLock = new Object(); | ||
55 | private bool m_flushWrite; | ||
56 | |||
57 | // set externally when debugging. If let 'null', this does not write any error messages. | ||
58 | public ILog ErrorLogger = null; | ||
59 | private string LogHeader = "[LOG WRITER]"; | ||
60 | |||
61 | /// <summary> | ||
62 | /// Create a log writer that will not write anything. Good for when not enabled | ||
63 | /// but the write statements are still in the code. | ||
64 | /// </summary> | ||
65 | public LogWriter() | ||
66 | { | ||
67 | Enabled = false; | ||
68 | m_logFile = null; | ||
69 | } | ||
70 | |||
71 | /// <summary> | ||
72 | /// Create a log writer instance. | ||
73 | /// </summary> | ||
74 | /// <param name="dir">The directory to create the log file in. May be 'null' for default.</param> | ||
75 | /// <param name="headr">The characters that begin the log file name. May be 'null' for default.</param> | ||
76 | /// <param name="maxFileTime">Maximum age of a log file in minutes. If zero, will set default.</param> | ||
77 | /// <param name="flushWrite">Whether to do a flush after every log write. Best left off but | ||
78 | /// if one is looking for a crash, this is a good thing to turn on.</param> | ||
79 | public LogWriter(string dir, string headr, int maxFileTime, bool flushWrite) | ||
80 | { | ||
81 | m_logDirectory = dir == null ? "." : dir; | ||
82 | |||
83 | LogFileHeader = headr == null ? "log-" : headr; | ||
84 | |||
85 | m_logMaxFileTimeMin = maxFileTime; | ||
86 | if (m_logMaxFileTimeMin < 1) | ||
87 | m_logMaxFileTimeMin = 5; | ||
88 | |||
89 | m_logFileLife = new TimeSpan(0, m_logMaxFileTimeMin, 0); | ||
90 | m_logFileEndTime = DateTime.Now + m_logFileLife; | ||
91 | |||
92 | m_flushWrite = flushWrite; | ||
93 | |||
94 | Enabled = true; | ||
95 | } | ||
96 | // Constructor that assumes flushWrite is off. | ||
97 | public LogWriter(string dir, string headr, int maxFileTime) : this(dir, headr, maxFileTime, false) | ||
98 | { | ||
99 | } | ||
100 | |||
101 | public void Dispose() | ||
102 | { | ||
103 | this.Close(); | ||
104 | } | ||
105 | |||
106 | public void Close() | ||
107 | { | ||
108 | Enabled = false; | ||
109 | if (m_logFile != null) | ||
110 | { | ||
111 | m_logFile.Close(); | ||
112 | m_logFile.Dispose(); | ||
113 | m_logFile = null; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | public void Write(string line, params object[] args) | ||
118 | { | ||
119 | if (!Enabled) return; | ||
120 | Write(String.Format(line, args)); | ||
121 | } | ||
122 | |||
123 | public void Flush() | ||
124 | { | ||
125 | if (!Enabled) return; | ||
126 | if (m_logFile != null) | ||
127 | { | ||
128 | m_logFile.Flush(); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | public void Write(string line) | ||
133 | { | ||
134 | if (!Enabled) return; | ||
135 | try | ||
136 | { | ||
137 | lock (m_logFileWriteLock) | ||
138 | { | ||
139 | DateTime now = DateTime.UtcNow; | ||
140 | if (m_logFile == null || now > m_logFileEndTime) | ||
141 | { | ||
142 | if (m_logFile != null) | ||
143 | { | ||
144 | m_logFile.Close(); | ||
145 | m_logFile.Dispose(); | ||
146 | m_logFile = null; | ||
147 | } | ||
148 | |||
149 | // First log file or time has expired, start writing to a new log file | ||
150 | m_logFileEndTime = now + m_logFileLife; | ||
151 | string path = (m_logDirectory.Length > 0 ? m_logDirectory | ||
152 | + System.IO.Path.DirectorySeparatorChar.ToString() : "") | ||
153 | + String.Format("{0}{1}.log", LogFileHeader, now.ToString("yyyyMMddHHmmss")); | ||
154 | m_logFile = new StreamWriter(File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)); | ||
155 | } | ||
156 | if (m_logFile != null) | ||
157 | { | ||
158 | StringBuilder buff = new StringBuilder(line.Length + 25); | ||
159 | buff.Append(now.ToString("yyyyMMddHHmmssfff")); | ||
160 | // buff.Append(now.ToString("yyyyMMddHHmmss")); | ||
161 | buff.Append(","); | ||
162 | buff.Append(line); | ||
163 | buff.Append("\r\n"); | ||
164 | m_logFile.Write(buff.ToString()); | ||
165 | if (m_flushWrite) | ||
166 | m_logFile.Flush(); | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | catch (Exception e) | ||
171 | { | ||
172 | if (ErrorLogger != null) | ||
173 | { | ||
174 | ErrorLogger.ErrorFormat("{0}: FAILURE WRITING TO LOGFILE: {1}", LogHeader, e); | ||
175 | } | ||
176 | Enabled = false; | ||
177 | } | ||
178 | return; | ||
179 | } | ||
180 | } | ||
181 | } | ||