Skip to content

Commit

Permalink
Logger should create by LoggerProvider now
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Aug 14, 2023
1 parent 258564b commit 155f2e3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 100 deletions.
18 changes: 2 additions & 16 deletions AdvancedSharpAdbClient/AdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,15 @@ public partial class AdbCommandLineClient : IAdbCommandLineClient
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<AdbCommandLineClient> logger;
private readonly ILogger<AdbCommandLineClient> logger = LoggerProvider.CreateLogger<AdbCommandLineClient>();
#endif

#if !HAS_LOGGER
#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif
/// <summary>
/// Initializes a new instance of the <see cref="AdbCommandLineClient"/> class.
/// </summary>
/// <param name="adbPath">The path to the <c>adb.exe</c> executable.</param>
/// <param name="isForce">Don't check adb file name when <see langword="true"/>.</param>
/// <param name="logger">The logger to use when logging.</param>
public AdbCommandLineClient(string adbPath, bool isForce = false
#if HAS_LOGGER
, ILogger<AdbCommandLineClient> logger = null
#endif
)
public AdbCommandLineClient(string adbPath, bool isForce = false)
{
if (adbPath.IsNullOrWhiteSpace())
{
Expand Down Expand Up @@ -77,13 +69,7 @@ public AdbCommandLineClient(string adbPath, bool isForce = false
this.EnsureIsValidAdbFile(adbPath);

AdbPath = adbPath;
#if HAS_LOGGER
this.logger = logger ?? NullLogger<AdbCommandLineClient>.Instance;
#endif
}
#if !HAS_LOGGER
#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif

/// <summary>
/// Gets the path to the <c>adb.exe</c> executable.
Expand Down
38 changes: 5 additions & 33 deletions AdvancedSharpAdbClient/AdbSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,26 @@ public partial class AdbSocket : IAdbSocket
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<AdbSocket> logger;
private readonly ILogger<AdbSocket> logger = LoggerProvider.CreateLogger<AdbSocket>();
#endif

#if !HAS_LOGGER
#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif
/// <summary>
/// Initializes a new instance of the <see cref="AdbSocket"/> class.
/// </summary>
/// <param name="endPoint">The <see cref="EndPoint"/> at which the Android Debug Bridge is listening for clients.</param>
/// <param name="logger">The logger to use when logging.</param>
public AdbSocket(EndPoint endPoint
#if HAS_LOGGER
, ILogger<AdbSocket> logger = null
#endif
)
public AdbSocket(EndPoint endPoint)
{
socket = new TcpSocket();
socket.Connect(endPoint);
socket.ReceiveBufferSize = ReceiveBufferSize;
#if HAS_LOGGER
this.logger = logger ?? NullLogger<AdbSocket>.Instance;
#endif
}

/// <summary>
/// Initializes a new instance of the <see cref="AdbSocket"/> class.
/// </summary>
/// <param name="host">The host address at which the Android Debug Bridge is listening for clients.</param>
/// <param name="port">The port at which the Android Debug Bridge is listening for clients.</param>
/// <param name="logger">The logger to use when logging.</param>
public AdbSocket(string host, int port
#if HAS_LOGGER
, ILogger<AdbSocket> logger = null
#endif
)
public AdbSocket(string host, int port)
{
if (string.IsNullOrEmpty(host))
{
Expand All @@ -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<AdbSocket>.Instance;
#endif
}
#if !HAS_LOGGER
#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif

/// <summary>
/// Initializes a new instance of the <see cref="AdbSocket"/> class.
/// </summary>
/// <param name="socket">The <see cref="ITcpSocket"/> at which the Android Debug Bridge is listening for clients.</param>
public AdbSocket(ITcpSocket socket)
{
this.socket = socket;
#if HAS_LOGGER
logger ??= NullLogger<AdbSocket>.Instance;
#endif
}
public AdbSocket(ITcpSocket socket) => this.socket = socket;

/// <summary>
/// Gets or sets the size of the receive buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ public class AndroidProcess
/// </summary>
public int ExitCode { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="AndroidProcess"/> class.
/// </summary>
public AndroidProcess() { }

/// <summary>
/// Creates a <see cref="AndroidProcess"/> from it <see cref="string"/> representation.
/// </summary>
Expand Down
19 changes: 2 additions & 17 deletions AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class PackageManager
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<PackageManager> logger;
protected readonly ILogger<PackageManager> logger = LoggerProvider.CreateLogger<PackageManager>();
#endif

/// <summary>
Expand All @@ -55,9 +55,6 @@ public partial class PackageManager
/// </summary>
public event EventHandler<InstallProgressEventArgs> InstallProgressChanged;

#if !HAS_LOGGER
#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif
/// <summary>
/// Initializes a new instance of the <see cref="PackageManager"/> class.
/// </summary>
Expand All @@ -70,12 +67,7 @@ public partial class PackageManager
/// that can be used to transfer files to and from a given device.</param>
/// <param name="skipInit">A value indicating whether to skip the initial refresh of the package list or not.
/// Used mainly by unit tests.</param>
/// <param name="logger">The logger to use when logging.</param>
public PackageManager(IAdbClient client, DeviceData device, bool thirdPartyOnly = false, Func<IAdbClient, DeviceData, ISyncService> syncServiceFactory = null, bool skipInit = false
#if HAS_LOGGER
, ILogger<PackageManager> logger = null
#endif
)
public PackageManager(IAdbClient client, DeviceData device, bool thirdPartyOnly = false, Func<IAdbClient, DeviceData, ISyncService> syncServiceFactory = null, bool skipInit = false)
{
Device = device ?? throw new ArgumentNullException(nameof(device));
Packages = new Dictionary<string, string>();
Expand All @@ -88,14 +80,7 @@ public PackageManager(IAdbClient client, DeviceData device, bool thirdPartyOnly
{
RefreshPackages();
}

#if HAS_LOGGER
this.logger = logger ?? NullLogger<PackageManager>.Instance;
#endif
}
#if !HAS_LOGGER
#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif

/// <summary>
/// Gets a value indicating whether this package manager only lists third party applications,
Expand Down
18 changes: 2 additions & 16 deletions AdvancedSharpAdbClient/DeviceMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public partial class DeviceMonitor : IDeviceMonitor
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<DeviceMonitor> logger;
protected readonly ILogger<DeviceMonitor> logger = LoggerProvider.CreateLogger<DeviceMonitor>();
#endif

/// <summary>
Expand All @@ -66,30 +66,16 @@ public partial class DeviceMonitor : IDeviceMonitor
protected Thread monitorThread;
#endif

#if !HAS_LOGGER
#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif
/// <summary>
/// Initializes a new instance of the <see cref="DeviceMonitor"/> class.
/// </summary>
/// <param name="socket">The <see cref="IAdbSocket"/> that manages the connection with the adb server.</param>
/// <param name="logger">The logger to use when logging.</param>
public DeviceMonitor(IAdbSocket socket
#if HAS_LOGGER
, ILogger<DeviceMonitor> logger = null
#endif
)
public DeviceMonitor(IAdbSocket socket )
{
Socket = socket ?? throw new ArgumentNullException(nameof(socket));
devices = new List<DeviceData>();
Devices = devices.AsReadOnly();
#if HAS_LOGGER
this.logger = logger ?? NullLogger<DeviceMonitor>.Instance;
#endif
}
#if !HAS_LOGGER
#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif

/// <inheritdoc/>
public event EventHandler<DeviceDataChangeEventArgs> DeviceChanged;
Expand Down
36 changes: 36 additions & 0 deletions AdvancedSharpAdbClient/Extensions/LoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#if HAS_LOGGER
// <copyright file="LoggerProvider.cs" company="The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere">
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved.
// </copyright>

namespace AdvancedSharpAdbClient
{
/// <summary>
/// Provides a mechanism for creating instances of <see cref="ILogger"/> and <see cref="ILogger{T}"/> classes.
/// </summary>
public static class LoggerProvider
{
private static ILoggerFactory _loggerFactory = null;

/// <summary>
/// Sets the current log provider based on logger factory.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
public static void SetLogProvider(ILoggerFactory loggerFactory) => _loggerFactory = loggerFactory;

/// <summary>
/// Creates a new <see cref="ILogger"/> instance.
/// </summary>
/// <param name="category">The category name for messages produced by the logger.</param>
/// <returns>A new <see cref="ILogger"/> instance.</returns>
public static ILogger CreateLogger(string category) => _loggerFactory == null ? NullLogger.Instance : _loggerFactory.CreateLogger(category);

/// <summary>
/// Creates a new <see cref="ILogger"/> instance using the full name of the given type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <returns>The <see cref="ILogger"/> that was created</returns>
public static ILogger<T> CreateLogger<T>() => _loggerFactory == null ? NullLogger<T>.Instance : _loggerFactory.CreateLogger<T>();
}
}
#endif
20 changes: 2 additions & 18 deletions AdvancedSharpAdbClient/Receivers/ConsoleOutputReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,18 @@ public partial class ConsoleOutputReceiver : MultiLineReceiver
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<ConsoleOutputReceiver> logger;
private readonly ILogger<ConsoleOutputReceiver> logger = LoggerProvider.CreateLogger<ConsoleOutputReceiver>();
#endif

/// <summary>
/// A <see cref="StringBuilder"/> which receives all output from the device.
/// </summary>
protected readonly StringBuilder output = new();

#if !HAS_LOGGER
#pragma warning disable CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleOutputReceiver"/> class.
/// </summary>
/// <param name="logger">The logger to use when logging.</param>
public ConsoleOutputReceiver(
#if HAS_LOGGER
ILogger<ConsoleOutputReceiver> logger = null
#endif
)
{
#if HAS_LOGGER
this.logger = logger ?? NullLogger<ConsoleOutputReceiver>.Instance;
#endif
}
#if !HAS_LOGGER
#pragma warning restore CS1572 // XML 注释中有 param 标记,但是没有该名称的参数
#endif
public ConsoleOutputReceiver() { }

/// <summary>
/// Gets a <see cref="string"/> that represents the current <see cref="ConsoleOutputReceiver"/>.
Expand Down

0 comments on commit 155f2e3

Please sign in to comment.