Skip to content

Commit

Permalink
Replace Microsoft.Extensions.Logging with custom ILogger
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Sep 27, 2023
1 parent f81b855 commit 09d57c4
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 435 deletions.
2 changes: 0 additions & 2 deletions AdvancedSharpAdbClient/AdbCommandLineClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public virtual async Task<Version> GetVersionAsync(CancellationToken cancellatio
if (version < AdbServer.RequiredAdbVersion)
{
AdbException ex = new($"Required minimum version of adb: {AdbServer.RequiredAdbVersion}. Current version is {version}");
#if HAS_LOGGER
logger.LogError(ex, ex.Message);
#endif
throw ex;
}

Expand Down
21 changes: 3 additions & 18 deletions AdvancedSharpAdbClient/AdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// </copyright>

using AdvancedSharpAdbClient.Exceptions;
using AdvancedSharpAdbClient.Logs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -22,27 +23,18 @@ public partial class AdbCommandLineClient : IAdbCommandLineClient
/// </summary>
protected const string AdbVersionPattern = "^.*(\\d+)\\.(\\d+)\\.(\\d+)$";

#if HAS_LOGGER
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<AdbCommandLineClient> logger;
#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, ILogger<AdbCommandLineClient> logger = null)
{
if (adbPath.IsNullOrWhiteSpace())
{
Expand Down Expand Up @@ -77,13 +69,8 @@ public AdbCommandLineClient(string adbPath, bool isForce = false
this.EnsureIsValidAdbFile(adbPath);

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

/// <summary>
/// Gets the path to the <c>adb.exe</c> executable.
Expand All @@ -107,9 +94,7 @@ public virtual Version GetVersion()
if (version < AdbServer.RequiredAdbVersion)
{
AdbException ex = new($"Required minimum version of adb: {AdbServer.RequiredAdbVersion}. Current version is {version}");
#if HAS_LOGGER
logger.LogError(ex, ex.Message);
#endif
throw ex;
}

Expand Down
16 changes: 0 additions & 16 deletions AdvancedSharpAdbClient/AdbSocket.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@ public virtual async Task SendAsync(byte[] data, int offset, int length, Cancell
throw new AdbException("channel EOF");
}
}
#if HAS_LOGGER
catch (SocketException ex)
{
logger.LogError(ex, ex.Message);
#else
catch (SocketException)
{
#endif
throw;
}
}
Expand Down Expand Up @@ -115,16 +110,12 @@ public virtual async Task<int> ReadAsync(byte[] data, int length, CancellationTo

if (count < 0)
{
#if HAS_LOGGER
logger.LogError("read: channel EOF");
#endif
throw new AdbException("EOF");
}
else if (count == 0)
{
#if HAS_LOGGER
logger.LogInformation("DONE with Read");
#endif
}
else
{
Expand Down Expand Up @@ -244,14 +235,9 @@ protected virtual async Task<bool> WriteAsync(byte[] data, CancellationToken can
{
await SendAsync(data, -1, cancellationToken);
}
#if HAS_LOGGER
catch (IOException e)
{
logger.LogError(e, e.Message);
#else
catch (IOException)
{
#endif
return false;
}

Expand All @@ -278,9 +264,7 @@ protected virtual async Task<AdbResponse> ReadAdbResponseInnerAsync(Cancellation
{
string message = await ReadStringAsync(cancellationToken);
rasps.Message = message;
#if HAS_LOGGER
logger.LogError($"Got reply '{ReplyToString(reply)}', diag='{rasps.Message}'");
#endif
}

return rasps;
Expand Down
59 changes: 8 additions & 51 deletions AdvancedSharpAdbClient/AdbSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,22 @@ public partial class AdbSocket : IAdbSocket
/// </summary>
protected readonly ITcpSocket socket;

#if HAS_LOGGER
/// <summary>
/// The logger to use when logging messages.
/// </summary>
protected readonly ILogger<AdbSocket> logger;
#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, ILogger<AdbSocket> logger = null)
{
socket = new TcpSocket();
socket.Connect(endPoint);
socket.ReceiveBufferSize = ReceiveBufferSize;
#if HAS_LOGGER
this.logger = logger ?? NullLogger<AdbSocket>.Instance;
#endif
this.logger = logger ?? LoggerProvider.CreateLogger<AdbSocket>();
}

/// <summary>
Expand All @@ -66,11 +55,7 @@ public AdbSocket(EndPoint endPoint
/// <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, ILogger<AdbSocket> logger = null)
{
if (string.IsNullOrEmpty(host))
{
Expand All @@ -86,24 +71,18 @@ public AdbSocket(string host, int port
socket = new TcpSocket();
socket.Connect(endPoint);
socket.ReceiveBufferSize = ReceiveBufferSize;
#if HAS_LOGGER
this.logger = logger ?? NullLogger<AdbSocket>.Instance;
#endif
this.logger = logger ?? LoggerProvider.CreateLogger<AdbSocket>();
}
#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)
/// <param name="logger">The logger to use when logging.</param>
public AdbSocket(ITcpSocket socket, ILogger<AdbSocket> logger = null)
{
this.socket = socket;
#if HAS_LOGGER
logger ??= NullLogger<AdbSocket>.Instance;
#endif
this.logger = logger ?? LoggerProvider.CreateLogger<AdbSocket>();
}

/// <summary>
Expand Down Expand Up @@ -143,14 +122,9 @@ public virtual void Send(byte[] data, int offset, int length)
throw new AdbException("channel EOF");
}
}
#if HAS_LOGGER
catch (SocketException sex)
{
logger.LogError(sex, sex.Message);
#else
catch (SocketException)
{
#endif
throw;
}
}
Expand Down Expand Up @@ -222,16 +196,12 @@ public virtual int Read(byte[] data, int length)
count = socket.Receive(buffer, bufferLength, SocketFlags.None);
if (count < 0)
{
#if HAS_LOGGER
logger.LogError("read: channel EOF");
#endif
throw new AdbException("EOF");
}
else if (count == 0)
{
#if HAS_LOGGER
logger.LogInformation("DONE with Read");
#endif
}
else
{
Expand Down Expand Up @@ -364,14 +334,9 @@ protected virtual bool Write(byte[] data)
{
Send(data, -1);
}
#if HAS_LOGGER
catch (IOException e)
{
logger.LogError(e, e.Message);
#else
catch (IOException)
{
#endif
return false;
}

Expand All @@ -397,9 +362,7 @@ protected virtual AdbResponse ReadAdbResponseInner()
{
string message = ReadString();
rasps.Message = message;
#if HAS_LOGGER
logger.LogError($"Got reply '{ReplyToString(reply)}', diag='{rasps.Message}'");
#endif
logger.LogError("Got reply '{0}', diag='{1}'", ReplyToString(reply), rasps.Message);
}

return rasps;
Expand All @@ -417,17 +380,11 @@ protected virtual string ReplyToString(byte[] reply)
{
result = Encoding.ASCII.GetString(reply);
}
#if HAS_LOGGER
catch (DecoderFallbackException e)
{
logger.LogError(e, e.Message);
#else
catch (DecoderFallbackException)
{
#endif
result = string.Empty;
}

return result;
}

Expand Down
Loading

0 comments on commit 09d57c4

Please sign in to comment.