2024-03-04 12:21:53 +01:00
// Copyright (C) 2024, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
2009-01-14 17:29:30 +00:00
//
2024-03-04 12:21:53 +01:00
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
2009-01-14 17:29:30 +00:00
//
2024-03-04 12:21:53 +01:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2009-01-14 17:29:30 +00:00
//
2024-03-04 12:21:53 +01:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2024-02-28 15:45:30 +01:00
2009-01-14 17:29:30 +00:00
using System ;
using System.Collections.Generic ;
using System.Text ;
namespace Duplicati.Library.Logging
{
/// <summary>
/// The different types of messages
/// </summary>
public enum LogMessageType
{
2018-03-12 14:07:11 +01:00
/// <summary>
/// The message should only be shown if it is explicitly requested
/// </summary>
ExplicitOnly ,
2009-01-14 17:29:30 +00:00
/// <summary>
/// The message is a profiling message
/// </summary>
Profiling ,
/// <summary>
2019-11-30 11:35:43 -08:00
/// Messages that are normally not wanted for display
2018-03-12 14:07:11 +01:00
/// </summary>
Verbose ,
/// <summary>
/// The message is from a retry
/// </summary>
Retry ,
/// <summary>
2009-01-14 17:29:30 +00:00
/// The message is informative but does not indicate problems
/// </summary>
Information ,
/// <summary>
2018-03-12 14:07:11 +01:00
/// The message is from dry-run output
/// </summary>
DryRun ,
/// <summary>
2019-11-30 11:35:43 -08:00
/// The message is a warning, meaning that later errors may be related to this message
2009-01-14 17:29:30 +00:00
/// </summary>
Warning ,
/// <summary>
/// The message indicates an error
/// </summary>
Error ,
}
/// <summary>
/// This static class is used to write log messages
/// </summary>
public static class Log
{
/// <summary>
2017-01-09 23:27:56 +01:00
/// The key used to assign the current scope into the current call-context
2009-01-14 17:29:30 +00:00
/// </summary>
2017-01-09 23:27:56 +01:00
private const string LOGICAL_CONTEXT_KEY = "Duplicati:LoggingEntry" ;
/// <summary>
/// The root scope
/// </summary>
2018-04-11 23:02:47 +02:00
private static readonly LogScope m_root = new LogScope ( null , new LogTagFilter ( LogMessageType . Error , null , null ), null , true );
2017-01-09 23:27:56 +01:00
/// <summary>
/// The stored log instances
/// </summary>
private static readonly Dictionary < string , LogScope > m_log_instances = new Dictionary < string , LogScope >();
2009-01-14 17:29:30 +00:00
/// <summary>
2017-01-09 23:27:56 +01:00
/// Static lock object to provide thread safe logging
2009-01-14 17:29:30 +00:00
/// </summary>
2018-03-17 19:59:23 -07:00
private static readonly object m_lock = new object ();
2017-01-09 23:27:56 +01:00
2009-01-14 17:29:30 +00:00
/// <summary>
2018-03-12 14:07:11 +01:00
/// Gets the lock instance used to protect the logging calls
2009-01-14 17:29:30 +00:00
/// </summary>
2018-03-12 14:07:11 +01:00
public static object Lock { get { return m_lock ; } }
2017-01-11 22:41:01 +01:00
/// <summary>
2018-08-23 11:35:36 -03:00
/// Gets a log tag that reflects the type
2017-01-11 22:41:01 +01:00
/// </summary>
2018-03-12 14:07:11 +01:00
/// <returns>The log-tag for the type.</returns>
/// <typeparam name="T">The type to get the tag for.</typeparam>
public static string LogTagFromType < T >()
{
return LogTagFromType ( typeof ( T ));
}
2009-01-14 17:29:30 +00:00
/// <summary>
2018-08-23 11:35:36 -03:00
/// Gets a log tag that reflects the type
2018-03-12 14:07:11 +01:00
/// </summary>
/// <returns>The log-tag for the type.</returns>
/// <param name="t">The type to get the tag for.</param>
public static string LogTagFromType ( Type t )
{
return t . Namespace + "." + t . Name ;
}
/// <summary>
/// Gets a common list of items to include in the help-url
/// </summary>
/// <returns>The basic help url items.</returns>
/// <param name="fromCommandLine">A flag indicating if the link is for commandline</param>
private static List < string > GetBasicHelpUrlItems ( bool fromCommandLine )
{
var items = new List < string >();
items . Add ( "version=" + System . Uri . EscapeDataString ( typeof ( Log ). Assembly . GetName (). Version . ToString ()));
items . Add ( "cli=" + ( fromCommandLine ? "t" : "f" ));
2019-11-30 11:35:43 -08:00
// TODO: Add OS type? mono version? install id? app-name?
2018-03-12 14:07:11 +01:00
return items ;
}
/// <summary>
/// Encodes a list of query string values and appends them to the help url
/// </summary>
/// <returns>The help URL.</returns>
/// <param name="items">The items to include.</param>
private static string EncodeHelpUrl ( IEnumerable < string > items )
{
var query = string . Join ( "&" , items );
if (! string . IsNullOrWhiteSpace ( query ))
query = "?" + query ;
return "https://help.duplicati.com/" + query ;
}
/// <summary>
/// Creates a link to look up further information on a particular message
/// </summary>
/// <returns>The help link.</returns>
/// <param name="id">The id to use.</param>
/// <param name="fromCommandLine">A flag indicating if the link is for commandline</param>
public static string CreateHelpLink ( string id , bool fromCommandLine )
{
var items = GetBasicHelpUrlItems ( fromCommandLine );
if (! string . IsNullOrWhiteSpace ( id ))
items . Add ( "id=" + System . Uri . EscapeDataString ( id ));
return EncodeHelpUrl ( items );
}
/// <summary>
/// Creates a link to look up further information on a particular message
/// </summary>
/// <returns>The help link.</returns>
/// <param name="tag">The log tag, if any.</param>
/// <param name="messageid">The message id, if any.</param>
/// <param name="exceptionid">The exception id, if any.</param>
/// <param name="fromCommandLine">A flag indicating if the link is for commandline</param>
public static string CreateHelpLink ( string tag , string messageid , string exceptionid , bool fromCommandLine )
{
var items = GetBasicHelpUrlItems ( fromCommandLine );
if (! string . IsNullOrWhiteSpace ( tag ))
items . Add ( "tag=" + System . Uri . EscapeDataString ( tag ));
if (! string . IsNullOrWhiteSpace ( messageid ))
items . Add ( "msgid=" + System . Uri . EscapeDataString ( messageid ));
if (! string . IsNullOrWhiteSpace ( exceptionid ))
items . Add ( "exid=" + System . Uri . EscapeDataString ( exceptionid ));
return EncodeHelpUrl ( items );
}
/// <summary>
/// Writes an explicit message to the current log destination
2009-01-14 17:29:30 +00:00
/// </summary>
/// <param name="message">The message to write</param>
2018-03-12 14:07:11 +01:00
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteExplicitMessage ( string tag , string id , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . ExplicitOnly , tag , id , null , message , arguments );
}
/// <summary>
/// Writes an explicit message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="ex">The exception to log</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteExplicitMessage ( string tag , string id , Exception ex , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . ExplicitOnly , tag , id , ex , message , arguments );
}
/// <summary>
/// Writes a verbose message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteVerboseMessage ( string tag , string id , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Verbose , tag , id , null , message , arguments );
}
2018-05-09 10:44:32 +02:00
/// <summary>
/// Writes a verbose message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="ex">The exception to log</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteVerboseMessage ( string tag , string id , Exception ex , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Verbose , tag , id , ex , message , arguments );
}
2018-03-12 14:07:11 +01:00
/// <summary>
/// Writes a profiling message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteProfilingMessage ( string tag , string id , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Profiling , tag , id , null , message , arguments );
}
/// <summary>
/// Writes a dry-run message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteDryrunMessage ( string tag , string id , string message , params object [] arguments )
2009-01-14 17:29:30 +00:00
{
2018-03-12 14:07:11 +01:00
WriteMessage ( LogMessageType . DryRun , tag , id , null , message , arguments );
}
/// <summary>
/// Writes a retry message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="ex">The exception to attach</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteRetryMessage ( string tag , string id , Exception ex , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Retry , tag , id , ex , message , arguments );
}
/// <summary>
/// Writes an information message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteInformationMessage ( string tag , string id , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Information , tag , id , null , message , arguments );
}
/// <summary>
/// Writes a warning message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
/// <param name="ex">The exception to attach</param>
/// <param name="arguments">The message format arguments</param>
public static void WriteWarningMessage ( string tag , string id , Exception ex , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Warning , tag , id , ex , message , arguments );
}
/// <summary>
/// Writes an error message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
2018-04-09 14:28:30 +02:00
/// <param name="ex">The exception to attach</param>
2018-03-12 14:07:11 +01:00
/// <param name="arguments">The message format arguments</param>
public static void WriteErrorMessage ( string tag , string id , Exception ex , string message , params object [] arguments )
{
WriteMessage ( LogMessageType . Error , tag , id , ex , message , arguments );
2009-01-14 17:29:30 +00:00
}
/// <summary>
/// Writes a message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="type">The type of the message</param>
2018-03-12 14:07:11 +01:00
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
public static void WriteMessage ( LogMessageType type , string tag , string id , string message , params object [] arguments )
2009-01-14 17:29:30 +00:00
{
2018-03-12 14:07:11 +01:00
WriteMessage ( type , tag , id , null , message , arguments );
2009-01-14 17:29:30 +00:00
}
/// <summary>
/// Writes a message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="type">The type of the message</param>
/// <param name="ex">An exception value</param>
2018-03-12 14:07:11 +01:00
/// <param name="tag">The tag-type for this message</param>
/// <param name="id">The message id</param>
2018-04-09 14:28:30 +02:00
/// <param name="arguments">The arguments to format the log message with</param>
2018-03-12 14:07:11 +01:00
public static void WriteMessage ( LogMessageType type , string tag , string id , Exception ex , string message , params object [] arguments )
2009-01-14 17:29:30 +00:00
{
2018-04-09 14:28:30 +02:00
var msg = new LogEntry ( message , arguments , type , tag , id , ex );
2018-03-12 14:07:11 +01:00
2009-01-14 17:29:30 +00:00
lock ( m_lock )
2017-01-09 23:27:56 +01:00
{
var cs = CurrentScope ;
2018-04-11 23:02:47 +02:00
while ( cs != null && ! cs . IsolatingScope )
2017-01-09 23:27:56 +01:00
{
2018-03-12 14:07:11 +01:00
cs . WriteMessage ( msg );
2017-01-09 23:27:56 +01:00
cs = cs . Parent ;
}
2009-01-14 17:29:30 +00:00
}
}
/// <summary>
2018-04-11 23:02:47 +02:00
/// Starts a new scope, that can be closed by disposing the returned instance
2009-01-14 17:29:30 +00:00
/// </summary>
2018-08-07 13:05:09 +02:00
/// <param name="detached">Flag indicating if the scope should be detached from the parent</param>
2018-04-11 23:02:47 +02:00
/// <returns>The new scope.</returns>
2018-08-07 13:05:09 +02:00
public static IDisposable StartIsolatingScope ( bool detached )
2009-01-14 17:29:30 +00:00
{
2018-08-07 13:05:09 +02:00
lock ( m_lock )
{
2019-01-21 19:30:52 -08:00
var scope = StartScope ( null , null , true );
2018-08-07 13:05:09 +02:00
if ( detached )
DetachCurrentScope ( scope );
return scope ;
}
}
/// <summary>
/// Detaches the current scope, such that new scopes do not chain onto this
/// </summary>
/// <param name="scope">The current scope.</param>
public static IDisposable DetachCurrentScope ( IDisposable scope )
{
lock ( m_lock )
{
if ( CurrentScope == scope && scope != null && CurrentScope . Parent != null )
CurrentScope = CurrentScope . Parent ;
}
return scope ;
2017-01-09 23:27:56 +01:00
}
/// <summary>
/// Starts a new scope, that can be closed by disposing the returned instance
/// </summary>
/// <returns>The new scope.</returns>
public static IDisposable StartScope ()
{
2018-03-12 14:07:11 +01:00
return StartScope (( ILogDestination ) null , null );
2009-01-14 17:29:30 +00:00
}
/// <summary>
2018-03-12 14:07:11 +01:00
/// Starts a new scope, that can be stopped by disposing the returned instance
2009-01-14 17:29:30 +00:00
/// </summary>
2018-03-12 14:07:11 +01:00
/// <param name="log">The log target</param>
/// <param name="level">The log level</param>
/// <returns>The new scope.</returns>
public static IDisposable StartScope ( ILogDestination log , LogMessageType level )
2009-01-14 17:29:30 +00:00
{
2018-03-12 14:07:11 +01:00
return StartScope ( log , new LogTagFilter ( level , null , null ));
2017-01-09 23:27:56 +01:00
}
/// <summary>
2018-03-12 14:07:11 +01:00
/// Starts a new scope, that can be stopped by disposing the returned instance
2017-01-09 23:27:56 +01:00
/// </summary>
2018-03-12 14:07:11 +01:00
/// <param name="log">The log target</param>
/// <param name="filter">The log filter</param>
2017-01-09 23:27:56 +01:00
/// <returns>The new scope.</returns>
2018-04-11 23:02:47 +02:00
public static IDisposable StartScope ( ILogDestination log , ILogFilter filter = null , bool isolating = false )
2017-01-09 23:27:56 +01:00
{
2018-04-11 23:02:47 +02:00
return new LogScope ( log , filter , CurrentScope , isolating );
2017-01-09 23:27:56 +01:00
}
/// <summary>
/// Starts a new scope, that can be stopped by disposing the returned instance
/// </summary>
2018-03-12 14:07:11 +01:00
/// <param name="log">The log target</param>
/// <param name="filter">The log filter</param>
2017-01-09 23:27:56 +01:00
/// <returns>The new scope.</returns>
2018-03-12 14:07:11 +01:00
public static IDisposable StartScope ( Action < LogEntry > log , Func < LogEntry , bool > filter = null )
2017-01-09 23:27:56 +01:00
{
2018-04-11 23:02:47 +02:00
return new LogScope ( new FunctionLogDestination ( log ), filter == null ? null : new FunctionFilter ( filter ), CurrentScope , false );
2017-01-09 23:27:56 +01:00
}
/// <summary>
/// Starts the scope.
/// </summary>
/// <param name="scope">The scope to start.</param>
internal static void StartScope ( LogScope scope )
{
CurrentScope = scope ;
}
/// <summary>
/// Closes the scope.
/// </summary>
/// <param name="scope">The scope to finish.</param>
internal static void CloseScope ( LogScope scope )
{
lock ( m_lock )
{
if ( CurrentScope == scope && scope != m_root )
CurrentScope = scope . Parent ;
m_log_instances . Remove ( scope . InstanceID );
}
}
/// <summary>
/// Gets or sets the current log destination in a call-context aware fashion
/// </summary>
internal static LogScope CurrentScope
{
get
2009-01-14 17:29:30 +00:00
{
lock ( m_lock )
2017-01-09 23:27:56 +01:00
{
2024-03-01 14:30:28 +01:00
var cur = CallContext . GetData ( LOGICAL_CONTEXT_KEY ) as string ;
2017-01-09 23:27:56 +01:00
if ( cur == null || cur == m_root . InstanceID )
return m_root ;
LogScope sc ;
if (! m_log_instances . TryGetValue ( cur , out sc ))
2018-05-24 20:16:46 -07:00
throw new Exception ( "Unable to find log in lookup table, this may be caused by attempting to transport call contexts between AppDomains (eg. with remoting calls)" );
2017-01-09 23:27:56 +01:00
return sc ;
}
}
private set
{
lock ( m_lock )
{
if ( value != null )
{
m_log_instances [ value . InstanceID ] = value ;
2024-03-01 14:30:28 +01:00
CallContext . SetData ( LOGICAL_CONTEXT_KEY , value . InstanceID );
2017-01-09 23:27:56 +01:00
}
else
{
2024-03-01 14:30:28 +01:00
CallContext . SetData ( LOGICAL_CONTEXT_KEY , null );
2017-01-09 23:27:56 +01:00
}
}
2009-01-14 17:29:30 +00:00
}
}
}
}