Skip to content

Commit

Permalink
Remove internal 'LogDefault()'.
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j committed Jul 11, 2024
1 parent db579ee commit cf65350
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Realm/Realm/Handles/SharedRealmHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ private static void OnDisposeGCHandle(IntPtr handle)
[MonoPInvokeCallback(typeof(NativeMethods.LogMessageCallback))]
private static void LogMessage(LogLevel level, StringValue categoryName, StringValue message)
{
RealmLogger.CoreLogDefault(level, LogCategory.FromName(categoryName!), message!);
RealmLogger.Default.LogAnyLevel(level, LogCategory.FromName(categoryName!), message!);
}

[MonoPInvokeCallback(typeof(NativeMethods.MigrationCallback))]
Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm/Helpers/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static T ValidateNotNull<T>(T value, string paramName)

public static void AssertDebug(string message)
{
RealmLogger.LogDefault(LogLevel.Error, $"{message} {OpenIssueText}");
RealmLogger.Default.Log(LogLevel.Error, $"{message} {OpenIssueText}");

#if DEBUG
throw new Exception(message);
Expand Down
8 changes: 1 addition & 7 deletions Realm/Realm/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@ protected RealmLogger()
_gcHandle = new Lazy<GCHandle>(() => GCHandle.Alloc(this));
}

internal static void LogDefault(LogLevel level, string message) => Default?.Log(level, message);

internal static void LogDefault(LogLevel level, LogCategory category, string message) => Default?.Log(level, category, message);

internal static void CoreLogDefault(LogLevel level, LogCategory category, string message) => Default?.LogAnyLevel(level, category, message);

