From a0f719b8dfc91d84f2be95285da6881731e9c831 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 12 Feb 2022 22:25:10 +0200 Subject: [PATCH] Implement configurable logging. --- ConsoleInterface/Program.cs | 114 +++++++++++++++++++---------- ConsoleInterface/ProgramOptions.cs | 39 ++++++++++ 2 files changed, 114 insertions(+), 39 deletions(-) create mode 100644 ConsoleInterface/ProgramOptions.cs diff --git a/ConsoleInterface/Program.cs b/ConsoleInterface/Program.cs index 076e9f5..32e6b95 100644 --- a/ConsoleInterface/Program.cs +++ b/ConsoleInterface/Program.cs @@ -13,8 +13,8 @@ namespace ConsoleInterface public static class Program { private static ILoggerFactory _loggerFactory; - public static readonly ILogger Logger = NullLogger.Instance; - + public static ILogger Logger = NullLogger.Instance; + /// /// The console interface for the project and the main entrypoint. /// @@ -22,69 +22,105 @@ namespace ConsoleInterface // ReSharper disable once UnusedMember.Local public static void Main(string[] args) { - Parser.Default.ParseArguments(args).WithParsed(RunOptions); + Parser.Default.ParseArguments(args).WithParsed(RunOptions); } /// /// RunOptions will be called after the command-line arguments were successfully parsed. /// - private static void RunOptions(Options options) + private static void RunOptions(ProgramOptions options) { - _loggerFactory = LoggerFactory.Create(b => b.AddConsole()); - TaskExecutor.Logger = _loggerFactory.CreateLogger(nameof(TaskExecutor)); - LocalSystemFilesRetriever.Logger = _loggerFactory.CreateLogger(nameof(LocalSystemFilesRetriever)); - KeepFilenameFormatter.Logger = - _loggerFactory.CreateLogger(nameof(KeepFilenameFormatter)); - + SetupLogging(options.LogLevel); CreateDestinationDirectory(options.DestinationDirectory); - var outputFormatter = KeepFilenameFormatter.Create(options.DestinationDirectory); + var outputFormatter = SimpleOutputSink.Create(options.DestinationDirectory); var executor = TaskExecutor.Create(new TaskExecutorOptions { EnableCompression = options.CompressFiles is true, - FileOutputFormatter = outputFormatter + OutputSink = outputFormatter }); - var filesRetriever = LocalSystemFilesRetriever.Create(); + var filesRetriever = LocalFileBrowser.Create(); executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory)); } - + /// /// Creates the directory if it doesn't exist. /// /// The destination directory. private static void CreateDestinationDirectory(string destinationDirectory) { - if (FileSystemHelpers.Logger == null) - { - FileSystemHelpers.Logger = _loggerFactory.CreateLogger(nameof(FileSystemHelpers)); - } FileSystemHelpers.CreateDestinationDirectory(destinationDirectory); } - /// - /// Options is a class defining command line options supported by this program. - /// - public class Options + public static void SetupLogging(string logLevel) { - /// - /// CompressFiles indicates whether files should be compressed after being cleaned. - /// - [Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)] - public bool? CompressFiles { get; set; } + _loggerFactory = LoggerFactory.Create(b => + { + b.AddConsole(); + var logLevelToBeSet = LogLevel.Information; + 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; + } + } - /// - /// DestinationDirectory represents the destination directory for the cleaned images. - /// - [Option('d', "dest", Required = false, HelpText = "The destination directory for the cleaned images.", - Default = "./cleaned")] - public string DestinationDirectory { get; set; } - - /// - /// SourceDirectory represents the source directory of images. - /// - [Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")] - public string SourceDirectory { get; set; } + b.SetMinimumLevel(logLevelToBeSet); + }); + Logger = _loggerFactory.CreateLogger(nameof(Program)); + // Tasks + TaskExecutor.Logger = _loggerFactory.CreateLogger(nameof(TaskExecutor)); + // File Retriever + LocalFileBrowser.Logger = _loggerFactory.CreateLogger(nameof(LocalFileBrowser)); + // FileName formatter + SimpleOutputSink.Logger = + _loggerFactory.CreateLogger(nameof(SimpleOutputSink)); + FileSystemHelpers.Logger = _loggerFactory.CreateLogger(nameof(FileSystemHelpers)); + Logger.LogTrace("SetupLogging - exit"); } } } \ No newline at end of file diff --git a/ConsoleInterface/ProgramOptions.cs b/ConsoleInterface/ProgramOptions.cs new file mode 100644 index 0000000..2d0f3f7 --- /dev/null +++ b/ConsoleInterface/ProgramOptions.cs @@ -0,0 +1,39 @@ +using CommandLine; + +namespace ConsoleInterface +{ + /// + /// ProgramOptions is a class defining command line options supported by this program. + /// + public class ProgramOptions + { + /// + /// CompressFiles indicates whether files should be compressed after being cleaned. + /// + [Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)] + public bool? CompressFiles { get; set; } + + /// + /// DestinationDirectory represents the destination directory for the cleaned images. + /// + [Option('d', "destination", Required = false, + HelpText = "The destination directory for the cleaned images.", + Default = "./cleaned")] + public string DestinationDirectory { get; set; } + + /// + /// DestinationDirectory represents the destination directory for the cleaned images. + /// + [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; } + + /// + /// SourceDirectory represents the source directory of images. + /// + [Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")] + public string SourceDirectory { get; set; } + } +} \ No newline at end of file