Skip to content

Commit 4ce1943

Browse files
authored
Improve logging (#790)
1 parent 2eb0506 commit 4ce1943

21 files changed

+135
-15
lines changed

core/src/main/java/edu/wpi/grip/core/Connection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import edu.wpi.grip.core.sockets.InputSocket;
99
import edu.wpi.grip.core.sockets.OutputSocket;
1010

11+
import com.google.common.base.MoreObjects;
1112
import com.google.common.eventbus.EventBus;
1213
import com.google.common.eventbus.Subscribe;
1314
import com.google.inject.Inject;
@@ -112,6 +113,14 @@ public void remove() {
112113
eventBus.post(new ConnectionRemovedEvent(this));
113114
}
114115

116+
@Override
117+
public String toString() {
118+
return MoreObjects.toStringHelper(this)
119+
.add("src", outputSocket.simpleString())
120+
.add("dst", inputSocket.simpleString())
121+
.toString();
122+
}
123+
115124
public interface Factory<T> {
116125
Connection<T> create(OutputSocket<? extends T> outputSocket, InputSocket<T> inputSocket);
117126
}

core/src/main/java/edu/wpi/grip/core/GripCoreModule.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.grip.core;
22

3+
import edu.wpi.grip.core.events.EventLogger;
34
import edu.wpi.grip.core.events.UnexpectedThrowableEvent;
45
import edu.wpi.grip.core.metrics.BenchmarkRunner;
56
import edu.wpi.grip.core.metrics.Timer;
@@ -68,8 +69,8 @@ public class GripCoreModule extends AbstractModule {
6869
= new FileHandler(GripFileManager.GRIP_DIRECTORY.getPath() + "/GRIP.log");
6970

7071
//Set level to handler and logger
71-
fileHandler.setLevel(Level.FINE);
72-
globalLogger.setLevel(Level.FINE);
72+
fileHandler.setLevel(Level.INFO);
73+
globalLogger.setLevel(Level.INFO);
7374

7475
// We need to stream to System.out instead of System.err
7576
final StreamHandler sh = new StreamHandler(System.out, new SimpleFormatter()) {
@@ -124,6 +125,7 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
124125
});
125126

126127
bind(EventBus.class).toInstance(eventBus);
128+
bind(EventLogger.class).asEagerSingleton();
127129

128130
// Allow for just injecting the settings provider, instead of the whole pipeline
129131
bind(SettingsProvider.class).to(Pipeline.class);
@@ -172,7 +174,7 @@ protected void onSubscriberException(Throwable exception, @Nullable SubscriberEx
172174
} else {
173175
logger.log(Level.SEVERE, "An event subscriber threw an exception", exception);
174176
eventBus.post(new UnexpectedThrowableEvent(exception, "An event subscriber threw an "
175-
+ "exception"));
177+
+ "exception on thread '" + Thread.currentThread().getName() + "'"));
176178
}
177179
}
178180

core/src/main/java/edu/wpi/grip/core/Source.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import edu.wpi.grip.core.sources.NetworkTableEntrySource;
1010
import edu.wpi.grip.core.util.ExceptionWitness;
1111

12+
import com.google.common.base.MoreObjects;
1213
import com.google.common.collect.ImmutableList;
1314
import com.google.inject.Inject;
1415

@@ -100,6 +101,13 @@ public final void initializeSafely() {
100101
}
101102
}
102103

104+
@Override
105+
public String toString() {
106+
return MoreObjects.toStringHelper(this)
107+
.add("name", getName())
108+
.toString();
109+
}
110+
103111
public interface SourceFactory {
104112
Source create(Class<?> type, Properties properties) throws IOException;
105113
}

core/src/main/java/edu/wpi/grip/core/Step.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import edu.wpi.grip.core.sockets.Socket;
77
import edu.wpi.grip.core.util.ExceptionWitness;
88

9+
import com.google.common.base.MoreObjects;
910
import com.google.common.collect.ImmutableList;
1011
import com.google.inject.Inject;
1112
import com.google.inject.Singleton;
@@ -174,6 +175,13 @@ protected boolean removed() {
174175
return removed;
175176
}
176177

178+
@Override
179+
public String toString() {
180+
return MoreObjects.toStringHelper(this)
181+
.add("operation", description.name())
182+
.toString();
183+
}
184+
177185
@Singleton
178186
public static class Factory {
179187
private final ExceptionWitness.Factory exceptionWitnessFactory;

core/src/main/java/edu/wpi/grip/core/events/AppSettingsChangedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* An event fired when the app settings are changed.
99
*/
10-
public class AppSettingsChangedEvent {
10+
public class AppSettingsChangedEvent implements LoggableEvent {
1111

1212
private final AppSettings appSettings;
1313

core/src/main/java/edu/wpi/grip/core/events/BenchmarkEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* An event posted before and after a pipeline is benchmarked.
55
*/
6-
public final class BenchmarkEvent {
6+
public final class BenchmarkEvent implements LoggableEvent {
77

88
private final boolean isStart;
99

core/src/main/java/edu/wpi/grip/core/events/CodeGenerationSettingsChangedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* An event fired when code generation settings are changed.
99
*/
10-
public class CodeGenerationSettingsChangedEvent implements DirtiesSaveEvent {
10+
public class CodeGenerationSettingsChangedEvent implements DirtiesSaveEvent, LoggableEvent {
1111

1212
private final CodeGenerationSettings settings;
1313

core/src/main/java/edu/wpi/grip/core/events/ConnectionAddedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* An event that occurs when a new connection is added to the pipeline. This is triggered by the
1111
* user adding a connection with the GUI.
1212
*/
13-
public class ConnectionAddedEvent implements RunPipelineEvent, DirtiesSaveEvent {
13+
public class ConnectionAddedEvent implements RunPipelineEvent, DirtiesSaveEvent, LoggableEvent {
1414
private final Connection connection;
1515

1616
/**

core/src/main/java/edu/wpi/grip/core/events/ConnectionRemovedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* An event that occurs when a connection is removed from the pipeline. This is triggered by the
1111
* user deleting a connection with the GUI.
1212
*/
13-
public class ConnectionRemovedEvent implements DirtiesSaveEvent {
13+
public class ConnectionRemovedEvent implements DirtiesSaveEvent, LoggableEvent {
1414
private final Connection<?> connection;
1515

1616
/**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package edu.wpi.grip.core.events;
2+
3+
import com.google.common.eventbus.AllowConcurrentEvents;
4+
import com.google.common.eventbus.Subscribe;
5+
import com.google.inject.Singleton;
6+
7+
import java.util.logging.Logger;
8+
9+
/**
10+
* Class for logging events as they're posted to the event bus.
11+
*/
12+
@Singleton
13+
public class EventLogger {
14+
15+
private static final Logger logger = Logger.getLogger(EventLogger.class.getName());
16+
17+
@Subscribe
18+
@AllowConcurrentEvents
19+
public void eventPosted(LoggableEvent event) {
20+
final String threadName = Thread.currentThread().getName();
21+
logger.log(event.logLevel(),
22+
"Event on thread '" + threadName + "': " + event.asLoggableString());
23+
}
24+
25+
}

0 commit comments

Comments
 (0)