From 155f2e35699faafc55d22507707fcb2882c3942e Mon Sep 17 00:00:00 2001 From: wherewhere Date: Mon, 14 Aug 2023 22:16:49 +0800 Subject: [PATCH] Logger should create by LoggerProvider now --- .../AdbCommandLineClient.cs | 18 +-------- AdvancedSharpAdbClient/AdbSocket.cs | 38 +++---------------- .../DeviceCommands/Models/AndroidProcess.cs | 5 +++ .../DeviceCommands/PackageManager.cs | 19 +--------- AdvancedSharpAdbClient/DeviceMonitor.cs | 18 +-------- .../Extensions/LoggerProvider.cs | 36 ++++++++++++++++++ .../Receivers/ConsoleOutputReceiver.cs | 20 +--------- 7 files changed, 54 insertions(+), 100 deletions(-) create mode 100644 AdvancedSharpAdbClient/Extensions/LoggerProvider.cs diff --git a/AdvancedSharpAdbClient/AdbCommandLineClient.cs b/AdvancedSharpAdbClient/AdbCommandLineClient.cs index 8cd435c8..719c70e2 100644 --- a/AdvancedSharpAdbClient/AdbCommandLineClient.cs +++ b/AdvancedSharpAdbClient/AdbCommandLineClient.cs @@ -26,23 +26,15 @@ public partial class AdbCommandLineClient : IAdbCommandLineClient /// /// The logger to use when logging messages. /// - protected readonly ILogger logger; + private readonly ILogger logger = LoggerProvider.CreateLogger(); #endif -#if !HAS_LOGGER -#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Initializes a new instance of the class. /// /// The path to the adb.exe executable. /// Don't check adb file name when . - /// The logger to use when logging. - public AdbCommandLineClient(string adbPath, bool isForce = false -#if HAS_LOGGER - , ILogger logger = null -#endif - ) + public AdbCommandLineClient(string adbPath, bool isForce = false) { if (adbPath.IsNullOrWhiteSpace()) { @@ -77,13 +69,7 @@ public AdbCommandLineClient(string adbPath, bool isForce = false this.EnsureIsValidAdbFile(adbPath); AdbPath = adbPath; -#if HAS_LOGGER - this.logger = logger ?? NullLogger.Instance; -#endif } -#if !HAS_LOGGER -#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Gets the path to the adb.exe executable. diff --git a/AdvancedSharpAdbClient/AdbSocket.cs b/AdvancedSharpAdbClient/AdbSocket.cs index e8430f9e..e2879fc7 100644 --- a/AdvancedSharpAdbClient/AdbSocket.cs +++ b/AdvancedSharpAdbClient/AdbSocket.cs @@ -35,29 +35,18 @@ public partial class AdbSocket : IAdbSocket /// /// The logger to use when logging messages. /// - protected readonly ILogger logger; + private readonly ILogger logger = LoggerProvider.CreateLogger(); #endif -#if !HAS_LOGGER -#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Initializes a new instance of the class. /// /// The at which the Android Debug Bridge is listening for clients. - /// The logger to use when logging. - public AdbSocket(EndPoint endPoint -#if HAS_LOGGER - , ILogger logger = null -#endif - ) + public AdbSocket(EndPoint endPoint) { socket = new TcpSocket(); socket.Connect(endPoint); socket.ReceiveBufferSize = ReceiveBufferSize; -#if HAS_LOGGER - this.logger = logger ?? NullLogger.Instance; -#endif } /// @@ -65,12 +54,7 @@ public AdbSocket(EndPoint endPoint /// /// The host address at which the Android Debug Bridge is listening for clients. /// The port at which the Android Debug Bridge is listening for clients. - /// The logger to use when logging. - public AdbSocket(string host, int port -#if HAS_LOGGER - , ILogger logger = null -#endif - ) + public AdbSocket(string host, int port) { if (string.IsNullOrEmpty(host)) { @@ -82,29 +66,17 @@ public AdbSocket(string host, int port DnsEndPoint endPoint = values.Length <= 0 ? throw new ArgumentNullException(nameof(host)) : new DnsEndPoint(values[0], values.Length > 1 && int.TryParse(values[1], out int _port) ? _port : port); - + socket = new TcpSocket(); socket.Connect(endPoint); socket.ReceiveBufferSize = ReceiveBufferSize; -#if HAS_LOGGER - this.logger = logger ?? NullLogger.Instance; -#endif } -#if !HAS_LOGGER -#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Initializes a new instance of the class. /// /// The at which the Android Debug Bridge is listening for clients. - public AdbSocket(ITcpSocket socket) - { - this.socket = socket; -#if HAS_LOGGER - logger ??= NullLogger.Instance; -#endif - } + public AdbSocket(ITcpSocket socket) => this.socket = socket; /// /// Gets or sets the size of the receive buffer diff --git a/AdvancedSharpAdbClient/DeviceCommands/Models/AndroidProcess.cs b/AdvancedSharpAdbClient/DeviceCommands/Models/AndroidProcess.cs index c7dcc21d..06707716 100644 --- a/AdvancedSharpAdbClient/DeviceCommands/Models/AndroidProcess.cs +++ b/AdvancedSharpAdbClient/DeviceCommands/Models/AndroidProcess.cs @@ -313,6 +313,11 @@ public class AndroidProcess /// public int ExitCode { get; set; } + /// + /// Initializes a new instance of the class. + /// + public AndroidProcess() { } + /// /// Creates a from it representation. /// diff --git a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs index 9f56ba00..6a477e71 100644 --- a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs +++ b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs @@ -35,7 +35,7 @@ public partial class PackageManager /// /// The logger to use when logging messages. /// - protected readonly ILogger logger; + protected readonly ILogger logger = LoggerProvider.CreateLogger(); #endif /// @@ -55,9 +55,6 @@ public partial class PackageManager /// public event EventHandler InstallProgressChanged; -#if !HAS_LOGGER -#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Initializes a new instance of the class. /// @@ -70,12 +67,7 @@ public partial class PackageManager /// that can be used to transfer files to and from a given device. /// A value indicating whether to skip the initial refresh of the package list or not. /// Used mainly by unit tests. - /// The logger to use when logging. - public PackageManager(IAdbClient client, DeviceData device, bool thirdPartyOnly = false, Func syncServiceFactory = null, bool skipInit = false -#if HAS_LOGGER - , ILogger logger = null -#endif - ) + public PackageManager(IAdbClient client, DeviceData device, bool thirdPartyOnly = false, Func syncServiceFactory = null, bool skipInit = false) { Device = device ?? throw new ArgumentNullException(nameof(device)); Packages = new Dictionary(); @@ -88,14 +80,7 @@ public PackageManager(IAdbClient client, DeviceData device, bool thirdPartyOnly { RefreshPackages(); } - -#if HAS_LOGGER - this.logger = logger ?? NullLogger.Instance; -#endif } -#if !HAS_LOGGER -#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Gets a value indicating whether this package manager only lists third party applications, diff --git a/AdvancedSharpAdbClient/DeviceMonitor.cs b/AdvancedSharpAdbClient/DeviceMonitor.cs index f7ae7ac3..92b32ef9 100644 --- a/AdvancedSharpAdbClient/DeviceMonitor.cs +++ b/AdvancedSharpAdbClient/DeviceMonitor.cs @@ -39,7 +39,7 @@ public partial class DeviceMonitor : IDeviceMonitor /// /// The logger to use when logging messages. /// - protected readonly ILogger logger; + protected readonly ILogger logger = LoggerProvider.CreateLogger(); #endif /// @@ -66,30 +66,16 @@ public partial class DeviceMonitor : IDeviceMonitor protected Thread monitorThread; #endif -#if !HAS_LOGGER -#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Initializes a new instance of the class. /// /// The that manages the connection with the adb server. - /// The logger to use when logging. - public DeviceMonitor(IAdbSocket socket -#if HAS_LOGGER - , ILogger logger = null -#endif - ) + public DeviceMonitor(IAdbSocket socket ) { Socket = socket ?? throw new ArgumentNullException(nameof(socket)); devices = new List(); Devices = devices.AsReadOnly(); -#if HAS_LOGGER - this.logger = logger ?? NullLogger.Instance; -#endif } -#if !HAS_LOGGER -#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// public event EventHandler DeviceChanged; diff --git a/AdvancedSharpAdbClient/Extensions/LoggerProvider.cs b/AdvancedSharpAdbClient/Extensions/LoggerProvider.cs new file mode 100644 index 00000000..f91f40b6 --- /dev/null +++ b/AdvancedSharpAdbClient/Extensions/LoggerProvider.cs @@ -0,0 +1,36 @@ +#if HAS_LOGGER +// +// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved. +// + +namespace AdvancedSharpAdbClient +{ + /// + /// Provides a mechanism for creating instances of and classes. + /// + public static class LoggerProvider + { + private static ILoggerFactory _loggerFactory = null; + + /// + /// Sets the current log provider based on logger factory. + /// + /// The logger factory. + public static void SetLogProvider(ILoggerFactory loggerFactory) => _loggerFactory = loggerFactory; + + /// + /// Creates a new instance. + /// + /// The category name for messages produced by the logger. + /// A new instance. + public static ILogger CreateLogger(string category) => _loggerFactory == null ? NullLogger.Instance : _loggerFactory.CreateLogger(category); + + /// + /// Creates a new instance using the full name of the given type. + /// + /// The type. + /// The that was created + public static ILogger CreateLogger() => _loggerFactory == null ? NullLogger.Instance : _loggerFactory.CreateLogger(); + } +} +#endif \ No newline at end of file diff --git a/AdvancedSharpAdbClient/Receivers/ConsoleOutputReceiver.cs b/AdvancedSharpAdbClient/Receivers/ConsoleOutputReceiver.cs index 5ad3702f..ac7d79d0 100644 --- a/AdvancedSharpAdbClient/Receivers/ConsoleOutputReceiver.cs +++ b/AdvancedSharpAdbClient/Receivers/ConsoleOutputReceiver.cs @@ -25,7 +25,7 @@ public partial class ConsoleOutputReceiver : MultiLineReceiver /// /// The logger to use when logging messages. /// - protected readonly ILogger logger; + private readonly ILogger logger = LoggerProvider.CreateLogger(); #endif /// @@ -33,26 +33,10 @@ public partial class ConsoleOutputReceiver : MultiLineReceiver /// protected readonly StringBuilder output = new(); -#if !HAS_LOGGER -#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif /// /// Initializes a new instance of the class. /// - /// The logger to use when logging. - public ConsoleOutputReceiver( -#if HAS_LOGGER - ILogger logger = null -#endif - ) - { -#if HAS_LOGGER - this.logger = logger ?? NullLogger.Instance; -#endif - } -#if !HAS_LOGGER -#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数 -#endif + public ConsoleOutputReceiver() { } /// /// Gets a that represents the current .