/* * Copyright (c) 2007-2008, openmetaverse.org * All rights reserved. * * - Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * - Neither the name of the openmetaverse.org nor the names * of its contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ using System; using log4net; using log4net.Config; [assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace OpenMetaverse { /// /// Singleton logging class for the entire library /// public static class Logger { /// /// Callback used for client apps to receive log messages from /// the library /// /// Data being logged /// The severity of the log entry from public delegate void LogCallback(object message, Helpers.LogLevel level); /// Triggered whenever a message is logged. If this is left /// null, log messages will go to the console public static event LogCallback OnLogMessage; /// log4net logging engine public static ILog LogInstance; /// /// Default constructor /// static Logger() { LogInstance = LogManager.GetLogger("OpenMetaverse"); // If error level reporting isn't enabled we assume no logger is configured and initialize a default // ConsoleAppender if (!LogInstance.Logger.IsEnabledFor(log4net.Core.Level.Error)) { log4net.Appender.ConsoleAppender appender = new log4net.Appender.ConsoleAppender(); appender.Layout = new log4net.Layout.PatternLayout("%timestamp [%thread] %-5level - %message%newline"); BasicConfigurator.Configure(appender); if(Settings.LOG_LEVEL != Helpers.LogLevel.None) LogInstance.Info("No log configuration found, defaulting to console logging"); } } /// /// Send a log message to the logging engine /// /// The log message /// The severity of the log entry public static void Log(object message, Helpers.LogLevel level) { Log(message, level, null, null); } /// /// Send a log message to the logging engine /// /// The log message /// The severity of the log entry /// Instance of the client public static void Log(object message, Helpers.LogLevel level, GridClient client) { Log(message, level, client, null); } /// /// Send a log message to the logging engine /// /// The log message /// The severity of the log entry /// Exception that was raised public static void Log(object message, Helpers.LogLevel level, Exception exception) { Log(message, level, null, exception); } /// /// Send a log message to the logging engine /// /// The log message /// The severity of the log entry /// Instance of the client /// Exception that was raised public static void Log(object message, Helpers.LogLevel level, GridClient client, Exception exception) { if (client != null && client.Settings.LOG_NAMES) message = String.Format("<{0}>: {1}", client.Self.Name, message); if (OnLogMessage != null) OnLogMessage(message, level); switch (level) { case Helpers.LogLevel.Debug: if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug) LogInstance.Debug(message, exception); break; case Helpers.LogLevel.Info: if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug || Settings.LOG_LEVEL == Helpers.LogLevel.Info) LogInstance.Info(message, exception); break; case Helpers.LogLevel.Warning: if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug || Settings.LOG_LEVEL == Helpers.LogLevel.Info || Settings.LOG_LEVEL == Helpers.LogLevel.Warning) LogInstance.Warn(message, exception); break; case Helpers.LogLevel.Error: if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug || Settings.LOG_LEVEL == Helpers.LogLevel.Info || Settings.LOG_LEVEL == Helpers.LogLevel.Warning || Settings.LOG_LEVEL == Helpers.LogLevel.Error) LogInstance.Error(message, exception); break; default: break; } } /// /// If the library is compiled with DEBUG defined, an event will be /// fired if an OnLogMessage handler is registered and the /// message will be sent to the logging engine /// /// The message to log at the DEBUG level to the /// current logging engine public static void DebugLog(object message) { DebugLog(message, null); } /// /// If the library is compiled with DEBUG defined and /// GridClient.Settings.DEBUG is true, an event will be /// fired if an OnLogMessage handler is registered and the /// message will be sent to the logging engine /// /// The message to log at the DEBUG level to the /// current logging engine /// Instance of the client [System.Diagnostics.Conditional("DEBUG")] public static void DebugLog(object message, GridClient client) { if (Settings.LOG_LEVEL == Helpers.LogLevel.Debug) { if (client != null && client.Settings.LOG_NAMES) message = String.Format("<{0}>: {1}", client.Self.Name, message); if (OnLogMessage != null) OnLogMessage(message, Helpers.LogLevel.Debug); LogInstance.Debug(message); } } } }