Skip to content

Commit 7fffdbc

Browse files
authored
Merge pull request #27 from psobiech/feature/slf4j
* Add SLF4J to replace logging to stdout closes #25
2 parents db59374 + f33db5a commit 7fffdbc

File tree

13 files changed

+113
-20
lines changed

13 files changed

+113
-20
lines changed

core/pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
@@ -11,10 +12,21 @@
1112
<artifactId>javacan-core</artifactId>
1213

1314
<dependencies>
15+
<dependency>
16+
<groupId>org.slf4j</groupId>
17+
<artifactId>slf4j-api</artifactId>
18+
</dependency>
19+
1420
<dependency>
1521
<groupId>tel.schich</groupId>
1622
<artifactId>jni-access-generator</artifactId>
1723
</dependency>
24+
25+
<dependency>
26+
<groupId>ch.qos.logback</groupId>
27+
<artifactId>logback-classic</artifactId>
28+
<scope>test</scope>
29+
</dependency>
1830
</dependencies>
1931

2032
<build>
@@ -47,6 +59,11 @@
4759
<goals>
4860
<goal>test-jar</goal>
4961
</goals>
62+
<configuration>
63+
<excludes>
64+
<excludes>logback-test.xml</excludes>
65+
</excludes>
66+
</configuration>
5067
</execution>
5168
</executions>
5269
</plugin>

core/src/main/java/tel/schich/javacan/platform/Platform.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import tel.schich.javacan.JavaCAN;
2626

27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2729
import java.io.IOException;
2830
import java.io.InputStream;
2931
import java.nio.file.Files;
@@ -35,6 +37,8 @@
3537
* Helper class to detect and handle various platforms. Currently only Linux is handled.
3638
*/
3739
public class Platform {
40+
private static final Logger LOGGER = LoggerFactory.getLogger(Platform.class);
41+
3842
private static final String LIB_PREFIX = "/native";
3943

4044
/**
@@ -73,6 +77,8 @@ public static void loadBundledLib(String name) {
7377
}
7478

7579
final String sourceLibPath = LIB_PREFIX + "/lib" + name + "-" + archSuffix + ".so";
80+
LOGGER.trace("Loading native library for arch {} from {}", arch, sourceLibPath);
81+
7682
try (InputStream libStream = JavaCAN.class.getResourceAsStream(sourceLibPath)) {
7783
if (libStream == null) {
7884
throw new LinkageError("Failed to load the native library: " + sourceLibPath + " not found.");

core/src/test/java/tel/schich/javacan/test/isotp/IsotpCanSocketOptionsTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package tel.schich.javacan.test.isotp;
2424

2525
import org.junit.jupiter.api.Test;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628
import tel.schich.javacan.*;
2729

2830
import java.io.IOException;
@@ -35,25 +37,26 @@
3537
import static tel.schich.javacan.test.CanTestHelper.CAN_INTERFACE;
3638

3739
public class IsotpCanSocketOptionsTest {
40+
private static final Logger LOGGER = LoggerFactory.getLogger(IsotpCanSocketOptionsTest.class);
3841

3942
@Test
4043
void testOptions() throws IOException {
4144
try (final IsotpCanChannel a = CanChannels.newIsotpChannel()) {
4245
IsotpOptions opts = a.getOption(OPTS);
43-
System.out.println(opts);
46+
LOGGER.debug(String.valueOf(opts));
4447
IsotpOptions customizedOpts = IsotpOptions.DEFAULT.withPadding(0xAA);
4548
a.setOption(OPTS, customizedOpts);
4649
assertEquals(customizedOpts, a.getOption(OPTS), "What goes in should come out");
4750

4851
IsotpFlowControlOptions flowControlOpts = a.getOption(RECV_FC);
49-
System.out.println(flowControlOpts);
52+
LOGGER.debug(String.valueOf(flowControlOpts));
5053
IsotpFlowControlOptions customizedFlowControlOpts = IsotpFlowControlOptions.DEFAULT
5154
.withBlockSize(1);
5255
a.setOption(RECV_FC, customizedFlowControlOpts);
5356
assertEquals(customizedFlowControlOpts, a.getOption(RECV_FC), "What goes in should come out");
5457

5558
IsotpLinkLayerOptions linkLayerOpts = a.getOption(LL_OPTS);
56-
System.out.println(linkLayerOpts);
59+
LOGGER.debug(String.valueOf(linkLayerOpts));
5760
IsotpLinkLayerOptions customizedLinkLayerOpts = IsotpLinkLayerOptions.DEFAULT
5861
.withTransmissionFlags(CanFrame.FD_FLAG_BIT_RATE_SWITCH);
5962
a.setOption(LL_OPTS, customizedLinkLayerOpts);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[%level] %logger{16} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="INFO">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
</configuration>

epoll/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
@@ -20,6 +21,12 @@
2021
<artifactId>javacan-core</artifactId>
2122
<version>${project.parent.version}</version>
2223
</dependency>
24+
25+
<dependency>
26+
<groupId>ch.qos.logback</groupId>
27+
<artifactId>logback-classic</artifactId>
28+
<scope>test</scope>
29+
</dependency>
2330
<dependency>
2431
<groupId>${project.parent.groupId}</groupId>
2532
<artifactId>javacan-core</artifactId>

epoll/src/main/java/tel/schich/javacan/util/CanBroker.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package tel.schich.javacan.util;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527
import java.io.IOException;
2628
import java.net.SocketOption;
2729
import java.nio.ByteBuffer;
@@ -56,6 +58,7 @@
5658
* Frames can be send either to individual interfaces or all at once.
5759
*/
5860
public class CanBroker extends EventLoop<UnixFileDescriptor, RawCanChannel> {
61+
private static final Logger LOGGER = LoggerFactory.getLogger(CanBroker.class);
5962

6063
public static final Duration DEFAULT_TIMEOUT = ofMinutes(1);
6164
private static final CanFilter[] NO_FILTERS = { CanFilter.NONE };
@@ -271,10 +274,10 @@ protected void processEvents(List<IOEvent<UnixFileDescriptor>> events) throws IO
271274
readBuffer.clear();
272275
handler.handle(raw, raw.read(readBuffer));
273276
} else {
274-
System.err.println("Handler not found for channel: " + ch);
277+
LOGGER.warn("Handler not found for channel: " + ch);
275278
}
276279
} else {
277-
System.err.println("Unsupported channel: " + ch);
280+
LOGGER.warn("Unsupported channel: " + ch);
278281
}
279282
}
280283
}

