Add logging to EventBus using the Platform Logging API
This commit is contained in:
parent
8cf51441ad
commit
b6b73d335a
@ -108,7 +108,7 @@ To include it inside your project, just add the Maven repository and the depende
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>dev.kske</groupId>
|
<groupId>dev.kske</groupId>
|
||||||
<artifactId>event-bus</artifactId>
|
<artifactId>event-bus</artifactId>
|
||||||
<version>0.0.4</version>
|
<version>0.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
```
|
```
|
||||||
|
2
pom.xml
2
pom.xml
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>dev.kske</groupId>
|
<groupId>dev.kske</groupId>
|
||||||
<artifactId>event-bus</artifactId>
|
<artifactId>event-bus</artifactId>
|
||||||
<version>0.0.4</version>
|
<version>0.1.0</version>
|
||||||
|
|
||||||
<name>Event Bus</name>
|
<name>Event Bus</name>
|
||||||
<description>An event handling framework for Java utilizing annotations.</description>
|
<description>An event handling framework for Java utilizing annotations.</description>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package dev.kske.eventbus;
|
package dev.kske.eventbus;
|
||||||
|
|
||||||
|
import java.lang.System.Logger;
|
||||||
|
import java.lang.System.Logger.Level;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ -19,6 +21,8 @@ public final class EventBus {
|
|||||||
|
|
||||||
private static EventBus singletonInstance;
|
private static EventBus singletonInstance;
|
||||||
|
|
||||||
|
private static final Logger logger = System.getLogger(EventBus.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produces a singleton instance of the event bus. It is lazily initialized on the first call.
|
* Produces a singleton instance of the event bus. It is lazily initialized on the first call.
|
||||||
*
|
*
|
||||||
@ -26,8 +30,10 @@ public final class EventBus {
|
|||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public static EventBus getInstance() {
|
public static EventBus getInstance() {
|
||||||
if (singletonInstance == null)
|
if (singletonInstance == null) {
|
||||||
|
logger.log(Level.DEBUG, "Initializing singleton event bus instance");
|
||||||
singletonInstance = new EventBus();
|
singletonInstance = new EventBus();
|
||||||
|
}
|
||||||
return singletonInstance;
|
return singletonInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +50,7 @@ public final class EventBus {
|
|||||||
*/
|
*/
|
||||||
public void dispatch(IEvent event) {
|
public void dispatch(IEvent event) {
|
||||||
Objects.requireNonNull(event);
|
Objects.requireNonNull(event);
|
||||||
|
logger.log(Level.INFO, "Dispatching event {0}", event);
|
||||||
getHandlersFor(event.getClass()).forEach(handler -> handler.execute(event));
|
getHandlersFor(event.getClass()).forEach(handler -> handler.execute(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +91,8 @@ public final class EventBus {
|
|||||||
Objects.requireNonNull(listener);
|
Objects.requireNonNull(listener);
|
||||||
if (registeredListeners.contains(listener))
|
if (registeredListeners.contains(listener))
|
||||||
throw new EventBusException(listener + " already registered!");
|
throw new EventBusException(listener + " already registered!");
|
||||||
|
logger.log(Level.INFO, "Registering event listener {0}", listener.getClass().getName());
|
||||||
|
boolean handlerBound = false;
|
||||||
|
|
||||||
registeredListeners.add(listener);
|
registeredListeners.add(listener);
|
||||||
for (var method : listener.getClass().getDeclaredMethods()) {
|
for (var method : listener.getClass().getDeclaredMethods()) {
|
||||||
@ -97,9 +106,18 @@ public final class EventBus {
|
|||||||
var handler = new EventHandler(listener, method, annotation);
|
var handler = new EventHandler(listener, method, annotation);
|
||||||
if (!bindings.containsKey(handler.getEventType()))
|
if (!bindings.containsKey(handler.getEventType()))
|
||||||
bindings.put(handler.getEventType(), new TreeSet<>());
|
bindings.put(handler.getEventType(), new TreeSet<>());
|
||||||
|
logger.log(Level.DEBUG, "Binding event handler {0}", handler);
|
||||||
bindings.get(handler.getEventType())
|
bindings.get(handler.getEventType())
|
||||||
.add(handler);
|
.add(handler);
|
||||||
|
handlerBound = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!handlerBound)
|
||||||
|
logger.log(
|
||||||
|
Level.WARNING,
|
||||||
|
"No event handlers bound for event listener {0}",
|
||||||
|
listener.getClass().getName()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -110,11 +128,17 @@ public final class EventBus {
|
|||||||
*/
|
*/
|
||||||
public void removeListener(EventListener listener) {
|
public void removeListener(EventListener listener) {
|
||||||
Objects.requireNonNull(listener);
|
Objects.requireNonNull(listener);
|
||||||
|
logger.log(Level.INFO, "Removing event listener {0}", listener.getClass().getName());
|
||||||
|
|
||||||
for (var binding : bindings.values()) {
|
for (var binding : bindings.values()) {
|
||||||
var it = binding.iterator();
|
var it = binding.iterator();
|
||||||
while (it.hasNext())
|
while (it.hasNext()) {
|
||||||
if (it.next().getListener() == listener)
|
var handler = it.next();
|
||||||
|
if (handler.getListener() == listener) {
|
||||||
|
logger.log(Level.DEBUG, "Unbinding event handler {0}", handler);
|
||||||
it.remove();
|
it.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
registeredListeners.remove(listener);
|
registeredListeners.remove(listener);
|
||||||
}
|
}
|
||||||
@ -125,6 +149,7 @@ public final class EventBus {
|
|||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public void clearListeners() {
|
public void clearListeners() {
|
||||||
|
logger.log(Level.INFO, "Clearing event listeners");
|
||||||
bindings.clear();
|
bindings.clear();
|
||||||
registeredListeners.clear();
|
registeredListeners.clear();
|
||||||
}
|
}
|
||||||
|
@ -77,6 +77,11 @@ final class EventHandler implements Comparable<EventHandler> {
|
|||||||
return priority == 0 ? hashCode() - other.hashCode() : priority;
|
return priority == 0 ? hashCode() - other.hashCode() : priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("EventHandler[method=%s, annotation=%s]", method, annotation);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the event handler.
|
* Executes the event handler.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user