|
| 1 | +// This file is automatically generated, so please do not edit it. |
| 2 | +// @generated by `flutter_rust_bridge`@ 2.10.0. |
| 3 | + |
| 4 | +// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import |
| 5 | + |
| 6 | +import '../frb_generated.dart'; |
| 7 | +import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; |
| 8 | +import 'package:logging/logging.dart'; |
| 9 | + |
| 10 | +// These functions are ignored because they are not marked as `pub`: `_construct_default_message`, `_default_log_fn`, `_default_logger_name`, `_default_max_log_level`, `from_u16`, `to_u16` |
| 11 | +// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `clone`, `enabled`, `flush`, `from`, `log` |
| 12 | + |
| 13 | +String rootLoggerName() => |
| 14 | + RustLib.instance.api.crateApiMinimalLoggingRootLoggerName(); |
| 15 | + |
| 16 | +String maxLogLevel() => |
| 17 | + RustLib.instance.api.crateApiMinimalLoggingMaxLogLevel(); |
| 18 | + |
| 19 | +/// this is the call for logging (from Rust and Dart (as logFn)) |
| 20 | +void logFn({required MirLogRecord record}) => |
| 21 | + RustLib.instance.api.crateApiMinimalLoggingLogFn(record: record); |
| 22 | + |
| 23 | +/// uses custom type translation to translate between log::LogLevel and Dart:logging::Level |
| 24 | +/// loglevel is represented by a number, so that we don't need to put \import `import 'package:logging/logging.dart';` |
| 25 | +/// into the dart preamble in flutter_rust_bridge.yaml |
| 26 | +Stream<MirLogRecord> initializeLog2Dart({required int maxLogLevel}) => |
| 27 | + RustLib.instance.api |
| 28 | + .crateApiMinimalLoggingInitializeLog2Dart(maxLogLevel: maxLogLevel); |
| 29 | + |
| 30 | +Future<int> minimalAdder({required int a, required int b}) => |
| 31 | + RustLib.instance.api.crateApiMinimalLoggingMinimalAdder(a: a, b: b); |
| 32 | + |
| 33 | +class FRBLogger { |
| 34 | + final RustStreamSink<MirLogRecord> streamSink; |
| 35 | + |
| 36 | + const FRBLogger({ |
| 37 | + required this.streamSink, |
| 38 | + }); |
| 39 | + |
| 40 | + // HINT: Make it `#[frb(sync)]` to let it become the default constructor of Dart class. |
| 41 | + static Future<FRBLogger> newInstance() => |
| 42 | + RustLib.instance.api.crateApiMinimalLoggingFrbLoggerNew(); |
| 43 | + |
| 44 | + static FRBDartLogger initLogger( |
| 45 | + {String name = 'FRBLogger', |
| 46 | + LogLevel maxLogLevel = LogLevel.info, |
| 47 | + Function({required MirLogRecord record}) customLogFunction = logFn}) { |
| 48 | + //initialize the rust side |
| 49 | + int maxLogLevelNumber = maxLogLevel.levelNumberThreshold; |
| 50 | + Stream<MirLogRecord> stream = |
| 51 | + initializeLog2Dart(maxLogLevel: maxLogLevelNumber); |
| 52 | + |
| 53 | + // Functions for type conversion for interaction with frb_dart/utils/frb_logging.dart |
| 54 | + // Wrap logFn to match `void Function({required dynamic record})` |
| 55 | + void Function({required dynamic record}) wrappedLogFn = |
| 56 | + ({required dynamic record}) { |
| 57 | + // Safely cast `dynamic` record back to `MirLogRecord` for the original `logFn` |
| 58 | + logFn(record: record as MirLogRecord); |
| 59 | + }; |
| 60 | + // Wrap fromDartLogRecord to match `dynamic Function(LogRecord record)` |
| 61 | + MirLogRecord Function(LogRecord record) wrappedFromDartLogRecord = |
| 62 | + (LogRecord record) { |
| 63 | + return MirLogRecord.fromDartLogRecord(record); |
| 64 | + }; |
| 65 | + // Wrap customLogFunction if provided, to match `void Function({required dynamic record})?` |
| 66 | + void Function({required dynamic record})? wrappedCustomLogFunction; |
| 67 | + wrappedCustomLogFunction = ({required dynamic record}) { |
| 68 | + customLogFunction(record: record as MirLogRecord); |
| 69 | + }; |
| 70 | + |
| 71 | + return FRBDartLogger.initAndGetSingleton<MirLogRecord>( |
| 72 | + streamSink: stream, |
| 73 | + name: name, |
| 74 | + logFn: wrappedLogFn, |
| 75 | + fromDartLogRecord: wrappedFromDartLogRecord, |
| 76 | + maxLogLevel: maxLogLevel, |
| 77 | + customLogFunction: wrappedCustomLogFunction, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + static FRBDartLogger getLogger([String? name]) { |
| 82 | + return FRBDartLogger.getLogger(name); |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + int get hashCode => streamSink.hashCode; |
| 87 | + |
| 88 | + @override |
| 89 | + bool operator ==(Object other) => |
| 90 | + identical(this, other) || |
| 91 | + other is FRBLogger && |
| 92 | + runtimeType == other.runtimeType && |
| 93 | + streamSink == other.streamSink; |
| 94 | +} |
| 95 | + |
| 96 | +/// mapping log crate's [Record](https://docs.rs/log/latest/log/struct.Record.html) to dart's Logger [LogRecord](https://pub.dev/documentation/logging/latest/logging/LogRecord-class.html). |
| 97 | +/// intermediary struct to avoid Record's lifetimes |
| 98 | +class MirLogRecord { |
| 99 | + final int levelNumber; |
| 100 | + final String levelName; |
| 101 | + final String message; |
| 102 | + final String loggerName; |
| 103 | + final String timestamp; |
| 104 | + final bool rustLog; |
| 105 | + final String? modulePath; |
| 106 | + final String? fileName; |
| 107 | + final int? lineNumber; |
| 108 | + |
| 109 | + const MirLogRecord({ |
| 110 | + required this.levelNumber, |
| 111 | + required this.levelName, |
| 112 | + required this.message, |
| 113 | + required this.loggerName, |
| 114 | + required this.timestamp, |
| 115 | + required this.rustLog, |
| 116 | + this.modulePath, |
| 117 | + this.fileName, |
| 118 | + this.lineNumber, |
| 119 | + }); |
| 120 | + |
| 121 | + static MirLogRecord fromDartLogRecord(LogRecord record) { |
| 122 | + return MirLogRecord( |
| 123 | + message: record.message, |
| 124 | + levelNumber: record.level.value, |
| 125 | + levelName: LogLevel.fromLoggingLevel(record.level).name.toUpperCase(), |
| 126 | + timestamp: record.time.toString(), |
| 127 | + loggerName: record.loggerName, |
| 128 | + rustLog: false, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + static LogRecord toDartLogRecordFromMir(MirLogRecord record) { |
| 133 | + return LogRecord( |
| 134 | + LogLevel.fromString(record.levelName).toLoggingLevel(), |
| 135 | + record.message, |
| 136 | + record.loggerName, |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + LogRecord toDartLogRecord() { |
| 141 | + return toDartLogRecordFromMir(this); |
| 142 | + } |
| 143 | + |
| 144 | + @override |
| 145 | + int get hashCode => |
| 146 | + levelNumber.hashCode ^ |
| 147 | + levelName.hashCode ^ |
| 148 | + message.hashCode ^ |
| 149 | + loggerName.hashCode ^ |
| 150 | + timestamp.hashCode ^ |
| 151 | + rustLog.hashCode ^ |
| 152 | + modulePath.hashCode ^ |
| 153 | + fileName.hashCode ^ |
| 154 | + lineNumber.hashCode; |
| 155 | + |
| 156 | + @override |
| 157 | + bool operator ==(Object other) => |
| 158 | + identical(this, other) || |
| 159 | + other is MirLogRecord && |
| 160 | + runtimeType == other.runtimeType && |
| 161 | + levelNumber == other.levelNumber && |
| 162 | + levelName == other.levelName && |
| 163 | + message == other.message && |
| 164 | + loggerName == other.loggerName && |
| 165 | + timestamp == other.timestamp && |
| 166 | + rustLog == other.rustLog && |
| 167 | + modulePath == other.modulePath && |
| 168 | + fileName == other.fileName && |
| 169 | + lineNumber == other.lineNumber; |
| 170 | +} |
0 commit comments