corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using log4net;
29 using log4net.Config;
30  
31 [assembly: log4net.Config.XmlConfigurator(Watch = true)]
32  
33 namespace OpenMetaverse
34 {
35 /// <summary>
36 /// Singleton logging class for the entire library
37 /// </summary>
38 public static class Logger
39 {
40 /// <summary>
41 /// Callback used for client apps to receive log messages from
42 /// the library
43 /// </summary>
44 /// <param name="message">Data being logged</param>
45 /// <param name="level">The severity of the log entry from <seealso cref="Helpers.LogLevel"/></param>
46 public delegate void LogCallback(object message, Helpers.LogLevel level);
47  
48 /// <summary>Triggered whenever a message is logged. If this is left
49 /// null, log messages will go to the console</summary>
50 public static event LogCallback OnLogMessage;
51  
52 /// <summary>log4net logging engine</summary>
53 public static ILog LogInstance;
54  
55 /// <summary>
56 /// Default constructor
57 /// </summary>
58 static Logger()
59 {
60 LogInstance = LogManager.GetLogger("OpenMetaverse");
61  
62 // If error level reporting isn't enabled we assume no logger is configured and initialize a default
63 // ConsoleAppender
64 if (!LogInstance.Logger.IsEnabledFor(log4net.Core.Level.Error))
65 {
66 log4net.Appender.ConsoleAppender appender = new log4net.Appender.ConsoleAppender();
67 appender.Layout = new log4net.Layout.PatternLayout("%timestamp [%thread] %-5level - %message%newline");
68 BasicConfigurator.Configure(appender);
69  
70 if(Settings.LOG_LEVEL != Helpers.LogLevel.None)
71 LogInstance.Info("No log configuration found, defaulting to console logging");
72 }
73 }
74  
75 /// <summary>
76 /// Send a log message to the logging engine
77 /// </summary>
78 /// <param name="message">The log message</param>
79 /// <param name="level">The severity of the log entry</param>
80 public static void Log(object message, Helpers.LogLevel level)
81 {
82 Log(message, level, null, null);
83 }
84  
85 /// <summary>
86 /// Send a log message to the logging engine
87 /// </summary>
88 /// <param name="message">The log message</param>
89 /// <param name="level">The severity of the log entry</param>
90 /// <param name="client">Instance of the client</param>
91 public static void Log(object message, Helpers.LogLevel level, GridClient client)
92 {
93 Log(message, level, client, null);
94 }
95  
96 /// <summary>
97 /// Send a log message to the logging engine
98 /// </summary>
99 /// <param name="message">The log message</param>
100 /// <param name="level">The severity of the log entry</param>
101 /// <param name="exception">Exception that was raised</param>
102 public static void Log(object message, Helpers.LogLevel level, Exception exception)
103 {
104 Log(message, level, null, exception);
105 }
106  
107 /// <summary>
108 /// Send a log message to the logging engine
109 /// </summary>
110 /// <param name="message">The log message</param>
111 /// <param name="level">The severity of the log entry</param>
112 /// <param name="client">Instance of the client</param>
113 /// <param name="exception">Exception that was raised</param>
114 public static void Log(object message, Helpers.LogLevel level, GridClient client, Exception exception)
115 {
116 if (client != null && client.Settings.LOG_NAMES)
117 message = String.Format("<{0}>: {1}", client.Self.Name, message);
118  
119 if (OnLogMessage != null)
120 OnLogMessage(message, level);
121  
122 switch (level)
123 {
124 case Helpers.LogLevel.Debug:
125 if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug)
126 LogInstance.Debug(message, exception);
127 break;
128 case Helpers.LogLevel.Info:
129 if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug
130 || Settings.LOG_LEVEL == Helpers.LogLevel.Info)
131 LogInstance.Info(message, exception);
132 break;
133 case Helpers.LogLevel.Warning:
134 if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug
135 || Settings.LOG_LEVEL == Helpers.LogLevel.Info
136 || Settings.LOG_LEVEL == Helpers.LogLevel.Warning)
137 LogInstance.Warn(message, exception);
138 break;
139 case Helpers.LogLevel.Error:
140 if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug
141 || Settings.LOG_LEVEL == Helpers.LogLevel.Info
142 || Settings.LOG_LEVEL == Helpers.LogLevel.Warning
143 || Settings.LOG_LEVEL == Helpers.LogLevel.Error)
144 LogInstance.Error(message, exception);
145 break;
146 default:
147 break;
148 }
149 }
150  
151 /// <summary>
152 /// If the library is compiled with DEBUG defined, an event will be
153 /// fired if an <code>OnLogMessage</code> handler is registered and the
154 /// message will be sent to the logging engine
155 /// </summary>
156 /// <param name="message">The message to log at the DEBUG level to the
157 /// current logging engine</param>
158 public static void DebugLog(object message)
159 {
160 DebugLog(message, null);
161 }
162  
163 /// <summary>
164 /// If the library is compiled with DEBUG defined and
165 /// <code>GridClient.Settings.DEBUG</code> is true, an event will be
166 /// fired if an <code>OnLogMessage</code> handler is registered and the
167 /// message will be sent to the logging engine
168 /// </summary>
169 /// <param name="message">The message to log at the DEBUG level to the
170 /// current logging engine</param>
171 /// <param name="client">Instance of the client</param>
172 [System.Diagnostics.Conditional("DEBUG")]
173 public static void DebugLog(object message, GridClient client)
174 {
175 if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug)
176 {
177 if (client != null && client.Settings.LOG_NAMES)
178 message = String.Format("<{0}>: {1}", client.Self.Name, message);
179  
180 if (OnLogMessage != null)
181 OnLogMessage(message, Helpers.LogLevel.Debug);
182  
183 LogInstance.Debug(message);
184 }
185 }
186 }
187 }