opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 1 | #region BSD License |
2 | /* |
||
3 | Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) |
||
4 | |||
5 | Redistribution and use in source and binary forms, with or without modification, are permitted |
||
6 | provided that the following conditions are met: |
||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, this list of conditions |
||
9 | and the following disclaimer. |
||
10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions |
||
11 | and the following disclaimer in the documentation and/or other materials provided with the |
||
12 | distribution. |
||
13 | * The name of the author may not be used to endorse or promote products derived from this software |
||
14 | without specific prior written permission. |
||
15 | |||
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
||
17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
20 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
||
21 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
22 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
23 | */ |
||
24 | #endregion |
||
25 | |||
26 | using System; |
||
27 | using System.IO; |
||
28 | |||
29 | namespace Prebuild.Core.Utilities |
||
30 | { |
||
31 | /// <summary> |
||
32 | /// |
||
33 | /// </summary> |
||
34 | public enum LogType |
||
35 | { |
||
36 | /// <summary> |
||
37 | /// |
||
38 | /// </summary> |
||
39 | None, |
||
40 | /// <summary> |
||
41 | /// |
||
42 | /// </summary> |
||
43 | Info, |
||
44 | /// <summary> |
||
45 | /// |
||
46 | /// </summary> |
||
47 | Warning, |
||
48 | /// <summary> |
||
49 | /// |
||
50 | /// </summary> |
||
51 | Error |
||
52 | } |
||
53 | |||
54 | /// <summary> |
||
55 | /// |
||
56 | /// </summary> |
||
57 | [Flags] |
||
58 | public enum LogTargets |
||
59 | { |
||
60 | /// <summary> |
||
61 | /// |
||
62 | /// </summary> |
||
63 | None = 0, |
||
64 | /// <summary> |
||
65 | /// |
||
66 | /// </summary> |
||
67 | Null = 1, |
||
68 | /// <summary> |
||
69 | /// |
||
70 | /// </summary> |
||
71 | File = 2, |
||
72 | /// <summary> |
||
73 | /// |
||
74 | /// </summary> |
||
75 | Console = 4 |
||
76 | } |
||
77 | |||
78 | /// <summary> |
||
79 | /// Summary description for Log. |
||
80 | /// </summary> |
||
81 | public class Log : IDisposable |
||
82 | { |
||
83 | #region Fields |
||
84 | |||
85 | private TextWriter m_Writer; |
||
86 | private LogTargets m_Target = LogTargets.Null; |
||
87 | bool disposed; |
||
88 | |||
89 | #endregion |
||
90 | |||
91 | #region Constructors |
||
92 | |||
93 | /// <summary> |
||
94 | /// Initializes a new instance of the <see cref="Log"/> class. |
||
95 | /// </summary> |
||
96 | /// <param name="target">The target.</param> |
||
97 | /// <param name="fileName">Name of the file.</param> |
||
98 | public Log(LogTargets target, string fileName) |
||
99 | { |
||
100 | m_Target = target; |
||
101 | |||
102 | if ((m_Target & LogTargets.File) != 0) |
||
103 | { |
||
104 | m_Writer = new StreamWriter(fileName, false); |
||
105 | } |
||
106 | else if ((m_Target & LogTargets.Console) != 0) |
||
107 | { |
||
108 | // Prevents null reference exceptions when outputing to the log file. |
||
109 | // This seems to only happen when running on a network drive. |
||
110 | m_Writer = Console.Out; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | #endregion |
||
115 | |||
116 | #region Public Methods |
||
117 | |||
118 | /// <summary> |
||
119 | /// Writes this instance. |
||
120 | /// </summary> |
||
121 | public void Write() |
||
122 | { |
||
123 | Write(string.Empty); |
||
124 | } |
||
125 | |||
126 | /// <summary> |
||
127 | /// Writes the specified MSG. |
||
128 | /// </summary> |
||
129 | /// <param name="msg">The MSG.</param> |
||
130 | public void Write(string msg) |
||
131 | { |
||
132 | if((m_Target & LogTargets.Null) != 0) |
||
133 | { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | if((m_Target & LogTargets.Console) != 0) |
||
138 | { |
||
139 | Console.WriteLine(msg); |
||
140 | } |
||
141 | if((m_Target & LogTargets.File) != 0 && m_Writer != null) |
||
142 | { |
||
143 | m_Writer.WriteLine(msg); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /// <summary> |
||
148 | /// Writes the specified format. |
||
149 | /// </summary> |
||
150 | /// <param name="format">The format.</param> |
||
151 | /// <param name="args">The args.</param> |
||
152 | public void Write(string format, params object[] args) |
||
153 | { |
||
154 | Write(string.Format(format,args)); |
||
155 | } |
||
156 | |||
157 | /// <summary> |
||
158 | /// Writes the specified type. |
||
159 | /// </summary> |
||
160 | /// <param name="type">The type.</param> |
||
161 | /// <param name="format">The format.</param> |
||
162 | /// <param name="args">The args.</param> |
||
163 | public void Write(LogType type, string format, params object[] args) |
||
164 | { |
||
165 | if((m_Target & LogTargets.Null) != 0) |
||
166 | { |
||
167 | return; |
||
168 | } |
||
169 | |||
170 | string str = ""; |
||
171 | switch(type) |
||
172 | { |
||
173 | case LogType.Info: |
||
174 | str = "[I] "; |
||
175 | break; |
||
176 | case LogType.Warning: |
||
177 | str = "[!] "; |
||
178 | break; |
||
179 | case LogType.Error: |
||
180 | str = "[X] "; |
||
181 | break; |
||
182 | } |
||
183 | |||
184 | Write(str + format,args); |
||
185 | } |
||
186 | |||
187 | /// <summary> |
||
188 | /// Writes the exception. |
||
189 | /// </summary> |
||
190 | /// <param name="type">The type.</param> |
||
191 | /// <param name="ex">The ex.</param> |
||
192 | public void WriteException(LogType type, Exception ex) |
||
193 | { |
||
194 | if(ex != null) |
||
195 | { |
||
196 | Write(type, ex.Message); |
||
197 | //#if DEBUG |
||
198 | m_Writer.WriteLine("Exception @{0} stack trace [[", ex.TargetSite.Name); |
||
199 | m_Writer.WriteLine(ex.StackTrace); |
||
200 | m_Writer.WriteLine("]]"); |
||
201 | //#endif |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /// <summary> |
||
206 | /// Flushes this instance. |
||
207 | /// </summary> |
||
208 | public void Flush() |
||
209 | { |
||
210 | if(m_Writer != null) |
||
211 | { |
||
212 | m_Writer.Flush(); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | #endregion |
||
217 | |||
218 | #region IDisposable Members |
||
219 | |||
220 | /// <summary> |
||
221 | /// Performs application-defined tasks associated with freeing, releasing, or |
||
222 | /// resetting unmanaged resources. |
||
223 | /// </summary> |
||
224 | public void Dispose() |
||
225 | { |
||
226 | Dispose(true); |
||
227 | GC.SuppressFinalize(this); |
||
228 | } |
||
229 | |||
230 | /// <summary> |
||
231 | /// Dispose objects |
||
232 | /// </summary> |
||
233 | /// <param name="disposing"> |
||
234 | /// If true, it will dispose close the handle |
||
235 | /// </param> |
||
236 | /// <remarks> |
||
237 | /// Will dispose managed and unmanaged resources. |
||
238 | /// </remarks> |
||
239 | protected virtual void Dispose(bool disposing) |
||
240 | { |
||
241 | if (!this.disposed) |
||
242 | { |
||
243 | if (disposing) |
||
244 | { |
||
245 | if (m_Writer != null) |
||
246 | { |
||
247 | m_Writer.Close(); |
||
248 | m_Writer = null; |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | this.disposed = true; |
||
253 | } |
||
254 | |||
255 | /// <summary> |
||
256 | /// |
||
257 | /// </summary> |
||
258 | ~Log() |
||
259 | { |
||
260 | this.Dispose(false); |
||
261 | } |
||
262 | |||
263 | /// <summary> |
||
264 | /// Closes and destroys this object |
||
265 | /// </summary> |
||
266 | /// <remarks> |
||
267 | /// Same as Dispose(true) |
||
268 | /// </remarks> |
||
269 | public void Close() |
||
270 | { |
||
271 | Dispose(); |
||
272 | } |
||
273 | |||
274 | #endregion |
||
275 | } |
||
276 | } |