/// <summary>
/// Log a message at the supplied level and default category <see cref="LogCategory.RealmLogCategory.SDK"/>.
/// </summary>
Expand Down Expand Up @@ -206,7 +200,7 @@ public void Log(LogLevel level, LogCategory category, string message)
/// Log a message without calling into Core to check the current level. Logs from
/// Core should always call this API as they already check the level prior to notifying.
/// </summary>
private void LogAnyLevel(LogLevel level, LogCategory category, string message)
internal void LogAnyLevel(LogLevel level, LogCategory category, string message)
{
try
{
Expand Down
6 changes: 3 additions & 3 deletions Realm/Realm/Native/NativeCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void CleanupNativeResources(string reason)
{
if (Interlocked.CompareExchange(ref _isInitialized, 0, 1) == 1)
{
RealmLogger.LogDefault(LogLevel.Info, $"Realm: Force closing all native instances: {reason}");
RealmLogger.Default.Log(LogLevel.Info, $"Realm: Force closing all native instances: {reason}");

var sw = new Stopwatch();
sw.Start();
Expand All @@ -106,12 +106,12 @@ public static void CleanupNativeResources(string reason)
SharedRealmHandle.ForceCloseNativeRealms();

sw.Stop();
RealmLogger.LogDefault(LogLevel.Info, $"Realm: Closed all native instances in {sw.ElapsedMilliseconds} ms.");
RealmLogger.Default.Log(LogLevel.Info, $"Realm: Closed all native instances in {sw.ElapsedMilliseconds} ms.");
}
}
catch (Exception ex)
{
RealmLogger.LogDefault(LogLevel.Error, $"Realm: Failed to close all native instances. You may need to restart your app. Error: {ex}");
RealmLogger.Default.Log(LogLevel.Error, $"Realm: Failed to close all native instances. You may need to restart your app. Error: {ex}");
}
}

Expand Down
18 changes: 9 additions & 9 deletions Realm/Realm/Native/SyncSocketProvider.EventLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private class Timer

internal Timer(TimeSpan delay, IntPtr nativeCallback, ChannelWriter<IWork> workQueue)
{
RealmLogger.LogDefault(LogLevel.Trace, $"Creating timer with delay {delay} and target {nativeCallback}.");
RealmLogger.Default.Log(LogLevel.Trace, $"Creating timer with delay {delay} and target {nativeCallback}.");
var cancellationToken = _cts.Token;
Task.Delay(delay, cancellationToken).ContinueWith(async _ =>
{
Expand All @@ -42,7 +42,7 @@ internal Timer(TimeSpan delay, IntPtr nativeCallback, ChannelWriter<IWork> workQ

internal void Cancel()
{
RealmLogger.LogDefault(LogLevel.Trace, $"Canceling timer.");
RealmLogger.Default.Log(LogLevel.Trace, $"Canceling timer.");
_cts.Cancel();
_cts.Dispose();
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public void Execute()
{
if (cancellationToken.IsCancellationRequested)
{
RealmLogger.LogDefault(LogLevel.Trace, "Deleting EventLoopWork callback only because event loop was cancelled.");
RealmLogger.Default.Log(LogLevel.Trace, "Deleting EventLoopWork callback only because event loop was cancelled.");
NativeMethods.delete_callback(nativeCallback);
return;
}
Expand All @@ -83,21 +83,21 @@ public void Execute()

private static void RunCallback(IntPtr nativeCallback, Status status)
{
RealmLogger.LogDefault(LogLevel.Trace, $"SyncSocketProvider running native callback {nativeCallback} with status {status.Code} \"{status.Reason}\".");
RealmLogger.Default.Log(LogLevel.Trace, $"SyncSocketProvider running native callback {nativeCallback} with status {status.Code} \"{status.Reason}\".");

using var arena = new Arena();
NativeMethods.run_callback(nativeCallback, status.Code, StringValue.AllocateFrom(status.Reason, arena));
}

private async Task PostWorkAsync(IntPtr nativeCallback)
{
RealmLogger.LogDefault(LogLevel.Trace, "Posting work to SyncSocketProvider event loop.");
RealmLogger.Default.Log(LogLevel.Trace, "Posting work to SyncSocketProvider event loop.");
await _workQueue.Writer.WriteAsync(new EventLoopWork(nativeCallback, _cts.Token));
}

private async partial Task WorkThread()
{
RealmLogger.LogDefault(LogLevel.Trace, "Starting SyncSocketProvider event loop.");
RealmLogger.Default.Log(LogLevel.Trace, "Starting SyncSocketProvider event loop.");
try
{
while (await _workQueue.Reader.WaitToReadAsync())
Expand All @@ -110,15 +110,15 @@ private async partial Task WorkThread()
}
catch (Exception e)
{
RealmLogger.LogDefault(LogLevel.Error, $"Error occurred in SyncSocketProvider event loop {e.GetType().FullName}: {e.Message}");
RealmLogger.Default.Log(LogLevel.Error, $"Error occurred in SyncSocketProvider event loop {e.GetType().FullName}: {e.Message}");
if (!string.IsNullOrEmpty(e.StackTrace))
{
RealmLogger.LogDefault(LogLevel.Trace, e.StackTrace);
RealmLogger.Default.Log(LogLevel.Trace, e.StackTrace);
}

throw;
}

RealmLogger.LogDefault(LogLevel.Trace, "Exiting SyncSocketProvider event loop.");
RealmLogger.Default.Log(LogLevel.Trace, "Exiting SyncSocketProvider event loop.");
}
}
16 changes: 8 additions & 8 deletions Realm/Realm/Native/SyncSocketProvider.WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private class Socket : IDisposable

internal Socket(ClientWebSocket webSocket, IntPtr observer, ChannelWriter<IWork> workQueue, Uri uri)
{
RealmLogger.LogDefault(LogLevel.Trace, $"Creating a WebSocket to {uri.GetLeftPart(UriPartial.Path)}");
RealmLogger.Default.Log(LogLevel.Trace, $"Creating a WebSocket to {uri.GetLeftPart(UriPartial.Path)}");
_webSocket = webSocket;
_observer = observer;
_workQueue = workQueue;
Expand All @@ -56,7 +56,7 @@ internal Socket(ClientWebSocket webSocket, IntPtr observer, ChannelWriter<IWork>

private async Task ReadThread()
{
RealmLogger.LogDefault(LogLevel.Trace, "Entering WebSocket event loop.");
RealmLogger.Default.Log(LogLevel.Trace, "Entering WebSocket event loop.");

try
{
Expand All @@ -67,7 +67,7 @@ private async Task ReadThread()
{
var builder = new StringBuilder();
FormatExceptionForLogging(e, builder);
RealmLogger.LogDefault(LogLevel.Error, $"Error establishing WebSocket connection {builder}");
RealmLogger.Default.Log(LogLevel.Error, $"Error establishing WebSocket connection {builder}");

await _workQueue.WriteAsync(new WebSocketClosedWork(false, (WebSocketCloseStatus)RLM_ERR_WEBSOCKET_CONNECTION_FAILED, e.Message, _observer, _cancellationToken));
return;
Expand All @@ -92,19 +92,19 @@ private async Task ReadThread()

break;
case WebSocketMessageType.Close:
RealmLogger.LogDefault(LogLevel.Trace, $"WebSocket closed with status {result.CloseStatus!.Value} and description \"{result.CloseStatusDescription}\"");
RealmLogger.Default.Log(LogLevel.Trace, $"WebSocket closed with status {result.CloseStatus!.Value} and description \"{result.CloseStatusDescription}\"");
await _workQueue.WriteAsync(new WebSocketClosedWork(clean: true, result.CloseStatus!.Value, result.CloseStatusDescription!, _observer, _cancellationToken));
break;
default:
RealmLogger.LogDefault(LogLevel.Trace, $"Received unexpected text WebSocket message: {Encoding.UTF8.GetString(buffer, 0, result.Count)}");
RealmLogger.Default.Log(LogLevel.Trace, $"Received unexpected text WebSocket message: {Encoding.UTF8.GetString(buffer, 0, result.Count)}");
break;
}
}
catch (Exception e)
{
var builder = new StringBuilder();
FormatExceptionForLogging(e, builder);
RealmLogger.LogDefault(LogLevel.Error, $"Error reading from WebSocket {builder}");
RealmLogger.Default.Log(LogLevel.Error, $"Error reading from WebSocket {builder}");

await _workQueue.WriteAsync(new WebSocketClosedWork(false, (WebSocketCloseStatus)RLM_ERR_WEBSOCKET_READ_ERROR, e.Message, _observer, _cancellationToken));
return;
Expand All @@ -130,7 +130,7 @@ public async void Write(BinaryValue data, IntPtr native_callback)
{
var builder = new StringBuilder();
FormatExceptionForLogging(e, builder);
RealmLogger.LogDefault(LogLevel.Error, $"Error writing to WebSocket {builder}");
RealmLogger.Default.Log(LogLevel.Error, $"Error writing to WebSocket {builder}");

// in case of errors notify the websocket observer and just dispose the callback
await _workQueue.WriteAsync(new WebSocketClosedWork(false, (WebSocketCloseStatus)RLM_ERR_WEBSOCKET_WRITE_ERROR, e.Message, _observer, _cancellationToken));
Expand Down Expand Up @@ -164,7 +164,7 @@ public async void Dispose()
_webSocket.Dispose();
_receiveBuffer.Dispose();
_cts.Dispose();
RealmLogger.LogDefault(LogLevel.Trace, "Disposing WebSocket.");
RealmLogger.Default.Log(LogLevel.Trace, "Disposing WebSocket.");

try
{
Expand Down
4 changes: 2 additions & 2 deletions Realm/Realm/Native/SyncSocketProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private interface IWork

internal SyncSocketProvider(Action<ClientWebSocketOptions>? onWebSocketConnection)
{
RealmLogger.LogDefault(LogLevel.Debug, "Creating SyncSocketProvider.");
RealmLogger.Default.Log(LogLevel.Debug, "Creating SyncSocketProvider.");
_onWebSocketConnection = onWebSocketConnection;
_workQueue = Channel.CreateUnbounded<IWork>(new() { SingleReader = true });
_workThread = Task.Run(WorkThread);
Expand All @@ -166,7 +166,7 @@ internal SyncSocketProvider(Action<ClientWebSocketOptions>? onWebSocketConnectio

public void Dispose()
{
RealmLogger.LogDefault(LogLevel.Debug, "Destroying SyncSocketProvider.");
RealmLogger.Default.Log(LogLevel.Debug, "Destroying SyncSocketProvider.");
_workQueue.Writer.Complete();
_cts.Cancel();
_cts.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm/Realm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ internal void NotifyError(Exception ex)
{
if (Error is null)
{
RealmLogger.LogDefault(LogLevel.Error, "A realm-level exception has occurred. To handle and react to those, subscribe to the Realm.Error event.");
RealmLogger.Default.Log(LogLevel.Error, "A realm-level exception has occurred. To handle and react to those, subscribe to the Realm.Error event.");
}

Error?.Invoke(this, new ErrorEventArgs(ex));
Expand Down
28 changes: 14 additions & 14 deletions Tests/Realm.Tests/Database/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void Logger_CanSetDefaultLoggerToBuiltInLogger()
var messages = new List<string>();
RealmLogger.Default = RealmLogger.Function(message => messages.Add(message));

RealmLogger.LogDefault(LogLevel.Warn, LogCategory.Realm.SDK, "This is very dangerous!");
RealmLogger.Default.Log(LogLevel.Warn, LogCategory.Realm.SDK, "This is very dangerous!");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, LogCategory.Realm.SDK, "This is very dangerous!");
Expand All @@ -95,7 +95,7 @@ public void Logger_CanSetDefaultLoggerToUserDefinedLogger()
var messages = new List<string>();
RealmLogger.Default = new UserDefinedLogger((level, category, message) => messages.Add(RealmLogger.FormatLog(level, category, message)));

RealmLogger.LogDefault(LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
RealmLogger.Default.Log(LogLevel.Warn, LogCategory.Realm.SDK, "A log message");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
Expand All @@ -108,7 +108,7 @@ public void ObsoleteLogger_CanSetDefaultLoggerToUserDefinedLogger()
var messages = new List<string>();
Logger.Default = new ObsoleteUserDefinedLogger((level, message) => messages.Add(Logger.FormatLog(level, LogCategory.Realm.SDK, message)));

Logger.LogDefault(LogLevel.Warn, "A log message");
Logger.Default.Log(LogLevel.Warn, "A log message");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
Expand All @@ -120,7 +120,7 @@ public void Logger_SkipsDebugMessagesByDefault()
var messages = new List<string>();
RealmLogger.Default = RealmLogger.Function(message => messages.Add(message));

RealmLogger.LogDefault(LogLevel.Debug, "This is a debug message!");
RealmLogger.Default.Log(LogLevel.Debug, "This is a debug message!");

Assert.That(messages.Count, Is.EqualTo(0));
}
Expand Down Expand Up @@ -182,9 +182,9 @@ public void Logger_WhenLevelIsSet_LogsOnlyExpectedLevels(LogLevel level)
RealmLogger.Default = RealmLogger.Function(message => messages.Add(message));
RealmLogger.SetLogLevel(level, category);

RealmLogger.LogDefault(level - 1, category, "This is at level - 1");
RealmLogger.LogDefault(level, category, "This is at the same level");
RealmLogger.LogDefault(level + 1, category, "This is at level + 1");
RealmLogger.Default.Log(level - 1, category, "This is at level - 1");
RealmLogger.Default.Log(level, category, "This is at the same level");
RealmLogger.Default.Log(level + 1, category, "This is at level + 1");

Assert.That(messages.Count, Is.EqualTo(2));
AssertLogMessageContains(messages[0], level, category, "This is at the same level");
Expand All @@ -201,7 +201,7 @@ public void Logger_LogsAtGivenCategory()
var messages = new List<string>();
RealmLogger.Default = RealmLogger.Function((message) => messages.Add(message));

RealmLogger.LogDefault(LogLevel.Warn, category, "A log message");
RealmLogger.Default.Log(LogLevel.Warn, category, "A log message");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, category, "A log message");
Expand All @@ -214,7 +214,7 @@ public void Logger_LogsSdkCategoryByDefault()
var messages = new List<string>();
RealmLogger.Default = RealmLogger.Function((message) => messages.Add(message));

RealmLogger.LogDefault(LogLevel.Warn, "A log message");
RealmLogger.Default.Log(LogLevel.Warn, "A log message");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
Expand All @@ -226,7 +226,7 @@ public void Logger_CallsCustomFunction()
var messages = new List<string>();
RealmLogger.Default = RealmLogger.Function((level, category, message) => messages.Add(RealmLogger.FormatLog(level, category, message)));

RealmLogger.LogDefault(LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
RealmLogger.Default.Log(LogLevel.Warn, LogCategory.Realm.SDK, "A log message");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
Expand All @@ -239,7 +239,7 @@ public void Logger_CallsObsoleteCustomFunction()
var messages = new List<string>();
RealmLogger.Default = RealmLogger.Function((level, message) => messages.Add(RealmLogger.FormatLog(level, LogCategory.Realm.SDK, message)));

RealmLogger.LogDefault(LogLevel.Warn, "A log message");
RealmLogger.Default.Log(LogLevel.Warn, "A log message");

Assert.That(messages.Count, Is.EqualTo(1));
AssertLogMessageContains(messages[0], LogLevel.Warn, LogCategory.Realm.SDK, "A log message");
Expand Down Expand Up @@ -283,9 +283,9 @@ public void FileLogger()
var errorMessage = "This is an error!";
var timeString = DateTimeOffset.UtcNow.ToString("yyyy-MM-dd");

RealmLogger.LogDefault(LogLevel.Warn, warnMessage);
RealmLogger.LogDefault(LogLevel.Debug, debugMessage);
RealmLogger.LogDefault(LogLevel.Error, errorMessage);
RealmLogger.Default.Log(LogLevel.Warn, warnMessage);
RealmLogger.Default.Log(LogLevel.Debug, debugMessage);
RealmLogger.Default.Log(LogLevel.Error, errorMessage);

var loggedStrings = File.ReadAllLines(tempFilePath);

Expand Down

0 comments on commit cf65350

Please sign in to comment.