Skip to content

Commit

Permalink
Updated ablyrest and realtime instances to use internal logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Aug 10, 2023
1 parent da06e52 commit ba1e9e9
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/IO.Ably.Android/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ internal class Platform : IPlatform

public IMobileDevice MobileDevice { get; set; }

public void RegisterOsNetworkStateChanged()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (_lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down
4 changes: 2 additions & 2 deletions src/IO.Ably.NETFramework/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ internal static void Initialize()
HookedUpToNetworkEvents = false;
}

public void RegisterOsNetworkStateChanged()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (_lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down
4 changes: 2 additions & 2 deletions src/IO.Ably.NETStandard20/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ internal static void Initialize()
HookedUpToNetworkEvents = false;
}

public void RegisterOsNetworkStateChanged()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (Lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down
12 changes: 4 additions & 8 deletions src/IO.Ably.Shared/AblyRealtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AblyRealtime : IRealtimeClient, IDisposable
{
private SynchronizationContext _synchronizationContext;

internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance;
internal ILogger Logger { get; set; }

internal RealtimeWorkflow Workflow { get; private set; }

Expand Down Expand Up @@ -51,11 +51,7 @@ internal AblyRealtime(ClientOptions options, IMobileDevice mobileDevice)

internal AblyRealtime(ClientOptions options, Func<ClientOptions, IMobileDevice, AblyRest> createRestFunc, IMobileDevice mobileDevice = null)
{
if (options.Logger != null)
{
Logger = options.Logger;
}

Logger = options.Logger;
Logger.LogLevel = options.LogLevel;

if (options.LogHandler != null)
Expand All @@ -67,12 +63,12 @@ internal AblyRealtime(ClientOptions options, Func<ClientOptions, IMobileDevice,
RestClient = createRestFunc != null ? createRestFunc.Invoke(options, mobileDevice) : new AblyRest(options, mobileDevice);
Push = new PushRealtime(RestClient, Logger);

Connection = new Connection(this, options.NowFunc, options.Logger);
Connection = new Connection(this, options.NowFunc);
Connection.Initialise();

if (options.AutomaticNetworkStateMonitoring)
{
IoC.RegisterOsNetworkStateChanged();
IoC.RegisterOsNetworkStateChanged(Logger);
}

Channels = new RealtimeChannels(this, Connection, mobileDevice);
Expand Down
12 changes: 4 additions & 8 deletions src/IO.Ably.Shared/AblyRest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,20 @@ public LocalDevice Device

internal ClientOptions Options { get; }

internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance;
internal ILogger Logger { get; set; }

/// <summary>Initializes the rest client and validates the passed in options.</summary>
private void InitializeAbly(IMobileDevice mobileDevice)
{
Logger = Options.Logger;
Logger.LogLevel = Options.LogLevel;

if (Options == null)
{
Logger.Error("No options provider to Ably rest");
throw new AblyException("Invalid options");
}

if (Options.Logger != null)
{
Logger = Options.Logger;
}

Logger.LogLevel = Options.LogLevel;

if (Options.LogHandler != null)
{
Logger.LoggerSink = Options.LogHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/IO.Ably.Shared/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public abstract class EventEmitter<TState, TArgs> : IEventEmitter<TState, TArgs>
{
internal EventEmitter(ILogger logger)
{
Logger = logger ?? DefaultLogger.LoggerInstance;
Logger = logger;
}

internal ILogger Logger { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/IO.Ably.Shared/IPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal interface IPlatform
/// The implementation will only allow one registration to operating system network state events even
/// thought this method can be called multiple times.
/// </summary>
void RegisterOsNetworkStateChanged();
/// <param name="logger">Logger provided by the realtime client.</param>
void RegisterOsNetworkStateChanged(ILogger logger);
}
}
2 changes: 1 addition & 1 deletion src/IO.Ably.Shared/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static IoC()

public static ITransportFactory TransportFactory => Platform?.TransportFactory ?? new MsWebSocketTransport.TransportFactory();

public static void RegisterOsNetworkStateChanged() => Platform.RegisterOsNetworkStateChanged();
public static void RegisterOsNetworkStateChanged(ILogger logger) => Platform.RegisterOsNetworkStateChanged(logger);

public static Agent.PlatformRuntime PlatformId => Platform?.PlatformId ?? Agent.PlatformRuntime.Other;

Expand Down
13 changes: 3 additions & 10 deletions src/IO.Ably.Shared/Realtime/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ public sealed class Connection : EventEmitter<ConnectionEvent, ConnectionStateCh
/// the network state.
/// </summary>
/// <param name="state">The current state of the OS network connection.</param>
public static void NotifyOperatingSystemNetworkState(NetworkState state) =>
NotifyOperatingSystemNetworkState(state, null);

/// <param name="logger">Logger provided by the realtime client.</param>
internal static void NotifyOperatingSystemNetworkState(NetworkState state, ILogger logger)
{
if (logger == null)
{
logger = DefaultLogger.LoggerInstance;
}

if (logger.IsDebug)
{
logger.Debug("OS Network connection state: " + state);
Expand Down Expand Up @@ -119,8 +112,8 @@ private void CleanUpNetworkStateEvents()
|| State == Realtime.ConnectionState.Disconnected)
&& RealtimeClient.Options.QueueMessages);

internal Connection(AblyRealtime realtimeClient, Func<DateTimeOffset> nowFunc, ILogger logger = null)
: base(logger)
internal Connection(AblyRealtime realtimeClient, Func<DateTimeOffset> nowFunc)
: base(realtimeClient.Logger)
{
Now = nowFunc;
RealtimeClient = realtimeClient;
Expand Down
4 changes: 2 additions & 2 deletions src/IO.Ably.iOS/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ internal class Platform : IPlatform
public ITransportFactory TransportFactory => null;
public IMobileDevice MobileDevice { get; set; }

public void RegisterOsNetworkStateChanged()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (_lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down

0 comments on commit ba1e9e9

Please sign in to comment.