#region Disclaimer / License // Copyright (C) 2015, The Duplicati Team // http://www.duplicati.com, info@duplicati.com // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; namespace Duplicati.Library.Logging { /// /// The different types of messages /// public enum LogMessageType { /// /// The message is a profiling message /// Profiling, /// /// The message is informative but does not indicate problems /// Information, /// /// The message is a warning, meaning that later errors may be releated to this message /// Warning, /// /// The message indicates an error /// Error, } /// /// This static class is used to write log messages /// public static class Log { /// /// The key used to assign the current scope into the current call-context /// private const string LOGICAL_CONTEXT_KEY = "Duplicati:LoggingEntry"; /// /// The root scope /// private static readonly LogScope m_root = new LogScope(null, LogMessageType.Error, null); /// /// The stored log instances /// private static readonly Dictionary m_log_instances = new Dictionary(); /// /// Static lock object to provide thread safe logging /// private static object m_lock = new object(); /// /// Flag used to block recursion /// private static bool m_is_active = false; /// /// Gets the lock instance used to protect the logging calls /// public static object Lock { get { return m_lock; } } /// /// Writes a message to the current log destination /// /// The message to write /// The type of the message public static void WriteMessage(string message, LogMessageType type) { WriteMessage(message, type, null); } /// /// Writes a message to the current log destination /// /// The message to write /// The type of the message /// An exception value public static void WriteMessage(string message, LogMessageType type, Exception ex) { lock (m_lock) { if (m_is_active) return; try { m_is_active = true; var cs = CurrentScope; while (cs != null) { if (cs.Log != null && type >= cs.LogLevel) cs.Log.WriteMessage(message, type, ex); cs = cs.Parent; } } finally { m_is_active = false; } } } /// /// Gets or sets the level of log messages to write to the CurrentLog /// public static LogMessageType LogLevel { get { var cs = CurrentScope; var ll = (int)cs.LogLevel; while (cs != null) { ll = Math.Min(ll, (int)cs.LogLevel); cs = cs.Parent; } return (LogMessageType)ll; } set { CurrentScope.LogLevel = value; } } /// /// Gets or sets the current log destination /// public static ILog CurrentLog { get { return CurrentScope.Log; } set { CurrentScope.Log = value; } } /// /// Starts a new scope, that can be closed by disposing the returned instance /// /// The new scope. public static IDisposable StartScope() { return StartScope((ILog)null); } /// /// Starts a new scope, that can be stopped by disposing the returned instance /// /// The new scope. public static IDisposable StartScope(ILog log) { return new LogScope(log, LogLevel, CurrentScope); } /// /// Starts the scope. /// /// The scope to start. internal static void StartScope(LogScope scope) { CurrentScope = scope; } /// /// Closes the scope. /// /// The scope to finish. internal static void CloseScope(LogScope scope) { lock(m_lock) { if (CurrentScope == scope && scope != m_root) CurrentScope = scope.Parent; m_log_instances.Remove(scope.InstanceID); } } /// /// Gets or sets the current log destination in a call-context aware fashion /// internal static LogScope CurrentScope { get { lock (m_lock) { var cur = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(LOGICAL_CONTEXT_KEY) as string; if (cur == null || cur == m_root.InstanceID) return m_root; LogScope sc; if (!m_log_instances.TryGetValue(cur, out sc)) throw new Exception(string.Format("Unable to find log in lookup table, this may be caused by attempting to transport call contexts between AppDomains (eg. with remoting calls)")); return sc; } } private set { lock (m_lock) { if (value != null) { m_log_instances[value.InstanceID] = value; System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(LOGICAL_CONTEXT_KEY, value.InstanceID); } else { System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(LOGICAL_CONTEXT_KEY, null); } } } } } }