From c685e9ea553c1bf93182f1f1f140610307954aaf Mon Sep 17 00:00:00 2001 From: LJ <81748770+elle-j@users.noreply.github.com> Date: Wed, 26 Jun 2024 13:51:39 +0200 Subject: [PATCH] Replace static '_logLevel' member with call into Core. --- Realm/Realm/Handles/SharedRealmHandle.cs | 5 +++++ Realm/Realm/Logging/Logger.cs | 20 ++++++++++++++------ wrappers/src/shared_realm_cs.cpp | 10 ++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Realm/Realm/Handles/SharedRealmHandle.cs b/Realm/Realm/Handles/SharedRealmHandle.cs index a82588b259..14af2ec71b 100644 --- a/Realm/Realm/Handles/SharedRealmHandle.cs +++ b/Realm/Realm/Handles/SharedRealmHandle.cs @@ -224,6 +224,9 @@ public static extern void rename_property(SharedRealmHandle sharedRealm, [DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_refresh_async", CallingConvention = CallingConvention.Cdecl)] public static extern bool refresh_async(SharedRealmHandle realm, IntPtr tcs_handle, out NativeException ex); + [DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_get_log_level", CallingConvention = CallingConvention.Cdecl)] + public static extern LogLevel get_log_level([MarshalAs(UnmanagedType.LPWStr)] string category_name, IntPtr category_name_len); + [DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_set_log_level", CallingConvention = CallingConvention.Cdecl)] public static extern void set_log_level(LogLevel level, [MarshalAs(UnmanagedType.LPWStr)] string category_name, IntPtr category_name_len); @@ -272,6 +275,8 @@ public static unsafe void Initialize() notifyObject, notifyDictionary, onMigration, shouldCompact, handleTaskCompletion, onInitialization); } + public static LogLevel GetLogLevel(LogCategory category) => NativeMethods.get_log_level(category.Name, (IntPtr)category.Name.Length); + public static void SetLogLevel(LogLevel level, LogCategory category) => NativeMethods.set_log_level(level, category.Name, (IntPtr)category.Name.Length); [Preserve] diff --git a/Realm/Realm/Logging/Logger.cs b/Realm/Realm/Logging/Logger.cs index e409d8cbd8..8a0078594a 100644 --- a/Realm/Realm/Logging/Logger.cs +++ b/Realm/Realm/Logging/Logger.cs @@ -35,11 +35,11 @@ public abstract class Logger { private readonly Lazy _gcHandle; + // TODO(lj): Use this default log level? + private static readonly LogLevel _defaultLogLevel = LogLevel.Info; private static readonly LogCategory _defaultLogCategory = LogCategory.Realm; - private static Logger? _defaultLogger; - // TODO(lj): Remove since it's not one level anymore. (Get it from Core) - private static LogLevel _logLevel = LogLevel.Info; private static LogCategory _logCategory = _defaultLogCategory; + private static Logger? _defaultLogger; /// /// Gets a that outputs messages to the default console. For most project types, that will be @@ -102,7 +102,8 @@ public abstract class Logger /// The log level for Realm-originating messages. public static LogLevel LogLevel { - get => _logLevel; + // TODO(lj): Do we want to deprecate the getter as well? + get => GetLogLevel(); // TODO(lj): Deprecate and refer to `SetLogLevel`. set { @@ -115,12 +116,19 @@ public static LogCategory LogCategory get => _logCategory; } + public static LogLevel GetLogLevel(LogCategory? category = null) + { + // TODO(lj): Perhaps we should grab the current category (`_logCategory`) + // instead of the default here? If there hasn't been a category + // explicitly set, it will still be the default. + category ??= _defaultLogCategory; + return SharedRealmHandle.GetLogLevel(category); + } + public static void SetLogLevel(LogLevel level, LogCategory? category = null) { category ??= _defaultLogCategory; SharedRealmHandle.SetLogLevel(level, category); - // TODO(lj): Remove setting `_logLevel` as we should get the level at the current category. - _logLevel = level; _logCategory = category; } diff --git a/wrappers/src/shared_realm_cs.cpp b/wrappers/src/shared_realm_cs.cpp index 6357b41f87..45f857fec9 100644 --- a/wrappers/src/shared_realm_cs.cpp +++ b/wrappers/src/shared_realm_cs.cpp @@ -296,6 +296,16 @@ REALM_EXPORT void shared_realm_install_callbacks( LogCategory::realm.set_default_level_threshold(Logger::Level::info); } +REALM_EXPORT Logger::Level shared_realm_get_log_level(uint16_t* category_name_buf, size_t category_name_len) { + Utf16StringAccessor category_name(category_name_buf, category_name_len); + // TODO(lj): Usage in Core: + auto& category = LogCategory::get_category(category_name); + return Logger::get_default_logger()->get_level_threshold(category); + + // TODO(lj): But this seems to work as well: + // return LogCategory::get_category(category_name).get_default_level_threshold(); +} + REALM_EXPORT void shared_realm_set_log_level(Logger::Level level, uint16_t* category_name_buf, size_t category_name_len) { Utf16StringAccessor category_name(category_name_buf, category_name_len); LogCategory::get_category(category_name).set_default_level_threshold(level);