Skip to content

Commit

Permalink
Replace static '_logLevel' member with call into Core.
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j committed Jun 26, 2024
1 parent 6ca9bb5 commit c685e9e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Realm/Realm/Handles/SharedRealmHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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]
Expand Down
20 changes: 14 additions & 6 deletions Realm/Realm/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public abstract class Logger
{
private readonly Lazy<GCHandle> _gcHandle;

// TODO(lj): Use this default log level?

Check warning on line 38 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Verify TODOs

Realm/Realm/Logging/Logger.cs#L38

TODO entry doesn't have a link to Github issue or Jira ticket lj): Use this default log level?
private static readonly LogLevel _defaultLogLevel = LogLevel.Info;

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)

The field 'Logger._defaultLogLevel' is assigned but its value is never used

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Test Weaver (macos-14, osx-arm64)

The field 'Logger._defaultLogLevel' is assigned but its value is never used

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Test Source Generation

The field 'Logger._defaultLogLevel' is assigned but its value is never used

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Test Weaver (windows-latest, win-x64)

The field 'Logger._defaultLogLevel' is assigned but its value is never used

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Analyze C#

The field 'Logger._defaultLogLevel' is assigned but its value is never used

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Package NuGet

The field 'Logger._defaultLogLevel' is assigned but its value is never used

Check warning on line 39 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Test Code Coverage

The field 'Logger._defaultLogLevel' is assigned but its value is never used [/home/runner/work/realm-dotnet/realm-dotnet/Realm/Realm/Realm.csproj::TargetFramework=net8.0]
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;

/// <summary>
/// Gets a <see cref="ConsoleLogger"/> that outputs messages to the default console. For most project types, that will be
Expand Down Expand Up @@ -102,7 +102,8 @@ public abstract class Logger
/// <value>The log level for Realm-originating messages.</value>
public static LogLevel LogLevel
{
get => _logLevel;
// TODO(lj): Do we want to deprecate the getter as well?

Check warning on line 105 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Verify TODOs

Realm/Realm/Logging/Logger.cs#L105

TODO entry doesn't have a link to Github issue or Jira ticket lj): Do we want to deprecate the getter as well?
get => GetLogLevel();
// TODO(lj): Deprecate and refer to `SetLogLevel`.

Check warning on line 107 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Verify TODOs

Realm/Realm/Logging/Logger.cs#L107

TODO entry doesn't have a link to Github issue or Jira ticket lj): Deprecate and refer to `SetLogLevel`.

Check warning on line 107 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Test Weaver (windows-latest, win-x64)

set
{
Expand All @@ -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`)

Check warning on line 121 in Realm/Realm/Logging/Logger.cs

View workflow job for this annotation

GitHub Actions / Verify TODOs

Realm/Realm/Logging/Logger.cs#L121

TODO entry doesn't have a link to Github issue or Jira ticket 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;
}

Expand Down
10 changes: 10 additions & 0 deletions wrappers/src/shared_realm_cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Check warning on line 301 in wrappers/src/shared_realm_cs.cpp

View workflow job for this annotation

GitHub Actions / Verify TODOs

wrappers/src/shared_realm_cs.cpp#L301

TODO entry doesn't have a link to Github issue or Jira ticket 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:

Check warning on line 305 in wrappers/src/shared_realm_cs.cpp

View workflow job for this annotation

GitHub Actions / Verify TODOs

wrappers/src/shared_realm_cs.cpp#L305

TODO entry doesn't have a link to Github issue or Jira ticket 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);
Expand Down

0 comments on commit c685e9e

Please sign in to comment.