Work on adding filters to send-mail

This commit is contained in:
Kenneth Skovhede
2018-04-02 11:20:46 +02:00
parent c60e32b459
commit bceed2ffbe
4 changed files with 85 additions and 32 deletions
+2 -32
View File
@@ -1248,37 +1248,7 @@ namespace Duplicati.Library.Main
}
}
/// <summary>
/// Helper method to support filters with either a + or - prefix
/// </summary>
/// <returns>The IFilter instance.</returns>
/// <param name="msg">The filter string to parse.</param>
private static IFilter StringToIFilter(string msg)
{
if (string.IsNullOrWhiteSpace(msg))
return new FilterExpression();
if (msg[0] == '+')
return new FilterExpression(msg.Substring(1), true);
if (msg[0] == '-')
return new FilterExpression(msg.Substring(1), false);
return new FilterExpression(msg, true);
}
/// <summary>
/// Parses a log filter string, and returns the filter instance
/// </summary>
/// <returns>The log filter.</returns>
/// <param name="value">The filter string to parse.</param>
public static IFilter ParseLogFilter(string value)
{
if (string.IsNullOrWhiteSpace(value))
return new FilterExpression();
return value
.Split(new char[] { System.IO.Path.PathSeparator, ':', ';', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(StringToIFilter)
.Aggregate(FilterExpression.Combine);
}
/// <summary>
/// Parses a log level string
@@ -1305,7 +1275,7 @@ namespace Duplicati.Library.Main
get
{
m_options.TryGetValue("log-file-log-filter", out var value);
return ParseLogFilter(value);
return Library.Utility.FilterExpression.ParseLogFilter(value);
}
}
@@ -1318,7 +1288,7 @@ namespace Duplicati.Library.Main
get
{
m_options.TryGetValue("console-log-filter", out var value);
return ParseLogFilter(value);
return Library.Utility.FilterExpression.ParseLogFilter(value);
}
}
@@ -62,6 +62,14 @@ namespace Duplicati.Library.Modules.Builtin
/// Option used to specify if reports are sent for other operations than backups
/// </summary>
private const string OPTION_SENDALL = "send-mail-any-operation";
/// <summary>
/// Option used to set the log level for mail reports
/// </summary>
private const string OPTION_LOG_LEVEL = "send-mail-log-level";
/// <summary>
/// Option used to set the log filters for mail reports
/// </summary>
private const string OPTION_LOG_FILTER = "send-mail-log-filter";
#endregion
@@ -142,6 +150,14 @@ namespace Duplicati.Library.Modules.Builtin
/// True to send all operations
/// </summary>
private bool m_sendAll;
/// <summary>
/// The log scope that should be disposed
/// </summary>
private IDisposable m_logscope;
/// <summary>
/// The log storage
/// </summary>
private Utility.FileBackedStringList m_logstorage;
#endregion
@@ -187,6 +203,10 @@ namespace Duplicati.Library.Modules.Builtin
new CommandLineArgument(OPTION_PASSWORD, CommandLineArgument.ArgumentType.String, Strings.SendMail.OptionPasswordShort, Strings.SendMail.OptionPasswordLong),
new CommandLineArgument(OPTION_SENDLEVEL, CommandLineArgument.ArgumentType.String, Strings.SendMail.OptionSendlevelShort, Strings.SendMail.OptionSendlevelLong(ParsedResultType.Success.ToString(), ParsedResultType.Warning.ToString(), ParsedResultType.Error.ToString(), ParsedResultType.Fatal.ToString(), "All"), DEFAULT_LEVEL, null, Enum.GetNames(typeof(ParsedResultType)).Union(new string [] { "All" }).ToArray()),
new CommandLineArgument(OPTION_SENDALL, CommandLineArgument.ArgumentType.Boolean, Strings.SendMail.OptionSendallShort, Strings.SendMail.OptionSendallLong),
new CommandLineArgument(OPTION_LOG_LEVEL, CommandLineArgument.ArgumentType.Enumeration, Strings.SendMail.OptionLoglevellShort, Strings.SendMail.OptionLoglevelLong, Logging.LogMessageType.Warning.ToString(), null, Enum.GetNames(typeof(Logging.LogMessageType))),
new CommandLineArgument(OPTION_LOG_FILTER, CommandLineArgument.ArgumentType.String, Strings.SendMail.OptionLogfilterShort, Strings.SendMail.OptionLogfilterLong),
});
}
}
@@ -236,6 +256,21 @@ namespace Duplicati.Library.Modules.Builtin
m_body = DEFAULT_BODY;
if (string.IsNullOrEmpty(m_from))
m_from = DEFAULT_SENDER;
m_options.TryGetValue(OPTION_LOG_FILTER, out var logfilterstring);
var filter = Utility.FilterExpression.ParseLogFilter(logfilterstring);
var logLevel =
m_logstorage = new FileBackedStringList();
m_logscope = Logging.Log.StartScope(m => m_logstorage.Add(m.AsString(true)), m => {
if (filter.Matches(m.Tag, out var result, out var match))
return result;
else if (m.Level < loglevel)
return false;
return true;
});
}
#endregion
@@ -261,6 +296,14 @@ namespace Duplicati.Library.Modules.Builtin
/// <param name="result">The result object, if this derives from an exception, the operation failed</param>
public void OnFinish(object result)
{
// Dispose the current log scope
if (m_logscope != null)
{
try { m_logscope.Dispose(); }
catch { }
m_logscope = null;
}
//If no email is supplied, then skip
if (string.IsNullOrEmpty(m_to))
return;
@@ -613,6 +613,38 @@ namespace Duplicati.Library.Utility
return res;
}
/// <summary>
/// Parses a log filter string, and returns the filter instance
/// </summary>
/// <returns>The log filter.</returns>
/// <param name="value">The filter string to parse.</param>
public static IFilter ParseLogFilter(string value)
{
if (string.IsNullOrWhiteSpace(value))
return new FilterExpression();
return value
.Split(new char[] { System.IO.Path.PathSeparator, ':', ';', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(StringToIFilter)
.Aggregate(FilterExpression.Combine);
}
/// <summary>
/// Helper method to support filters with either a + or - prefix
/// </summary>
/// <returns>The IFilter instance.</returns>
/// <param name="msg">The filter string to parse.</param>
public static IFilter StringToIFilter(string msg)
{
if (string.IsNullOrWhiteSpace(msg))
return new FilterExpression();
if (msg[0] == '+')
return new FilterExpression(msg.Substring(1), true);
if (msg[0] == '-')
return new FilterExpression(msg.Substring(1), false);
return new FilterExpression(msg, true);
}
}
}
+8
View File
@@ -687,7 +687,15 @@ namespace Duplicati.Library.Utility
return ParseBool(opt, true);
else
return false;
}
public static T ParseEnum<T>(string value, T @default)
{
foreach (string s in Enum.GetNames(typeof(T)))
if (s.Equals(value, StringComparison.OrdinalIgnoreCase))
return (T)Enum.Parse(typeof(T), s);
return @default;
}
/// <summary>