* FileSystemInfo.cs: corrected COM visibility of UTC properties
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlEventLog.cs
1 // created on 07/06/2002 at 09:34
2
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
11 //
12 //
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.
17 //
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.
22 //
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
26
27 using System.IO;
28 using System.Text;
29 using System.Diagnostics;
30 using System;
31 using System.Resources;
32
33 namespace Npgsql
34 {
35
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
52     }
53
54
55     /// <summary>
56     /// This class handles all the Npgsql event and debug logging
57     /// </summary>
58     public class NpgsqlEventLog
59     {
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;
65
66         private static ResourceManager LogResMan;
67
68         private NpgsqlEventLog()
69         {}
70         // static constructor
71         static NpgsqlEventLog()
72         {
73             LogResMan = new ResourceManager(typeof(NpgsqlEventLog));
74         }
75
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
82             {
83                 LogPropertyGet(LogLevel.Debug, CLASSNAME, "LogLevel");
84                 return level;
85             }
86             set
87             {
88                 LogPropertySet(LogLevel.Debug, CLASSNAME, "LogLevel", value);
89                 level = value;
90             }
91         }
92
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
99             {
100                 LogPropertyGet(LogLevel.Debug, CLASSNAME, "LogName");
101                 return logfile;
102             }
103             set
104             {
105                 LogPropertySet(LogLevel.Normal, CLASSNAME, "LogName", value);
106                 logfile = value;
107             }
108         }
109
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 {
115             get
116             {
117                 LogPropertyGet(LogLevel.Debug, CLASSNAME, "EchoMessages");
118                 return echomessages;
119             }
120             set
121             {
122                 LogPropertySet(LogLevel.Normal, CLASSNAME, "EchoMessages", value);
123                 echomessages = value;
124             }
125         }
126
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)
139         {
140             if (msglevel > level)
141                 return;
142
143             Process proc = Process.GetCurrentProcess();
144
145             if (echomessages)
146             {
147                 Console.WriteLine(message);
148             }
149             
150             if (logfile != null)
151             {
152                 if (logfile != "")
153                 {
154
155                     StreamWriter writer = new StreamWriter(logfile, true);
156
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();
161                 }
162             }
163         }
164
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)
173         {
174             if (msglevel > level)
175                 return;
176
177             if(Parameters.Length == 0)
178             {
179                 string message = resman.GetString(ResourceString);
180                 LogMsg(message, msglevel);
181             }
182             else
183             {
184                 string message = String.Format(resman.GetString(ResourceString), Parameters);
185                 LogMsg(message, msglevel);
186             }
187         }
188
189         /// <summary>
190         /// Writes the default log-message for the action of calling the Get-part of an Indexer to the log file.
191         /// </summary>
192         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
193         /// <param name="ClassName">The name of the class that contains the Indexer</param>
194         /// <param name="IndexerParam">The parameter given to the Indexer</param>
195         internal static void LogIndexerGet(LogLevel msglevel, string ClassName, object IndexerParam)
196         {
197             if (msglevel > level)
198                 return;
199             string message = String.Format(LogResMan.GetString("Indexer_Get"), ClassName, IndexerParam);
200             LogMsg(message, msglevel);
201         }
202
203         /// <summary>
204         /// Writes the default log-message for the action of calling the Set-part of an Indexer to the logfile.
205         /// </summary>
206         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
207         /// <param name="ClassName">The name of the class that contains the Indexer</param>
208         /// <param name="IndexerParam">The parameter given to the Indexer</param>
209         /// <param name="value">The value the Indexer is set to</param>
210         internal static void LogIndexerSet(LogLevel msglevel, string ClassName, object IndexerParam, object value)
211         {
212             if (msglevel > level)
213                 return;
214             string message = String.Format(LogResMan.GetString("Indexer_Set"), ClassName, IndexerParam, value);
215             LogMsg(message, msglevel);
216         }
217
218         /// <summary>
219         /// Writes the default log-message for the action of calling the Get-part of a Property to the logfile.
220         /// </summary>
221         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
222         /// <param name="ClassName">The name of the class that contains the Property</param>
223         /// <param name="PropertyName">The name of the Property</param>
224         internal static void LogPropertyGet(LogLevel msglevel, string ClassName, string PropertyName)
225         {
226             if (msglevel > level)
227                 return;
228             string message = String.Format(LogResMan.GetString("Property_Get"), ClassName, PropertyName);
229             LogMsg(message, msglevel);
230         }
231
232         /// <summary>
233         /// Writes the default log-message for the action of calling the Set-part of a Property to the logfile.
234         /// </summary>
235         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
236         /// <param name="ClassName">The name of the class that contains the Property</param>
237         /// <param name="PropertyName">The name of the Property</param>
238         /// <param name="value">The value the Property is set to</param>
239         internal static void LogPropertySet(LogLevel msglevel, string ClassName, string PropertyName, object value)
240         {
241             if (msglevel > level)
242                 return;
243             string message = String.Format(LogResMan.GetString("Property_Set"), ClassName, PropertyName, value);
244             LogMsg(message, msglevel);
245         }
246
247         /// <summary>
248         /// Writes the default log-message for the action of calling a Method without Arguments to the logfile.
249         /// </summary>
250         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
251         /// <param name="ClassName">The name of the class that contains the Method</param>
252         /// <param name="MethodName">The name of the Method</param>
253         internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName)
254         {
255             if (msglevel > level)
256                 return;
257             string message = String.Format(LogResMan.GetString("Method_0P_Enter"), ClassName, MethodName);
258             LogMsg(message, msglevel);
259         }
260
261         /// <summary>
262         /// Writes the default log-message for the action of calling a Method with one Argument to the logfile.
263         /// </summary>
264         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
265         /// <param name="ClassName">The name of the class that contains the Method</param>
266         /// <param name="MethodName">The name of the Method</param>
267         /// <param name="MethodParameter">The value of the Argument of the Method</param>
268         internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter)
269         {
270             if (msglevel > level)
271                 return;
272             string message = String.Format(LogResMan.GetString("Method_1P_Enter"), ClassName, MethodName, MethodParameter);
273             LogMsg(message, msglevel);
274         }
275
276         /// <summary>
277         /// Writes the default log-message for the action of calling a Method with two Arguments to the logfile.
278         /// </summary>
279         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
280         /// <param name="ClassName">The name of the class that contains the Method</param>
281         /// <param name="MethodName">The name of the Method</param>
282         /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
283         /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
284         internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter1, object MethodParameter2)
285         {
286             if (msglevel > level)
287                 return;
288             string message = String.Format(LogResMan.GetString("Method_2P_Enter"), ClassName, MethodName, MethodParameter1, MethodParameter2);
289             LogMsg(message, msglevel);
290         }
291
292         /// <summary>
293         /// Writes the default log-message for the action of calling a Method with three Arguments to the logfile.
294         /// </summary>
295         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
296         /// <param name="ClassName">The name of the class that contains the Method</param>
297         /// <param name="MethodName">The name of the Method</param>
298         /// <param name="MethodParameter1">The value of the first Argument of the Method</param>
299         /// <param name="MethodParameter2">The value of the second Argument of the Method</param>
300         /// <param name="MethodParameter3">The value of the third Argument of the Method</param>
301         internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, object MethodParameter1, object MethodParameter2, object MethodParameter3)
302         {
303             if (msglevel > level)
304                 return;
305             string message = String.Format(LogResMan.GetString("Method_3P_Enter"), ClassName, MethodName, MethodParameter1, MethodParameter2, MethodParameter3);
306             LogMsg(message, msglevel);
307         }
308
309         /// <summary>
310         /// Writes the default log-message for the action of calling a Method with more than three Arguments to the logfile.
311         /// </summary>
312         /// <param name="msglevel">The minimum <see cref="Npgsql.LogLevel">LogLevel</see> for which this message should be logged.</param>
313         /// <param name="ClassName">The name of the class that contains the Method</param>
314         /// <param name="MethodName">The name of the Method</param>
315         /// <param name="MethodParameters">A <see cref="System.Object">Object</see>-Array with zero or more Ojects that are Arguments of the Method.</param>
316         internal static void LogMethodEnter(LogLevel msglevel, string ClassName, string MethodName, params object[] MethodParameters)
317         {
318             if (msglevel > level)
319                 return;
320             string message = String.Empty;
321             switch (MethodParameters.Length)
322             {
323             case 4:
324                 message = String.Format(LogResMan.GetString("Method_4P_Enter"), ClassName, MethodName,
325                                         MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3]);
326                 break;
327             case 5:
328                 message = String.Format(LogResMan.GetString("Method_5P_Enter"), ClassName, MethodName,
329                                         MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3], MethodParameters[4]);
330                 break;
331             case 6:
332                 message = String.Format(LogResMan.GetString("Method_6P_Enter"), ClassName, MethodName,
333                                         MethodParameters[0], MethodParameters[1], MethodParameters[2], MethodParameters[3], MethodParameters[4], MethodParameters[5]);
334                 break;
335             default:
336                 // should always be true - but who knows ;-)
337                 if (MethodParameters.Length > 6)
338                     message = String.Format(LogResMan.GetString("Method_6P+_Enter"), ClassName, MethodName, MethodParameters[0], MethodParameters[1]);
339                 break;
340             }
341             LogMsg(message, msglevel);
342         }
343
344     }
345 }