(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Npgsql / Npgsql / NpgsqlEventLog.cs
blobb1a28bbe670114b8747d4368accd3119481bb4ee
1 // created on 07/06/2002 at 09:34
3 // Npgsql.NpgsqlEventLog.cs
4 //
5 // Author:
6 // Dave Page (dpage@postgresql.org)
7 //
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 using System.IO;
28 using System.Text;
29 using System.Diagnostics;
30 using System;
31 using System.Resources;
33 namespace Npgsql
36 /// <summary>
37 /// The level of verbosity of the NpgsqlEventLog
38 /// </summary>
39 public enum LogLevel {
40 /// <summary>
41 /// Don't log at all
42 /// </summary>
43 None = 0,
44 /// <summary>
45 /// Only log the most common issues
46 /// </summary>
47 Normal = 1,
48 /// <summary>
49 /// Log everything
50 /// </summary>
51 Debug = 2
55 /// <summary>
56 /// This class handles all the Npgsql event and debug logging
57 /// </summary>
58 public class NpgsqlEventLog
60 // Logging related values
61 private static readonly String CLASSNAME = "NpgsqlEventLog";
62 private static String logfile;
63 private static LogLevel level;
64 private static Boolean echomessages;
66 private static ResourceManager LogResMan;
68 private NpgsqlEventLog()
70 // static constructor
71 static NpgsqlEventLog()
73 LogResMan = new ResourceManager(typeof(NpgsqlEventLog));
76 ///<summary>
77 /// Sets/Returns the level of information to log to the logfile.
78 /// </summary>
79 /// <value>The current <see cref="Npgsql.LogLevel">LogLevel</see></value>
80 public static LogLevel Level {
81 get
83 LogPropertyGet(LogLevel.Debug, CLASSNAME, "LogLevel");
84 return level;
86 set
88 LogPropertySet(LogLevel.Debug, CLASSNAME, "LogLevel", value);
89 level = value;
93 ///<summary>
94 /// Sets/Returns the filename to use for logging.
95 /// </summary>
96 /// <value>The filename of the current Log file.</value>
97 public static String LogName {
98 get
100 LogPropertyGet(LogLevel.Debug, CLASSNAME, "LogName");
101 return logfile;
105 LogPropertySet(LogLevel.Normal, CLASSNAME, "LogName", value);
106 logfile = value;
110 ///<summary>
111 /// Sets/Returns whether Log messages should be echoed to the console
112 /// </summary>
113 /// <value><b>true</b> if Log messages are echoed to the console, otherwise <b>false</b></value>
114 public static Boolean EchoMessages {
117 LogPropertyGet(LogLevel.Debug, CLASSNAME, "EchoMessages");
118 return echomessages;
122 LogPropertySet(LogLevel.Normal, CLASSNAME, "EchoMessages", value);
123 echomessages = value;
127 // Event/Debug Logging
128 // This method should be private and only used by the internal methods that support localization.
129 /// <summary>
130 /// Writes a string to the Npgsql event log if msglevel is bigger then <see cref="Npgsql.NpgsqlEventLog.Level">NpgsqlEventLog.Level</see>
131 /// </summary>
132 /// <remarks>
133 /// This method is obsolete and should no longer be used.
134 /// It is likely to be removed in future versions of Npgsql
135 /// </remarks>
136 /// <param name="message">The message to write to the event log</param>
137 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
138 private static void LogMsg(String message, LogLevel msglevel)
140 if (msglevel > level)
141 return;
143 Process proc = Process.GetCurrentProcess();
145 if (echomessages)
147 Console.WriteLine(message);
150 if (logfile != null)
152 if (logfile != "")
155 StreamWriter writer = new StreamWriter(logfile, true);
157 // The format of the logfile is
158 // [Date] [Time] [PID] [Level] [Message]
159 writer.WriteLine(System.DateTime.Now + "\t" + proc.Id + "\t" + msglevel + "\t" + message);
160 writer.Close();
165 /// <summary>
166 /// Writes a string to the Npgsql event log if msglevel is bigger then <see cref="Npgsql.NpgsqlEventLog.Level">NpgsqlEventLog.Level</see>
167 /// </summary>
168 /// <param name="resman">The <see cref="System.Resources.ResourceManager">ResourceManager</see> to get the localized resources</param>
169 /// <param name="ResourceString">The name of the resource that should be fetched by the <see cref="System.Resources.ResourceManager">ResourceManager</see></param>
170 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
171 /// <param name="Parameters">The additional parameters that shall be included into the log-message (must be compatible with the string in the resource):</param>
172 internal static void LogMsg(ResourceManager resman, string ResourceString, LogLevel msglevel, params Object[] Parameters)
174 if (msglevel > level)
175 return;
177 string message = resman.GetString(ResourceString);
179 if (message == null) {
180 message = String.Format("Unable to find resource string {0} for class {1}", ResourceString, resman.BaseName);
181 } else if (Parameters.Length > 0) {
182 message = String.Format(message, Parameters);
185 LogMsg(message, msglevel);
188 /// <summary>
189 /// Writes the default log-message for the action of calling the Get-part of an Indexer to the log file.
190 /// </summary>
191 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
192 /// <param name="ClassName">The name of the class that contains the Indexer</param>
193 /// <param name="IndexerParam">The parameter given to the Indexer</param>
194 internal static void LogIndexerGet(LogLevel msglevel, string ClassName, object IndexerParam)
196 if (msglevel > level)
197 return;
198 string message = String.Format(LogResMan.GetString("Indexer_Get"), ClassName, IndexerParam);
199 LogMsg(message, msglevel);
202 /// <summary>
203 /// Writes the default log-message for the action of calling the Set-part of an Indexer to the logfile.
204 /// </summary>
205 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
206 /// <param name="ClassName">The name of the class that contains the Indexer</param>
207 /// <param name="IndexerParam">The parameter given to the Indexer</param>
208 /// <param name="value">The value the Indexer is set to</param>
209 internal static void LogIndexerSet(LogLevel msglevel, string ClassName, object IndexerParam, object value)
211 if (msglevel > level)
212 return;
213 string message = String.Format(LogResMan.GetString("Indexer_Set"), ClassName, IndexerParam, value);
214 LogMsg(message, msglevel);
217 /// <summary>
218 /// Writes the default log-message for the action of calling the Get-part of a Property to the logfile.
219 /// </summary>
220 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
221 /// <param name="ClassName">The name of the class that contains the Property</param>
222 /// <param name="PropertyName">The name of the Property</param>
223 internal static void LogPropertyGet(LogLevel msglevel, string ClassName, string PropertyName)
225 if (msglevel > level)
226 return;
227 string message = String.Format(LogResMan.GetString("Property_Get"), ClassName, PropertyName);
228 LogMsg(message, msglevel);
231 /// <summary>
232 /// Writes the default log-message for the action of calling the Set-part of a Property to the logfile.
233 /// </summary>
234 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
235 /// <param name="ClassName">The name of the class that contains the Property</param>
236 /// <param name="PropertyName">The name of the Property</param>
237 /// <param name="value">The value the Property is set to</param>
238 internal static void LogPropertySet(LogLevel msglevel, string ClassName, string PropertyName, object value)
240 if (msglevel > level)
241 return;
242 string message = String.Format(LogResMan.GetString("Property_Set"), ClassName, PropertyName, value);
243 LogMsg(message, msglevel);
246 /// <summary>
247 /// Writes the default log-message for the action of calling a Method without Arguments to the logfile.
248 /// </summary>
249 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
250 /// <param name="ClassName">The name of the class that contains the Method</param>
251 /// <param name="MethodName">The name of the Method</param>
252 internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName)
254 if (msglevel > level)
255 return;
256 string message = String.Format(LogResMan.GetString("Method_0P_Enter"), ClassName, MethodName);
257 LogMsg(message, msglevel);
260 /// <summary>
261 /// Writes the default log-message for the action of calling a Method with one Argument to the logfile.
262 /// </summary>
263 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
264 /// <param name="ClassName">The name of the class that contains the Method</param>
265 /// <param name="MethodName">The name of the Method</param>
266 /// <param name="MethodParameter">The value of the Argument of the Method</param>
267 internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter)
269 if (msglevel > level)
270 return;
271 string message = String.Format(LogResMan.GetString("Method_1P_Enter"), ClassName, MethodName, MethodParameter);
272 LogMsg(message, msglevel);
275 /// <summary>
276 /// Writes the default log-message for the action of calling a Method with two Arguments to the logfile.
277 /// </summary>
278 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
279 /// <param name="ClassName">The name of the class that contains the Method</param>
280 /// <param name="MethodName">The name of the Method</param>
281 /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
282 /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
283 internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter1, object MethodParameter2)
285 if (msglevel > level)
286 return;
287 string message = String.Format(LogResMan.GetString("Method_2P_Enter"), ClassName, MethodName, MethodParameter1, MethodParameter2);
288 LogMsg(message, msglevel);
291 /// <summary>
292 /// Writes the default log-message for the action of calling a Method with three Arguments to the logfile.
293 /// </summary>
294 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
295 /// <param name="ClassName">The name of the class that contains the Method</param>
296 /// <param name="MethodName">The name of the Method</param>
297 /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
298 /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
299 /// <param name="MethodParameter3">The value of the third Argument of the Method</param>
300 internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter1, object MethodParameter2, object MethodParameter3)
302 if (msglevel > level)
303 return;
304 string message = String.Format(LogResMan.GetString("Method_3P_Enter"), ClassName, MethodName, MethodParameter1, MethodParameter2, MethodParameter3);
305 LogMsg(message, msglevel);
308 /// <summary>
309 /// Writes the default log-message for the action of calling a Method with more than three Arguments to the logfile.
310 /// </summary>
311 /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
312 /// <param name="ClassName">The name of the class that contains the Method</param>
313 /// <param name="MethodName">The name of the Method</param>
314 /// <param name="MethodParameters">A <see cref="System.Object">Object</see>-Array with zero or more Ojects that are Arguments of the Method.</param>
315 internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, params object[] MethodParameters)
317 if (msglevel > level)
318 return;
319 string message = String.Empty;
320 switch (MethodParameters.Length)
322 case 4:
323 message = String.Format(LogResMan.GetString("Method_4P_Enter"), ClassName, MethodName,
324 MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3]);
325 break;
326 case 5:
327 message = String.Format(LogResMan.GetString("Method_5P_Enter"), ClassName, MethodName,
328 MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3], MethodParameters[4]);
329 break;
330 case 6:
331 message = String.Format(LogResMan.GetString("Method_6P_Enter"), ClassName, MethodName,
332 MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3], MethodParameters[4], MethodParameters[5]);
333 break;
334 default:
335 // should always be true - but who knows ;-)
336 if (MethodParameters.Length > 6)
337 message = String.Format(LogResMan.GetString("Method_6P+_Enter"), ClassName, MethodName, MethodParameters[0], MethodParameters[1]);
338 break;
340 LogMsg(message, msglevel);