-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogger_bridge.dart
More file actions
101 lines (86 loc) · 2.92 KB
/
logger_bridge.dart
File metadata and controls
101 lines (86 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:optimizely_flutter_sdk/src/logger/flutter_logger.dart';
import 'package:optimizely_flutter_sdk/optimizely_flutter_sdk.dart';
class LoggerBridge {
static const MethodChannel _loggerChannel =
MethodChannel('optimizely_flutter_sdk_logger');
static OptimizelyLogger? _customLogger;
/// Initialize the logger bridge to receive calls from native
static void initialize(OptimizelyLogger? logger) {
logInfo('[LoggerBridge] Initializing with logger: ${logger != null}');
_customLogger = logger;
_loggerChannel.setMethodCallHandler(_handleMethodCall);
}
/// Handle incoming method calls from native Swift/Java code
static Future<void> _handleMethodCall(MethodCall call) async {
logInfo('[LoggerBridge] Received method call: ${call.method}');
try {
switch (call.method) {
case 'log':
await _handleLogCall(call);
break;
default:
logWarning('[LoggerBridge] Unknown method call: ${call.method}');
}
} catch (e) {
logError('[LoggerBridge] Error handling method call: $e');
}
}
/// Process the log call from Swift/Java
static Future<void> _handleLogCall(MethodCall call) async {
try {
final args = Map<String, dynamic>.from(call.arguments ?? {});
final levelRawValue = args['level'] as int?;
final message = args['message'] as String?;
if (levelRawValue == null || message == null) {
logError('[LoggerBridge] Warning: Missing level or message in log call');
return;
}
final level = _convertLogLevel(levelRawValue);
logInfo('[LoggerBridge] Processing log: level=$levelRawValue, message=$message');
if (_customLogger != null) {
_customLogger!.log(level, message);
} else {
logInfo('[Optimizely ${level.name}] $message');
}
} catch (e) {
logError('[LoggerBridge] Error processing log call: $e');
}
}
/// Convert native log level to Flutter enum
static OptimizelyLogLevel _convertLogLevel(int rawValue) {
switch (rawValue) {
case 1:
return OptimizelyLogLevel.error;
case 2:
return OptimizelyLogLevel.warning;
case 3:
return OptimizelyLogLevel.info;
case 4:
return OptimizelyLogLevel.debug;
default:
return OptimizelyLogLevel.info;
}
}
/// Expose convertLogLevel
static OptimizelyLogLevel convertLogLevel(int rawValue) {
return _convertLogLevel(rawValue);
}
/// Check if a custom logger is set
static bool hasLogger() {
return _customLogger != null;
}
/// Get the current logger
static OptimizelyLogger? getCurrentLogger() {
return _customLogger;
}
/// Reset logger state
static void reset() {
_customLogger = null;
}
/// Simulate method calls
static Future<void> handleMethodCallForTesting(MethodCall call) async {
await _handleMethodCall(call);
}
}