epoll/src/main/java/tel/schich/javacan/util/EventLoop.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package tel.schich.javacan.util;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527
import tel.schich.javacan.select.IOEvent;
2628
import tel.schich.javacan.select.IOSelector;
2729
import tel.schich.javacan.select.SelectorRegistration;
@@ -39,6 +41,8 @@
3941
import java.util.concurrent.ThreadFactory;
4042

4143
public abstract class EventLoop<HandleType, ChannelType extends Channel> implements Closeable {
44+
private static final Logger LOGGER = LoggerFactory.getLogger(CanBroker.class);
45+
4246
private final String name;
4347

4448
private final ThreadFactory threadFactory;
@@ -203,14 +207,15 @@ protected final boolean poll(Duration timeout) throws IOException {
203207
* @return true if the event loop should continue, false for the event loop to exit
204208
*/
205209
protected boolean handleException(Thread thread, Throwable t, boolean terminal) {
206-
System.err.println("Polling thread failed: " + thread.getName());
207-
t.printStackTrace(System.err);
208-
System.err.println("Terminating other threads.");
210+
LOGGER.error("Polling thread failed: " + thread.getName(), t);
211+
LOGGER.warn("Terminating other threads.");
212+
209213
try {
210214
shutdown();
211215
} catch (InterruptedException e) {
212-
System.err.println("Got interrupted while stopping the threads");
216+
LOGGER.error("Got interrupted while stopping the threads");
213217
}
218+
214219
return true;
215220
}
216221

epoll/src/main/java/tel/schich/javacan/util/IsotpListener.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package tel.schich.javacan.util;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527
import java.io.IOException;
2628
import java.nio.ByteBuffer;
2729
import java.nio.channels.Channel;
@@ -43,6 +45,8 @@
4345
* for for each specific channel.
4446
*/
4547
public class IsotpListener extends EventLoop<UnixFileDescriptor, IsotpCanChannel> {
48+
private static final Logger LOGGER = LoggerFactory.getLogger(CanBroker.class);
49+
4650
private final ByteBuffer readBuffer = IsotpCanChannel.allocateSufficientMemory();
4751

4852
private final IdentityHashMap<IsotpCanChannel, MessageHandler> handlerMap = new IdentityHashMap<>();
@@ -122,10 +126,10 @@ protected void processEvents(List<IOEvent<UnixFileDescriptor>> events) throws IO
122126
readBuffer.flip();
123127
handler.handle(isotp, readBuffer.asReadOnlyBuffer());
124128
} else {
125-
System.err.println("Handler not found for channel: " + ch);
129+
LOGGER.warn("Handler not found for channel: " + ch);
126130
}
127131
} else {
128-
System.err.println("Unsupported channel: " + ch);
132+
LOGGER.warn("Unsupported channel: " + ch);
129133
}
130134
}
131135
}

epoll/src/test/java/tel/schich/javacan/test/select/EPollSelectorTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.api.function.Executable;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2729
import tel.schich.javacan.CanChannels;
2830
import tel.schich.javacan.CanFrame;
2931
import tel.schich.javacan.CanSocketOptions;
@@ -48,6 +50,7 @@
4850
import static tel.schich.javacan.test.CanTestHelper.runDelayed;
4951

