Skip to content

Commit

Permalink
Try without Realm prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 19, 2024
1 parent a2e3cec commit 7ab2ef1
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/realm_dart/lib/src/app.dart
Expand Up @@ -154,7 +154,7 @@ class App implements Finalizable {
// background isolates. This check will log a warning if the isolate name is != 'main' and doesn't start with 'test/' since dart test will
// construct a new isolate per file and we don't want to log excessively in unit test projects.
if (Isolate.current.debugName != 'main' && Isolate.current.debugName?.startsWith('test/') == false) {
Realm.logger.log(RealmLogLevel.warn,
Realm.logger.log(LogLevel.warn,
"App constructor called on Isolate ${Isolate.current.debugName} which doesn't appear to be the main isolate. If you need an app instance on a background isolate use App.getById after constructing the App on the main isolate.");
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/realm_dart/lib/src/configuration.dart
Expand Up @@ -305,12 +305,12 @@ enum SessionStopPolicy {
typedef SyncErrorHandler = void Function(SyncError);

void defaultSyncErrorHandler(SyncError e) {
Realm.logger.log(RealmLogLevel.error, e);
Realm.logger.log(LogLevel.error, e);
}

void _defaultClientResetHandler(ClientResetError e) {
Realm.logger.log(
RealmLogLevel.error,
LogLevel.error,
"A client reset error occurred but no handler was supplied. "
"Synchronization is now paused and will resume automatically once the app is restarted and "
"the server data is re-downloaded. Any un-synchronized changes the client has made or will "
Expand Down
66 changes: 33 additions & 33 deletions packages/realm_dart/lib/src/logging.dart
Expand Up @@ -8,7 +8,7 @@ import 'package:logging/logging.dart';
import 'native/realm_core.dart';

// Using classes to make a fancy hierarchical enum
sealed class RealmLogCategory {
sealed class LogCategory {
/// All possible log categories.
static final values = [
realm,
Expand All @@ -32,12 +32,12 @@ sealed class RealmLogCategory {
static final realm = _RealmLogCategory();

final String _name;
final RealmLogCategory? _parent;
final LogCategory? _parent;

RealmLogCategory._(this._name, this._parent);
LogCategory._(this._name, this._parent);

/// Returns `true` if this category contains the given [category].
bool _contains(RealmLogCategory category) {
bool _contains(LogCategory category) {
var current = category;
while (current != this) {
final isRoot = current == realm;
Expand All @@ -54,18 +54,18 @@ sealed class RealmLogCategory {
@override
String toString() => _toString;

static final _map = {for (final category in RealmLogCategory.values) category.toString(): category};
static final _map = {for (final category in LogCategory.values) category.toString(): category};

/// Returns the [RealmLogCategory] for the given [category] string.
/// Returns the [LogCategory] for the given [category] string.
/// Will throw if the category is not recognized.
factory RealmLogCategory.fromString(String category) => _map[category]!;
factory LogCategory.fromString(String category) => _map[category]!;
}

class _LeafLogCategory extends RealmLogCategory {
_LeafLogCategory(super.name, RealmLogCategory super.parent) : super._();
class _LeafLogCategory extends LogCategory {
_LeafLogCategory(super.name, LogCategory super.parent) : super._();
}

class _RealmLogCategory extends RealmLogCategory {
class _RealmLogCategory extends LogCategory {
_RealmLogCategory() : super._('Realm', null);

late final app = _LeafLogCategory('App', this);
Expand All @@ -74,24 +74,24 @@ class _RealmLogCategory extends RealmLogCategory {
late final sync = _SyncLogCategory(this);
}

class _SyncLogCategory extends RealmLogCategory {
_SyncLogCategory(RealmLogCategory parent) : super._('Sync', parent);
class _SyncLogCategory extends LogCategory {
_SyncLogCategory(LogCategory parent) : super._('Sync', parent);

late final client = _ClientLogCategory(this);
late final server = _LeafLogCategory('Server', this);
}

class _ClientLogCategory extends RealmLogCategory {
_ClientLogCategory(RealmLogCategory parent) : super._('Client', parent);
class _ClientLogCategory extends LogCategory {
_ClientLogCategory(LogCategory parent) : super._('Client', parent);

late final changeset = _LeafLogCategory('Changeset', this);
late final network = _LeafLogCategory('Network', this);
late final reset = _LeafLogCategory('Reset', this);
late final session = _LeafLogCategory('Session', this);
}

class _StorageLogCategory extends RealmLogCategory {
_StorageLogCategory(RealmLogCategory parent) : super._('Storage', parent);
class _StorageLogCategory extends LogCategory {
_StorageLogCategory(LogCategory parent) : super._('Storage', parent);

late final notification = _LeafLogCategory('Notification', this);
late final object = _LeafLogCategory('Object', this);
Expand All @@ -102,7 +102,7 @@ class _StorageLogCategory extends RealmLogCategory {
/// Specifies the criticality level above which messages will be logged
/// by the default sync client logger.
/// {@category Realm}
enum RealmLogLevel {
enum LogLevel {
/// Log everything. This will seriously harm the performance of the
/// sync client and should never be used in production scenarios.
all(Level.ALL),
Expand Down Expand Up @@ -136,14 +136,14 @@ enum RealmLogLevel {
;

/// The [Level] from package [logging](https://pub.dev/packages/logging) that
/// corresponds to this [RealmLogLevel].
/// corresponds to this [LogLevel].
final Level level;

const RealmLogLevel(this.level);
const LogLevel(this.level);
}

/// A record of a log message from the Realm SDK.
typedef RealmLogRecord = ({RealmLogCategory category, RealmLogLevel level, String message});
typedef LogRecord = ({LogCategory category, LogLevel level, String message});

/// A logger that logs messages from the Realm SDK.
///
Expand All @@ -163,7 +163,7 @@ typedef RealmLogRecord = ({RealmLogCategory category, RealmLogLevel level, Strin
/// If no listeners are attached to [onRecord] in any isolate, the trace will go
/// to stdout.
class RealmLogger {
static final _controller = StreamController<RealmLogRecord>.broadcast(
static final _controller = StreamController<LogRecord>.broadcast(
onListen: () => realmCore.loggerAttach(),
onCancel: () => realmCore.loggerDetach(),
);
Expand All @@ -172,33 +172,33 @@ class RealmLogger {

/// Set the log [level] for the given [category].
///
/// If [category] is not provided, the log level will be set [RealmLogCategory.realm].
void setLogLevel(RealmLogLevel level, {RealmLogCategory? category}) {
category ??= RealmLogCategory.realm;
/// If [category] is not provided, the log level will be set [LogCategory.realm].
void setLogLevel(LogLevel level, {LogCategory? category}) {
category ??= LogCategory.realm;
realmCore.setLogLevel(level, category: category);
}

/// The stream of log records.
///
/// This is a broadcast stream. It is safe to listen to it multiple times.
/// If no listeners are attached in any isolate, the trace will go to stdout.
Stream<RealmLogRecord> get onRecord => _controller.stream;
Stream<LogRecord> get onRecord => _controller.stream;

void _raise(RealmLogRecord record) {
void _raise(LogRecord record) {
_controller.add(record);
}

void _log(RealmLogLevel level, Object message, {RealmLogCategory? category}) {
category ??= RealmLogCategory.realm.sdk;
realmCore.logMessage(RealmLogCategory.realm.sdk, level, message.toString());
void _log(LogLevel level, Object message, {LogCategory? category}) {
category ??= LogCategory.realm.sdk;
realmCore.logMessage(LogCategory.realm.sdk, level, message.toString());
}
}

extension RealmLoggerInternal on RealmLogger {
void raise(RealmLogRecord record) => _raise(record);
void log(RealmLogLevel level, Object message, {RealmLogCategory? category}) => _log(level, message, category: category);
void raise(LogRecord record) => _raise(record);
void log(LogLevel level, Object message, {LogCategory? category}) => _log(level, message, category: category);
}

extension RealmLogCategoryInternal on RealmLogCategory {
bool contains(RealmLogCategory category) => _contains(category);
extension RealmLogCategoryInternal on LogCategory {
bool contains(LogCategory category) => _contains(category);
}
18 changes: 9 additions & 9 deletions packages/realm_dart/lib/src/native/realm_core.dart
Expand Up @@ -1766,7 +1766,7 @@ class _RealmCore {
try {
realm.updateSchema();
} catch (e) {
Realm.logger.log(RealmLogLevel.error, 'Failed to update Realm schema: $e');
Realm.logger.log(LogLevel.error, 'Failed to update Realm schema: $e');
}
}

Expand Down Expand Up @@ -2026,15 +2026,15 @@ class _RealmCore {
request.add(utf8.encode(body));
}

Realm.logger.log(RealmLogLevel.debug, "HTTP Transport: Executing ${method.name} $url");
Realm.logger.log(LogLevel.debug, "HTTP Transport: Executing ${method.name} $url");

final stopwatch = Stopwatch()..start();

// Do the call..
final response = await request.close();

stopwatch.stop();
Realm.logger.log(RealmLogLevel.debug, "HTTP Transport: Executed ${method.name} $url: ${response.statusCode} in ${stopwatch.elapsedMilliseconds} ms");
Realm.logger.log(LogLevel.debug, "HTTP Transport: Executed ${method.name} $url: ${response.statusCode} in ${stopwatch.elapsedMilliseconds} ms");

final responseBody = await response.fold<List<int>>([], (acc, l) => acc..addAll(l)); // gather response

Expand Down Expand Up @@ -2063,21 +2063,21 @@ class _RealmCore {

responseRef.custom_status_code = _CustomErrorCode.noError.code;
} on SocketException catch (socketEx) {
Realm.logger.log(RealmLogLevel.warn, "HTTP Transport: SocketException executing ${method.name} $url: $socketEx");
Realm.logger.log(LogLevel.warn, "HTTP Transport: SocketException executing ${method.name} $url: $socketEx");
responseRef.custom_status_code = _CustomErrorCode.timeout.code;
} on HttpException catch (httpEx) {
Realm.logger.log(RealmLogLevel.warn, "HTTP Transport: HttpException executing ${method.name} $url: $httpEx");
Realm.logger.log(LogLevel.warn, "HTTP Transport: HttpException executing ${method.name} $url: $httpEx");
responseRef.custom_status_code = _CustomErrorCode.unknownHttp.code;
} catch (ex) {
Realm.logger.log(RealmLogLevel.error, "HTTP Transport: Exception executing ${method.name} $url: $ex");
Realm.logger.log(LogLevel.error, "HTTP Transport: Exception executing ${method.name} $url: $ex");
responseRef.custom_status_code = _CustomErrorCode.unknown.code;
} finally {
_realmLib.realm_http_transport_complete_request(request_context, response_pointer);
}
});
}

void logMessage(RealmLogCategory category, RealmLogLevel logLevel, String message) {
void logMessage(LogCategory category, LogLevel logLevel, String message) {
return using((arena) {
_realmLib.realm_dart_log(logLevel.index, category.toString().toCharPtr(arena), message.toCharPtr(arena));
});
Expand Down Expand Up @@ -3065,7 +3065,7 @@ class _RealmCore {
}
}

void setLogLevel(RealmLogLevel level, {required RealmLogCategory category}) {
void setLogLevel(LogLevel level, {required LogCategory category}) {
using((arena) {
_realmLib.realm_set_log_level_category(category.toString().toCharPtr(arena), level.index);
});
Expand Down Expand Up @@ -3102,7 +3102,7 @@ class LastError {
const _enableFinalizerTrace = false;

// Level used for finalization trace, if enabled.
const _finalizerTraceLevel = RealmLogLevel.trace;
const _finalizerTraceLevel = LogLevel.trace;

void _traceFinalization(Object o) {
Realm.logger.log(_finalizerTraceLevel, 'Finalizing: $o');
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/lib/src/realm_class.dart
Expand Up @@ -516,7 +516,7 @@ class Realm implements Finalizable {
///
/// If no isolate subscribes to the stream, the trace messages will go to stdout.
///
/// The default log level is [RealmLogLevel.info].
/// The default log level is [LogLevel.info].
static const logger = RealmLogger();

/// Used to shutdown Realm and allow the process to correctly release native resources and exit.
Expand Down
6 changes: 3 additions & 3 deletions packages/realm_dart/lib/src/scheduler.dart
Expand Up @@ -24,14 +24,14 @@ class Scheduler {
_receivePort.handler = (dynamic message) {
if (message is List) {
// currently the only `message as List` is from the logger.
final category = RealmLogCategory.fromString(message[0] as String);
final level = RealmLogLevel.values[message[1] as int];
final category = LogCategory.fromString(message[0] as String);
final level = LogLevel.values[message[1] as int];
final text = message[2] as String;
Realm.logger.raise((category: category, level: level, message: text));
} else if (message is int) {
realmCore.invokeScheduler(message);
} else {
Realm.logger.log(RealmLogLevel.error, 'Unexpected Scheduler message type: ${message.runtimeType} - $message');
Realm.logger.log(LogLevel.error, 'Unexpected Scheduler message type: ${message.runtimeType} - $message');
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/app_test.dart
Expand Up @@ -358,7 +358,7 @@ void main() {
});

baasTest('App(AppConfiguration) on background isolate logs warning', (appConfig) async {
Realm.logger.setLogLevel(RealmLogLevel.warn);
Realm.logger.setLogLevel(LogLevel.warn);

final sb = StringBuffer();
Realm.logger.onRecord.listen((event) {
Expand Down

0 comments on commit 7ab2ef1

Please sign in to comment.