Skip to content

Commit 4bfd6ce

Browse files
Added logging support with a default binding to SLF4J-Simple (prints to command line, like before). Also addressed several warnings in the POM during the build. Closes #2, #3, #4, #5
1 parent 6c2f580 commit 4bfd6ce

File tree

8 files changed

+135
-52
lines changed

8 files changed

+135
-52
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Filesystem Stuff
2+
**.DS_Store
3+
4+
# Maven output directory
5+
**/target
6+
7+
### Maven Plugins ###
8+
.flattened-pom.xml
9+
10+
### Intellij ###
11+
**/.idea
12+
**/*.iml
13+
14+
### react ###
15+
.DS_*
16+
*.log
17+
**logs
18+
**/*.backup.*
19+
**/*.back.*
20+
21+
**node_modules
22+
**bower_components
23+
24+
*.sublime*

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ If you don't want your program to wait for a connection you could alternatively
3131
```java
3232
controller.registerConnectCallback(response -> {
3333
GetVersionResponse version = (GetVersionResponse) response;
34-
System.out.println(version.getObsStudioVersion());
34+
log.debug(version.getObsStudioVersion());
3535

3636
// Other requests...
3737
});
@@ -47,7 +47,7 @@ OBSRemoteController controller = new OBSRemoteController("ws://localhost:4444",
4747
Catch any authentication errors by registering a callback for this:
4848
```java
4949
controller.registerConnectionFailedCallback(message -> {
50-
System.err.println("Failed to connect: " + message);
50+
log.error("Failed to connect: " + message);
5151
})
5252
```
5353

@@ -66,6 +66,36 @@ A description of every request and event can be found in the plugin's [**Protoco
6666
Examples can be found [**here**](src/test/java/net/twasi/obsremotejava/test/OBSRemoteControllerTest.java). Just uncomment the requests you want to test or copy.
6767

6868
---
69+
70+
## Logging
71+
This project ships with SLF4J, and uses the SLF4J-Simple binding by default so logs are printed directly to the console.
72+
73+
If you wish to override this, for example with Logback, you must exclude SLF4J in your POM and add the dependency to the
74+
binding you want (depends on the vendor)
75+
```
76+
<dependencies>
77+
<dependency>
78+
<groupId>net.twasi</groupId>
79+
<artifactId>obs-websocket-java</artifactId>
80+
<version>1.0.6-tinatiel-1-0-0</version>
81+
<!-- Exclude the default logging implementation -->
82+
<exclusions>
83+
<exclusion>
84+
<groupId>org.slf4j</groupId>
85+
<artifactId>slf4j-simple</artifactId>
86+
</exclusion>
87+
</exclusions>
88+
</dependency>
89+
90+
<!-- Add your desired logging implementation -->
91+
<dependency>
92+
<groupId>ch.qos.logback</groupId>
93+
<artifactId>logback-classic</artifactId>
94+
<version>1.1.7</version>
95+
</dependency>
96+
</dependencies>
97+
```
98+
6999
## Contribution
70100

71101
If you miss an endpoint feel free to make a pull request. Any help is appreciated.

pom.xml

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<name>obs-websocket-java</name>
77
<groupId>net.twasi</groupId>
88
<artifactId>obs-websocket-java</artifactId>
9-
<version>1.0.6-snapshot</version>
9+
<version>1.0.6-tinatiel-1-0-0</version>
1010

1111
<description>Library to connect to the OBS WebSocket interface.</description>
1212
<url>https://github.com/Twasi/obs-websocket-java</url>
@@ -29,21 +29,27 @@
2929
</developer>
3030
</developers>
3131

32-
<scm>
33-
<connection>scm:git:git://github.com/Twasi/obs-websocket-java.git</connection>
34-
<developerConnection>scm:git:[email protected]:Twasi/obs-websocket-java.git</developerConnection>
35-
<url>https://github.com/Twasi/obs-websocket-java</url>
36-
<tag>HEAD</tag>
37-
</scm>
32+
<!-- <scm>-->
33+
<!-- <connection>scm:git:git://github.com/Twasi/obs-websocket-java.git</connection>-->
34+
<!-- <developerConnection>scm:git:[email protected]:Twasi/obs-websocket-java.git</developerConnection>-->
35+
<!-- <url>https://github.com/Twasi/obs-websocket-java</url>-->
36+
<!-- <tag>HEAD</tag>-->
37+
<!-- </scm>-->
3838

39+
<properties>
40+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41+
<java.version>8</java.version>
42+
<slf4j.version>1.7.30</slf4j.version>
43+
</properties>
3944
<build>
4045
<plugins>
4146
<plugin>
4247
<groupId>org.apache.maven.plugins</groupId>
4348
<artifactId>maven-compiler-plugin</artifactId>
49+
<version>3.8.1</version>
4450
<configuration>
45-
<source>8</source>
46-
<target>8</target>
51+
<source>${java.version}</source>
52+
<target>${java.version}</target>
4753
</configuration>
4854
</plugin>
4955
<plugin>
@@ -67,13 +73,13 @@
6773
<skip>false</skip>
6874
</configuration>
6975
<executions>
70-
<execution>
71-
<id>sign-artifacts</id>
72-
<phase>verify</phase>
73-
<goals>
74-
<goal>sign</goal>
75-
</goals>
76-
</execution>
76+
<!-- <execution>-->
77+
<!-- <id>sign-artifacts</id>-->
78+
<!-- <phase>verify</phase>-->
79+
<!-- <goals>-->
80+
<!-- <goal>sign</goal>-->
81+
<!-- </goals>-->
82+
<!-- </execution>-->
7783
</executions>
7884
</plugin>
7985
<plugin>
@@ -179,6 +185,16 @@
179185
<artifactId>gson</artifactId>
180186
<version>2.8.2</version>
181187
</dependency>
188+
<dependency>
189+
<groupId>org.slf4j</groupId>
190+
<artifactId>slf4j-api</artifactId>
191+
<version>${slf4j.version}</version>
192+
</dependency>
193+
<dependency>
194+
<groupId>org.slf4j</groupId>
195+
<artifactId>slf4j-simple</artifactId>
196+
<version>${slf4j.version}</version>
197+
</dependency>
182198
</dependencies>
183199

184200
</project>

src/main/java/net/twasi/obsremotejava/OBSCommunicator.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@
8080
import net.twasi.obsremotejava.requests.TransitionToProgram.TransitionToProgramRequest;
8181
import net.twasi.obsremotejava.requests.TransitionToProgram.TransitionToProgramResponse;
8282
import org.eclipse.jetty.websocket.api.Session;
83-
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
84-
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
85-
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
86-
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
83+
import org.eclipse.jetty.websocket.api.annotations.*;
84+
import org.slf4j.Logger;
85+
import org.slf4j.LoggerFactory;
8786

8887
import java.nio.charset.StandardCharsets;
8988
import java.security.MessageDigest;
@@ -97,6 +96,8 @@
9796

9897
@WebSocket(maxIdleTime = 360000000)
9998
public class OBSCommunicator {
99+
Logger log = LoggerFactory.getLogger(this.getClass());
100+
100101
private boolean debug;
101102
private final String password;
102103
private final CountDownLatch closeLatch;
@@ -147,7 +148,7 @@ public void await() throws InterruptedException {
147148

148149
@OnWebSocketClose
149150
public void onClose(int statusCode, String reason) {
150-
System.out.printf("Connection closed: %d - %s%n", statusCode, reason);
151+
log.info("Connection closed: %d - %s%n", statusCode, reason);
151152
this.closeLatch.countDown(); // trigger latch
152153
try {
153154
this.onDisconnect.run(null);
@@ -171,12 +172,12 @@ public void onConnect(Session session) {
171172
@OnWebSocketMessage
172173
public void onMessage(String msg) {
173174
if (msg == null) {
174-
System.out.println("Ignored empty message");
175+
log.debug("Ignored empty message");
175176
return;
176177
}
177178

178179
if (debug) {
179-
System.out.println(msg);
180+
log.debug(msg);
180181
}
181182

182183
try {
@@ -189,7 +190,7 @@ public void onMessage(String msg) {
189190
try {
190191
processIncomingResponse(responseBase, type);
191192
} catch (Throwable t) {
192-
System.err.println("Failed to process response '" + type.getSimpleName() + "' from websocket.");
193+
log.error("Failed to process response '" + type.getSimpleName() + "' from websocket.");
193194
t.printStackTrace();
194195
runOnError("Failed to process response '" + type.getSimpleName() + "' from websocket", t);
195196
}
@@ -207,13 +208,13 @@ public void onMessage(String msg) {
207208
try {
208209
processIncomingEvent(msg, eventType);
209210
} catch (Throwable t) {
210-
System.err.println("Failed to execute callback for event: " + eventType);
211+
log.error("Failed to execute callback for event: " + eventType);
211212
t.printStackTrace();
212213
runOnError("Failed to execute callback for event: " + eventType, t);
213214
}
214215
}
215216
} catch (Throwable t) {
216-
System.err.println("Failed to process message from websocket.");
217+
log.error("Failed to process message from websocket.");
217218
t.printStackTrace();
218219
runOnError("Failed to process message from websocket", t);
219220
}
@@ -223,17 +224,17 @@ private void processIncomingResponse(ResponseBase responseBase, Class type) {
223224
switch (type.getSimpleName()) {
224225
case "GetVersionResponse":
225226
versionInfo = (GetVersionResponse) responseBase;
226-
System.out.printf("Connected to OBS. Websocket Version: %s, Studio Version: %s\n", versionInfo.getObsWebsocketVersion(), versionInfo.getObsStudioVersion());
227+
log.info("Connected to OBS. Websocket Version: %s, Studio Version: %s\n", versionInfo.getObsWebsocketVersion(), versionInfo.getObsStudioVersion());
227228
session.getRemote().sendStringByFuture(new Gson().toJson(new GetAuthRequiredRequest(this)));
228229
break;
229230

230231
case "GetAuthRequiredResponse":
231232
GetAuthRequiredResponse authRequiredResponse = (GetAuthRequiredResponse) responseBase;
232233
if (authRequiredResponse.isAuthRequired()) {
233-
System.out.println("Authentication is required.");
234+
log.info("Authentication is required.");
234235
authenticateWithServer(authRequiredResponse.getChallenge(), authRequiredResponse.getSalt());
235236
} else {
236-
System.out.println("Authentication is not required. You're ready to go!");
237+
log.info("Authentication is not required. You're ready to go!");
237238
runOnConnect(versionInfo);
238239
}
239240
break;
@@ -250,15 +251,15 @@ private void processIncomingResponse(ResponseBase responseBase, Class type) {
250251
break;
251252
default:
252253
if (!callbacks.containsKey(type)) {
253-
System.out.println("Invalid type received: " + type.getName());
254+
log.warn("Invalid type received: " + type.getName());
254255
runOnError("Invalid response type received", new InvalidResponseTypeError(type.getName()));
255256
return;
256257
}
257258

258259
try {
259260
callbacks.get(type).run(responseBase);
260261
} catch (Throwable t) {
261-
System.err.println("Failed to execute callback for response: " + type);
262+
log.error("Failed to execute callback for response: " + type);
262263
t.printStackTrace();
263264
runOnError("Failed to execute callback for response: " + type, t);
264265
}
@@ -324,7 +325,7 @@ private void processIncomingEvent(String msg, EventType eventType) {
324325

325326
private void authenticateWithServer(String challenge, String salt) {
326327
if (password == null) {
327-
System.err.println("Authentication required by server but no password set by client");
328+
log.error("Authentication required by server but no password set by client");
328329
runOnConnectionFailed("Authentication required by server but no password set by client");
329330
return;
330331
}
@@ -346,7 +347,7 @@ private String generateAuthenticationResponseString(String challenge, String sal
346347
try {
347348
digest = MessageDigest.getInstance("SHA-256");
348349
} catch (NoSuchAlgorithmException e) {
349-
System.err.println("Failed to perform password authentication with server");
350+
log.error("Failed to perform password authentication with server");
350351
e.printStackTrace();
351352
runOnConnectionFailed("Failed to perform password authentication with server");
352353
return null;
@@ -443,14 +444,14 @@ public void setCurrentTransition(String transition, Callback callback) {
443444

444445
public void setSourceVisiblity(String scene, String source, boolean visibility, Callback callback) {
445446
SetSceneItemPropertiesRequest request = new SetSceneItemPropertiesRequest(this, scene, source, visibility);
446-
System.out.println(new Gson().toJson(request));
447+
log.debug(new Gson().toJson(request));
447448
session.getRemote().sendStringByFuture(new Gson().toJson(request));
448449
callbacks.put(SetSceneItemPropertiesResponse.class, callback);
449450
}
450451

451452
public void getSceneItemProperties(String scene, String source, Callback callback) {
452453
GetSceneItemPropertiesRequest request = new GetSceneItemPropertiesRequest(this, scene, source);
453-
System.out.println(new Gson().toJson(request));
454+
log.debug(new Gson().toJson(request));
454455
session.getRemote().sendStringByFuture(new Gson().toJson(request));
455456
callbacks.put(SetSceneItemPropertiesResponse.class, callback);
456457
}
@@ -469,7 +470,7 @@ public void transitionToProgram(String transitionName, int duration, Callback ca
469470

470471
public void getSourceSettings(String sourceName, Callback callback) {
471472
GetSourceSettingsRequest request = new GetSourceSettingsRequest(this, sourceName);
472-
System.out.println(new Gson().toJson(request));
473+
log.debug(new Gson().toJson(request));
473474
session.getRemote().sendStringByFuture(new Gson().toJson(request));
474475
callbacks.put(GetSourceSettingsResponse.class, callback);
475476
}
@@ -646,7 +647,7 @@ private void runOnError(String message, Throwable throwable) {
646647
try {
647648
onError.run(message, throwable);
648649
} catch (Throwable t) {
649-
System.err.println("Exception during callback execution for 'onError'");
650+
log.error("Exception during callback execution for 'onError'");
650651
t.printStackTrace();
651652
}
652653
}
@@ -659,7 +660,7 @@ private void runOnConnectionFailed(String message) {
659660
try {
660661
onConnectionFailed.run(message);
661662
} catch (Throwable t) {
662-
System.err.println("Exception during callback execution for 'onConnectionFailed'");
663+
log.error("Exception during callback execution for 'onConnectionFailed'");
663664
t.printStackTrace();
664665
}
665666
}
@@ -672,7 +673,7 @@ private void runOnConnect(GetVersionResponse versionInfo) {
672673
try {
673674
onConnect.run(versionInfo);
674675
} catch (Throwable t) {
675-
System.err.println("Exception during callback execution for 'onConnect'");
676+
log.error("Exception during callback execution for 'onConnect'");
676677
t.printStackTrace();
677678
}
678679
}

0 commit comments

Comments
 (0)