C#: Introduce extractor option for logging verbosity

This commit is contained in:
Tamas Vajk
2024-01-25 17:20:47 +01:00
parent bb4327294d
commit b8c8f52efc
8 changed files with 118 additions and 6 deletions

View File

@@ -2,7 +2,7 @@ using System;
namespace Semmle.Util.Logging
{
internal static class VerbosityExtensions
public static class VerbosityExtensions
{
/// <summary>
/// Whether a message with the given severity must be included
@@ -26,5 +26,34 @@ namespace Semmle.Util.Logging
throw new ArgumentOutOfRangeException(nameof(s));
}
}
public static Verbosity? ParseVerbosity(string? str, bool logThreadId)
{
if (str == null)
{
return null;
}
Verbosity? verbosity = str.ToLowerInvariant() switch
{
"off" => Verbosity.Off,
"errors" => Verbosity.Error,
"warnings" => Verbosity.Warning,
"info" or "progress" => Verbosity.Info,
"debug" or "progress+" => Verbosity.Debug,
"trace" or "progress++" => Verbosity.Trace,
"progress+++" => Verbosity.All,
_ => null
};
if (verbosity == null && str != null)
{
// We don't have a logger when this setting is parsed, so writing it to the console:
var prefix = logThreadId ? $"[{Environment.CurrentManagedThreadId:D3}] " : "";
Console.WriteLine($"{prefix}Error: Invalid verbosity level: '{str}'");
}
return verbosity;
}
}
}