5052
public class EPollSelectorTest {
53+
private static final Logger LOGGER = LoggerFactory.getLogger(EPollSelectorTest.class);
5154

5255
@Test
5356
public void testOpenClose() throws IOException {
@@ -128,12 +131,12 @@ public void testEPollFdReuse() throws IOException, InterruptedException {
128131

129132
private static SelectorRegistration<UnixFileDescriptor, RawCanChannel> configureAndRegisterChannel(IOSelector<UnixFileDescriptor> selector) throws IOException {
130133
final RawCanChannel ch = CanChannels.newRawChannel(CAN_INTERFACE);
131-
System.out.println("Created channel: " + ch);
134+
LOGGER.debug("Created channel: " + ch);
132135

133136
ch.configureBlocking(false);
134137
ch.setOption(CanSocketOptions.LOOPBACK, true);
135138
SelectorRegistration<UnixFileDescriptor, RawCanChannel> registration = selector.register(ch, SelectorRegistration.Operation.READ);
136-
System.out.println("Selection key: " + registration);
139+
LOGGER.debug("Selection key: " + registration);
137140

138141
return registration;
139142
}

epoll/src/test/java/tel/schich/javacan/test/util/CanBrokerTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import org.junit.Assert;
2626
import org.junit.jupiter.api.Test;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2729
import tel.schich.javacan.CanFilter;
2830
import tel.schich.javacan.CanFrame;
2931
import tel.schich.javacan.platform.linux.epoll.EPollSelector;
@@ -39,6 +41,8 @@
3941
import static org.junit.jupiter.api.Assertions.assertNotNull;
4042

4143
class CanBrokerTest {
44+
private static final Logger LOGGER = LoggerFactory.getLogger(CanBrokerTest.class);
45+
4246
private static final ThreadFactory FACTORY = r -> {
4347
Thread t = new Thread(r);
4448
t.setName("can-broker-test" + Math.random());
@@ -68,7 +72,7 @@ void testLoopback() throws Exception {
6872
});
6973

7074
brokerB.addDevice(CanTestHelper.CAN_INTERFACE, (d, frame) -> {
71-
System.out.println(frame);
75+
LOGGER.debug(String.valueOf(frame));
7276
});
7377
brokerB.send(expected);
7478

epoll/src/test/java/tel/schich/javacan/test/util/IsotpListenerTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package tel.schich.javacan.test.util;
2424

2525
import org.junit.jupiter.api.Test;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628
import tel.schich.javacan.CanChannels;
2729
import tel.schich.javacan.IsotpCanChannel;
2830
import tel.schich.javacan.IsotpSocketAddress;
@@ -45,6 +47,7 @@
4547
import static tel.schich.javacan.IsotpAddress.*;
4648

4749
class IsotpListenerTest {
50+
private static final Logger LOGGER = LoggerFactory.getLogger(IsotpListenerTest.class);
4851

4952
@Test
5053
void testBroker() throws Exception {
@@ -105,8 +108,7 @@ public PingPing(Lock lock, Condition condition, ByteBuffer buf) {
105108
public void handle(IsotpCanChannel ch, ByteBuffer buffer) {
106109
int length = buffer.remaining();
107110
if (length % 200 == 0) {
108-
System.out.printf("(%04d) -> %08X#%s%n", length, ch.getTxAddress().getId(), CanUtils.hexDump(buffer));
109-
System.out.flush();
111+
LOGGER.debug(String.format("(%04d) -> %08X#%s%n", length, ch.getTxAddress().getId(), CanUtils.hexDump(buffer)));
110112
}
111113
buf.clear();
112114
buf.put(buffer);
@@ -116,8 +118,7 @@ public void handle(IsotpCanChannel ch, ByteBuffer buffer) {
116118
ch.write(buf);
117119
} catch (Exception e) {
118120
assertEquals(IllegalArgumentException.class, e.getClass());
119-
System.err.println("Failed to send message:");
120-
e.printStackTrace(System.err);
121+
LOGGER.debug("Failed to send message", e);
121122
lock.lock();
122123
try {
123124
condition.signalAll();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[%level] %logger{16} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="INFO">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
</configuration>

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@
6060
<properties>
6161
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6262
<headers.target>${project.build.directory}/jni</headers.target>
63+
64+
<slf4j.version>1.7.30</slf4j.version>
65+
<logback.version>1.2.3</logback.version>
66+
6367
<junit.minor>2</junit.minor>
6468
<junit.version>5.${junit.minor}.0</junit.version>
6569
<junit.runner.version>1.${junit.minor}.0</junit.runner.version>
70+
6671
<releaseProfile>release-javacan</releaseProfile>
6772
</properties>
6873

@@ -74,6 +79,19 @@
7479
<version>1.1.0</version>
7580
<scope>provided</scope>
7681
</dependency>
82+
83+
<dependency>
84+
<groupId>org.slf4j</groupId>
85+
<artifactId>slf4j-api</artifactId>
86+
<version>${slf4j.version}</version>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>ch.qos.logback</groupId>
91+
<artifactId>logback-classic</artifactId>
92+
<version>${logback.version}</version>
93+
<scope>test</scope>
94+
</dependency>
7795
</dependencies>
7896
</dependencyManagement>
7997

0 commit comments

Comments
 (0)