Skip to content

Commit e6d84b6

Browse files
committed
Revert "Remove callbacks"
This reverts commit 8dae0e8.
1 parent 15ff26a commit e6d84b6

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

lib/src/logger.dart

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:logger/src/filters/development_filter.dart';
2-
import 'package:logger/src/printers/pretty_printer.dart';
3-
import 'package:logger/src/outputs/console_output.dart';
42
import 'package:logger/src/log_filter.dart';
5-
import 'package:logger/src/log_printer.dart';
63
import 'package:logger/src/log_output.dart';
4+
import 'package:logger/src/log_printer.dart';
5+
import 'package:logger/src/outputs/console_output.dart';
6+
import 'package:logger/src/printers/pretty_printer.dart';
77

88
/// [Level]s to control logging output. Logging can be enabled to include all
99
/// levels above certain [Level].
@@ -33,10 +33,8 @@ class OutputEvent {
3333
OutputEvent(this.level, this.lines);
3434
}
3535

36-
@Deprecated('Use a custom LogFilter instead')
3736
typedef LogCallback = void Function(LogEvent event);
3837

39-
@Deprecated('Use a custom LogOutput instead')
4038
typedef OutputCallback = void Function(OutputEvent event);
4139

4240
/// Use instances of logger to send log messages to the [LogPrinter].
@@ -46,6 +44,10 @@ class Logger {
4644
/// All logs with levels below this level will be omitted.
4745
static Level level = Level.verbose;
4846

47+
static final Set<LogCallback> _logCallbacks = {};
48+
49+
static final Set<OutputCallback> _outputCallbacks = {};
50+
4951
final LogFilter _filter;
5052
final LogPrinter _printer;
5153
final LogOutput _output;
@@ -112,13 +114,19 @@ class Logger {
112114
}
113115
var logEvent = LogEvent(level, message, error, stackTrace);
114116
if (_filter.shouldLog(logEvent)) {
117+
for (var callback in _logCallbacks) {
118+
callback(logEvent);
119+
}
115120
var output = _printer.log(logEvent);
116121

117122
if (output.isNotEmpty) {
118123
var outputEvent = OutputEvent(level, output);
119124
// Issues with log output should NOT influence
120125
// the main software behavior.
121126
try {
127+
for (var callback in _outputCallbacks) {
128+
callback(outputEvent);
129+
}
122130
_output.output(outputEvent);
123131
} catch (e, s) {
124132
print(e);
@@ -135,4 +143,28 @@ class Logger {
135143
_printer.destroy();
136144
_output.destroy();
137145
}
146+
147+
/// Register a [LogCallback] which is called for each new [LogEvent].
148+
static void addLogListener(LogCallback callback) {
149+
_logCallbacks.add(callback);
150+
}
151+
152+
/// Removes a [LogCallback] which was previously registered.
153+
///
154+
/// Returns whether the callback was successfully removed.
155+
static bool removeLogListener(LogCallback callback) {
156+
return _logCallbacks.remove(callback);
157+
}
158+
159+
/// Register an [OutputCallback] which is called for each new [OutputEvent].
160+
static void addOutputListener(OutputCallback callback) {
161+
_outputCallbacks.add(callback);
162+
}
163+
164+
/// Removes a [OutputCallback] which was previously registered.
165+
///
166+
/// Returns whether the callback was successfully removed.
167+
static void removeOutputListener(OutputCallback callback) {
168+
_outputCallbacks.remove(callback);
169+
}
138170
}

0 commit comments

Comments
 (0)