Implement configurable logging.
This commit is contained in:
parent
856fc63627
commit
a0f719b8df
2 changed files with 114 additions and 39 deletions
|
@ -13,7 +13,7 @@ namespace ConsoleInterface
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
private static ILoggerFactory _loggerFactory;
|
private static ILoggerFactory _loggerFactory;
|
||||||
public static readonly ILogger Logger = NullLogger.Instance;
|
public static ILogger Logger = NullLogger.Instance;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The console interface for the project and the main entrypoint.
|
/// The console interface for the project and the main entrypoint.
|
||||||
|
@ -22,28 +22,23 @@ namespace ConsoleInterface
|
||||||
// ReSharper disable once UnusedMember.Local
|
// ReSharper disable once UnusedMember.Local
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Parser.Default.ParseArguments<Options>(args).WithParsed(RunOptions);
|
Parser.Default.ParseArguments<ProgramOptions>(args).WithParsed(RunOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// RunOptions will be called after the command-line arguments were successfully parsed.
|
/// RunOptions will be called after the command-line arguments were successfully parsed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static void RunOptions(Options options)
|
private static void RunOptions(ProgramOptions options)
|
||||||
{
|
{
|
||||||
_loggerFactory = LoggerFactory.Create(b => b.AddConsole());
|
SetupLogging(options.LogLevel);
|
||||||
TaskExecutor.Logger = _loggerFactory.CreateLogger(nameof(TaskExecutor));
|
|
||||||
LocalSystemFilesRetriever.Logger = _loggerFactory.CreateLogger(nameof(LocalSystemFilesRetriever));
|
|
||||||
KeepFilenameFormatter.Logger =
|
|
||||||
_loggerFactory.CreateLogger(nameof(KeepFilenameFormatter));
|
|
||||||
|
|
||||||
CreateDestinationDirectory(options.DestinationDirectory);
|
CreateDestinationDirectory(options.DestinationDirectory);
|
||||||
var outputFormatter = KeepFilenameFormatter.Create(options.DestinationDirectory);
|
var outputFormatter = SimpleOutputSink.Create(options.DestinationDirectory);
|
||||||
var executor = TaskExecutor.Create(new TaskExecutorOptions
|
var executor = TaskExecutor.Create(new TaskExecutorOptions
|
||||||
{
|
{
|
||||||
EnableCompression = options.CompressFiles is true,
|
EnableCompression = options.CompressFiles is true,
|
||||||
FileOutputFormatter = outputFormatter
|
OutputSink = outputFormatter
|
||||||
});
|
});
|
||||||
var filesRetriever = LocalSystemFilesRetriever.Create();
|
var filesRetriever = LocalFileBrowser.Create();
|
||||||
|
|
||||||
|
|
||||||
executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory));
|
executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory));
|
||||||
|
@ -55,36 +50,77 @@ namespace ConsoleInterface
|
||||||
/// <param name="destinationDirectory">The destination directory.</param>
|
/// <param name="destinationDirectory">The destination directory.</param>
|
||||||
private static void CreateDestinationDirectory(string destinationDirectory)
|
private static void CreateDestinationDirectory(string destinationDirectory)
|
||||||
{
|
{
|
||||||
if (FileSystemHelpers.Logger == null)
|
|
||||||
{
|
|
||||||
FileSystemHelpers.Logger = _loggerFactory.CreateLogger(nameof(FileSystemHelpers));
|
|
||||||
}
|
|
||||||
FileSystemHelpers.CreateDestinationDirectory(destinationDirectory);
|
FileSystemHelpers.CreateDestinationDirectory(destinationDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public static void SetupLogging(string logLevel)
|
||||||
/// Options is a class defining command line options supported by this program.
|
|
||||||
/// </summary>
|
|
||||||
public class Options
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
_loggerFactory = LoggerFactory.Create(b =>
|
||||||
/// CompressFiles indicates whether files should be compressed after being cleaned.
|
{
|
||||||
/// </summary>
|
b.AddConsole();
|
||||||
[Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)]
|
var logLevelToBeSet = LogLevel.Information;
|
||||||
public bool? CompressFiles { get; set; }
|
switch (logLevel.ToLower())
|
||||||
|
{
|
||||||
|
case "trace":
|
||||||
|
case "t":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.Trace;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "debug":
|
||||||
|
case "d":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.Debug;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "information":
|
||||||
|
case "i":
|
||||||
|
case "info":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.Information;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "warning":
|
||||||
|
case "warn":
|
||||||
|
case "w":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.Warning;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "error":
|
||||||
|
case "err":
|
||||||
|
case "e":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.Error;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "critical":
|
||||||
|
case "crt":
|
||||||
|
case "c":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.Critical;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "none":
|
||||||
|
case "n":
|
||||||
|
{
|
||||||
|
logLevelToBeSet = LogLevel.None;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
b.SetMinimumLevel(logLevelToBeSet);
|
||||||
/// DestinationDirectory represents the destination directory for the cleaned images.
|
});
|
||||||
/// </summary>
|
Logger = _loggerFactory.CreateLogger(nameof(Program));
|
||||||
[Option('d', "dest", Required = false, HelpText = "The destination directory for the cleaned images.",
|
// Tasks
|
||||||
Default = "./cleaned")]
|
TaskExecutor.Logger = _loggerFactory.CreateLogger(nameof(TaskExecutor));
|
||||||
public string DestinationDirectory { get; set; }
|
// File Retriever
|
||||||
|
LocalFileBrowser.Logger = _loggerFactory.CreateLogger(nameof(LocalFileBrowser));
|
||||||
/// <summary>
|
// FileName formatter
|
||||||
/// SourceDirectory represents the source directory of images.
|
SimpleOutputSink.Logger =
|
||||||
/// </summary>
|
_loggerFactory.CreateLogger(nameof(SimpleOutputSink));
|
||||||
[Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")]
|
FileSystemHelpers.Logger = _loggerFactory.CreateLogger(nameof(FileSystemHelpers));
|
||||||
public string SourceDirectory { get; set; }
|
Logger.LogTrace("SetupLogging - exit");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
39
ConsoleInterface/ProgramOptions.cs
Normal file
39
ConsoleInterface/ProgramOptions.cs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
using CommandLine;
|
||||||
|
|
||||||
|
namespace ConsoleInterface
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// ProgramOptions is a class defining command line options supported by this program.
|
||||||
|
/// </summary>
|
||||||
|
public class ProgramOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// CompressFiles indicates whether files should be compressed after being cleaned.
|
||||||
|
/// </summary>
|
||||||
|
[Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)]
|
||||||
|
public bool? CompressFiles { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DestinationDirectory represents the destination directory for the cleaned images.
|
||||||
|
/// </summary>
|
||||||
|
[Option('d', "destination", Required = false,
|
||||||
|
HelpText = "The destination directory for the cleaned images.",
|
||||||
|
Default = "./cleaned")]
|
||||||
|
public string DestinationDirectory { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DestinationDirectory represents the destination directory for the cleaned images.
|
||||||
|
/// </summary>
|
||||||
|
[Option('l', "log-level", Required = false,
|
||||||
|
HelpText =
|
||||||
|
"The logging level of the program. Available log levels are: None,Trace,Debug,Information,Warning,Error,Critical.",
|
||||||
|
Default = "Information")]
|
||||||
|
public string LogLevel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SourceDirectory represents the source directory of images.
|
||||||
|
/// </summary>
|
||||||
|
[Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")]
|
||||||
|
public string SourceDirectory { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue