From d5e9b7fbef27a30156cadfcfb7ea666bbdfc2e3f Mon Sep 17 00:00:00 2001 From: marcojakob Date: Mon, 16 Sep 2013 23:31:08 +0200 Subject: [PATCH] Fix issue #8: Add logging of events that flow through event bus --- CHANGELOG.md | 3 +++ example/event_bus_example.dart | 8 +++++++- example/events.dart | 4 ++-- lib/event_bus.dart | 9 +++++++++ lib/src/logging_event_bus.dart | 14 ++++++++++++++ pubspec.yaml | 5 ++++- 6 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 lib/src/logging_event_bus.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index d432053..2dcb33b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Changelog ================ +## Version 0.2.3 (2013-09-16) ## +* Fix issue #8: Add logging of events that flow through event bus + ## Version 0.2.2 (2013-09-16) ## * Change default of SimpleEventBus to sync (same as factory in EventBus) diff --git a/example/event_bus_example.dart b/example/event_bus_example.dart index a629eae..be5f31c 100644 --- a/example/event_bus_example.dart +++ b/example/event_bus_example.dart @@ -2,13 +2,19 @@ import 'dart:html'; import 'dart:async'; import 'events.dart' as events; +import 'package:logging_handlers/logging_handlers_shared.dart'; +import 'package:logging/logging.dart'; int counterA = 1; int counterB = 1; void main() { + // Init logging. + Logger.root.onRecord.listen(new PrintHandler().call); + Logger.root.level = Level.FINEST; + // Initialize the global event bus. - events.init(new events.EventBus()); + events.init(new events.LoggingEventBus()); // Initialize the listener boxes. Listener listener1 = new Listener(query('#listener-1')); diff --git a/example/events.dart b/example/events.dart index 2c298f4..25398ec 100644 --- a/example/events.dart +++ b/example/events.dart @@ -16,5 +16,5 @@ void init(EventBus eventBus) { _eventBus = eventBus; } -final EventType textUpdateA = new EventType(); -final EventType textUpdateB = new EventType(); \ No newline at end of file +final EventType textUpdateA = new EventType('textUpdateA'); +final EventType textUpdateB = new EventType('textUpdateB'); \ No newline at end of file diff --git a/lib/event_bus.dart b/lib/event_bus.dart index 17dfb17..4465cfe 100644 --- a/lib/event_bus.dart +++ b/lib/event_bus.dart @@ -1,8 +1,10 @@ library event_bus; +import 'package:logging/logging.dart'; import 'dart:async'; part 'src/simple_event_bus.dart'; +part 'src/logging_event_bus.dart'; /** * Dispatches events to listeners using the Dart [Stream] API. The [EventBus] @@ -58,6 +60,13 @@ abstract class EventBus { */ class EventType { + String name; + + /** + * Constructor with an optional [name] for logging purposes. + */ + EventType([this.name]); + /** * Returns true if the provided data is of type [T]. * diff --git a/lib/src/logging_event_bus.dart b/lib/src/logging_event_bus.dart new file mode 100644 index 0000000..f6a7cc1 --- /dev/null +++ b/lib/src/logging_event_bus.dart @@ -0,0 +1,14 @@ +part of event_bus; + +final _logger = new Logger("event_bus"); + +/** + * A [SimpleEventBus] that adds logging. + */ +class LoggingEventBus extends SimpleEventBus { + + void fire(EventType/**/ eventType, /**/ data) { + super.fire(eventType, data); + _logger.finest('event fired: ${eventType.name}'); + } +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index ff87535..8bee215 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,9 +1,12 @@ name: event_bus -version: 0.2.2 +version: 0.2.3 author: Marco Jakob description: A simple Event Bus using Dart Streams for decoupling applications homepage: https://github.com/marcojakob/dart-event-bus documentation: http://edu.makery.ch/projects/dart-event-bus +dependencies: + logging: any dev_dependencies: browser: any + logging_handlers: any unittest: any