diff --git a/Realm/Realm/Handles/SharedRealmHandle.cs b/Realm/Realm/Handles/SharedRealmHandle.cs index 9b508df999..17962d92b2 100644 --- a/Realm/Realm/Handles/SharedRealmHandle.cs +++ b/Realm/Realm/Handles/SharedRealmHandle.cs @@ -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))] diff --git a/Realm/Realm/Helpers/Argument.cs b/Realm/Realm/Helpers/Argument.cs index 6afba4e8a3..bb674114b7 100644 --- a/Realm/Realm/Helpers/Argument.cs +++ b/Realm/Realm/Helpers/Argument.cs @@ -95,7 +95,7 @@ public static T ValidateNotNull(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); diff --git a/Realm/Realm/Logging/Logger.cs b/Realm/Realm/Logging/Logger.cs index 55f26155f5..da28f9a331 100644 --- a/Realm/Realm/Logging/Logger.cs +++ b/Realm/Realm/Logging/Logger.cs @@ -170,12 +170,6 @@ protected RealmLogger() _gcHandle = new Lazy(() => 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); - /// /// Log a message at the supplied level and default category . /// @@ -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. /// - private void LogAnyLevel(LogLevel level, LogCategory category, string message) + internal void LogAnyLevel(LogLevel level, LogCategory category, string message) { try { diff --git a/Realm/Realm/Native/NativeCommon.cs b/Realm/Realm/Native/NativeCommon.cs index f33b2ffaab..9dc192fc9e 100644 --- a/Realm/Realm/Native/NativeCommon.cs +++ b/Realm/Realm/Native/NativeCommon.cs @@ -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(); @@ -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}"); } } diff --git a/Realm/Realm/Native/SyncSocketProvider.EventLoop.cs b/Realm/Realm/Native/SyncSocketProvider.EventLoop.cs index bf85babd7f..d74a5f5161 100644 --- a/Realm/Realm/Native/SyncSocketProvider.EventLoop.cs +++ b/Realm/Realm/Native/SyncSocketProvider.EventLoop.cs @@ -32,7 +32,7 @@ private class Timer internal Timer(TimeSpan delay, IntPtr nativeCallback, ChannelWriter 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 _ => { @@ -42,7 +42,7 @@ internal Timer(TimeSpan delay, IntPtr nativeCallback, ChannelWriter workQ internal void Cancel() { - RealmLogger.LogDefault(LogLevel.Trace, $"Canceling timer."); + RealmLogger.Default.Log(LogLevel.Trace, $"Canceling timer."); _cts.Cancel(); _cts.Dispose(); } @@ -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; } @@ -83,7 +83,7 @@ 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)); @@ -91,13 +91,13 @@ private static void RunCallback(IntPtr nativeCallback, Status status) 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()) @@ -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."); } } diff --git a/Realm/Realm/Native/SyncSocketProvider.WebSocket.cs b/Realm/Realm/Native/SyncSocketProvider.WebSocket.cs index 2af8ce6d76..5aa91cc811 100644 --- a/Realm/Realm/Native/SyncSocketProvider.WebSocket.cs +++ b/Realm/Realm/Native/SyncSocketProvider.WebSocket.cs @@ -45,7 +45,7 @@ private class Socket : IDisposable internal Socket(ClientWebSocket webSocket, IntPtr observer, ChannelWriter 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; @@ -56,7 +56,7 @@ internal Socket(ClientWebSocket webSocket, IntPtr observer, ChannelWriter private async Task ReadThread() { - RealmLogger.LogDefault(LogLevel.Trace, "Entering WebSocket event loop."); + RealmLogger.Default.Log(LogLevel.Trace, "Entering WebSocket event loop."); try { @@ -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; @@ -92,11 +92,11 @@ 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; } } @@ -104,7 +104,7 @@ private async Task ReadThread() { 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; @@ -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)); @@ -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 { diff --git a/Realm/Realm/Native/SyncSocketProvider.cs b/Realm/Realm/Native/SyncSocketProvider.cs index 48cbb186d7..e281847a9f 100644 --- a/Realm/Realm/Native/SyncSocketProvider.cs +++ b/Realm/Realm/Native/SyncSocketProvider.cs @@ -156,7 +156,7 @@ private interface IWork internal SyncSocketProvider(Action? onWebSocketConnection) { - RealmLogger.LogDefault(LogLevel.Debug, "Creating SyncSocketProvider."); + RealmLogger.Default.Log(LogLevel.Debug, "Creating SyncSocketProvider."); _onWebSocketConnection = onWebSocketConnection; _workQueue = Channel.CreateUnbounded(new() { SingleReader = true }); _workThread = Task.Run(WorkThread); @@ -166,7 +166,7 @@ internal SyncSocketProvider(Action? onWebSocketConnectio public void Dispose() { - RealmLogger.LogDefault(LogLevel.Debug, "Destroying SyncSocketProvider."); + RealmLogger.Default.Log(LogLevel.Debug, "Destroying SyncSocketProvider."); _workQueue.Writer.Complete(); _cts.Cancel(); _cts.Dispose(); diff --git a/Realm/Realm/Realm.cs b/Realm/Realm/Realm.cs index 1a50e2eea3..1aa7bdb83a 100644 --- a/Realm/Realm/Realm.cs +++ b/Realm/Realm/Realm.cs @@ -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)); diff --git a/Tests/Realm.Tests/Database/LoggerTests.cs b/Tests/Realm.Tests/Database/LoggerTests.cs index da781ca433..522b0f3b72 100644 --- a/Tests/Realm.Tests/Database/LoggerTests.cs +++ b/Tests/Realm.Tests/Database/LoggerTests.cs @@ -83,7 +83,7 @@ public void Logger_CanSetDefaultLoggerToBuiltInLogger() var messages = new List(); 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!"); @@ -95,7 +95,7 @@ public void Logger_CanSetDefaultLoggerToUserDefinedLogger() var messages = new List(); 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"); @@ -108,7 +108,7 @@ public void ObsoleteLogger_CanSetDefaultLoggerToUserDefinedLogger() var messages = new List(); 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"); @@ -120,7 +120,7 @@ public void Logger_SkipsDebugMessagesByDefault() var messages = new List(); 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)); } @@ -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"); @@ -201,7 +201,7 @@ public void Logger_LogsAtGivenCategory() var messages = new List(); 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"); @@ -214,7 +214,7 @@ public void Logger_LogsSdkCategoryByDefault() var messages = new List(); 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"); @@ -226,7 +226,7 @@ public void Logger_CallsCustomFunction() var messages = new List(); 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"); @@ -239,7 +239,7 @@ public void Logger_CallsObsoleteCustomFunction() var messages = new List(); 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"); @@ -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);