corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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 #region CVS Information
27 /*
28 * $Source$
29 * $Author: jendave $
30 * $Date: 2006-01-27 16:49:58 -0800 (Fri, 27 Jan 2006) $
31 * $Revision: 71 $
32 */
33 #endregion
34  
35 using System;
36 using System.IO;
37  
38 namespace Prebuild.Core.Utilities
39 {
40 /// <summary>
41 ///
42 /// </summary>
43 public enum LogType
44 {
45 /// <summary>
46 ///
47 /// </summary>
48 None,
49 /// <summary>
50 ///
51 /// </summary>
52 Info,
53 /// <summary>
54 ///
55 /// </summary>
56 Warning,
57 /// <summary>
58 ///
59 /// </summary>
60 Error
61 }
62  
63 /// <summary>
64 ///
65 /// </summary>
66 [Flags]
67 public enum LogTargets
68 {
69 /// <summary>
70 ///
71 /// </summary>
72 None = 0,
73 /// <summary>
74 ///
75 /// </summary>
76 Null = 1,
77 /// <summary>
78 ///
79 /// </summary>
80 File = 2,
81 /// <summary>
82 ///
83 /// </summary>
84 Console = 4
85 }
86  
87 /// <summary>
88 /// Summary description for Log.
89 /// </summary>
90 public class Log : IDisposable
91 {
92 #region Fields
93  
94 private StreamWriter m_Writer;
95 private LogTargets m_Target = LogTargets.Null;
96 bool disposed;
97  
98 #endregion
99  
100 #region Constructors
101  
102 /// <summary>
103 /// Initializes a new instance of the <see cref="Log"/> class.
104 /// </summary>
105 /// <param name="target">The target.</param>
106 /// <param name="fileName">Name of the file.</param>
107 public Log(LogTargets target, string fileName)
108 {
109 m_Target = target;
110  
111 if((m_Target & LogTargets.File) != 0)
112 {
113 m_Writer = new StreamWriter(fileName, false);
114 }
115 }
116  
117 #endregion
118  
119 #region Public Methods
120  
121 /// <summary>
122 /// Writes this instance.
123 /// </summary>
124 public void Write()
125 {
126 Write(string.Empty);
127 }
128  
129 /// <summary>
130 /// Writes the specified MSG.
131 /// </summary>
132 /// <param name="msg">The MSG.</param>
133 public void Write(string msg)
134 {
135 if((m_Target & LogTargets.Null) != 0)
136 {
137 return;
138 }
139  
140 if((m_Target & LogTargets.Console) != 0)
141 {
142 Console.WriteLine(msg);
143 }
144 if((m_Target & LogTargets.File) != 0 && m_Writer != null)
145 {
146 m_Writer.WriteLine(msg);
147 }
148 }
149  
150 /// <summary>
151 /// Writes the specified format.
152 /// </summary>
153 /// <param name="format">The format.</param>
154 /// <param name="args">The args.</param>
155 public void Write(string format, params object[] args)
156 {
157 Write(string.Format(format,args));
158 }
159  
160 /// <summary>
161 /// Writes the specified type.
162 /// </summary>
163 /// <param name="type">The type.</param>
164 /// <param name="format">The format.</param>
165 /// <param name="args">The args.</param>
166 public void Write(LogType type, string format, params object[] args)
167 {
168 if((m_Target & LogTargets.Null) != 0)
169 {
170 return;
171 }
172  
173 string str = "";
174 switch(type)
175 {
176 case LogType.Info:
177 str = "[I] ";
178 break;
179 case LogType.Warning:
180 str = "[!] ";
181 break;
182 case LogType.Error:
183 str = "[X] ";
184 break;
185 }
186  
187 Write(str + format,args);
188 }
189  
190 /// <summary>
191 /// Writes the exception.
192 /// </summary>
193 /// <param name="type">The type.</param>
194 /// <param name="ex">The ex.</param>
195 public void WriteException(LogType type, Exception ex)
196 {
197 if(ex != null)
198 {
199 Write(type, ex.Message);
200 //#if DEBUG
201 m_Writer.WriteLine("Exception @{0} stack trace [[", ex.TargetSite.Name);
202 m_Writer.WriteLine(ex.StackTrace);
203 m_Writer.WriteLine("]]");
204 //#endif
205 }
206 }
207  
208 /// <summary>
209 /// Flushes this instance.
210 /// </summary>
211 public void Flush()
212 {
213 if(m_Writer != null)
214 {
215 m_Writer.Flush();
216 }
217 }
218  
219 #endregion
220  
221 #region IDisposable Members
222  
223 /// <summary>
224 /// Performs application-defined tasks associated with freeing, releasing, or
225 /// resetting unmanaged resources.
226 /// </summary>
227 public void Dispose()
228 {
229 Dispose(true);
230 GC.SuppressFinalize(this);
231 }
232  
233 /// <summary>
234 /// Dispose objects
235 /// </summary>
236 /// <param name="disposing">
237 /// If true, it will dispose close the handle
238 /// </param>
239 /// <remarks>
240 /// Will dispose managed and unmanaged resources.
241 /// </remarks>
242 protected virtual void Dispose(bool disposing)
243 {
244 if (!this.disposed)
245 {
246 if (disposing)
247 {
248 if (m_Writer != null)
249 {
250 m_Writer.Close();
251 m_Writer = null;
252 }
253 }
254 }
255 this.disposed = true;
256 }
257  
258 /// <summary>
259 ///
260 /// </summary>
261 ~Log()
262 {
263 this.Dispose(false);
264 }
265  
266 /// <summary>
267 /// Closes and destroys this object
268 /// </summary>
269 /// <remarks>
270 /// Same as Dispose(true)
271 /// </remarks>
272 public void Close()
273 {
274 Dispose();
275 }
276  
277 #endregion
278 }
279 }