Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

69: improve internal logging #77

Merged
merged 13 commits into from
Oct 14, 2024
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<maven.build.helper.plugin.version>3.6.0</maven.build.helper.plugin.version>
<maven.shade.plugin.version>3.6.0</maven.shade.plugin.version>
<pi4j.version>2.7.0-SNAPSHOT</pi4j.version>
<slf4j.version>2.0.13</slf4j.version>
<slf4j.version>2.0.16</slf4j.version>
<nexus.staging.version>1.6.7</nexus.staging.version>

<!-- Test -->
Expand Down
9 changes: 9 additions & 0 deletions robo4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
</parent>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
175 changes: 88 additions & 87 deletions robo4j-core/src/main/java/com/robo4j/RoboApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/
package com.robo4j;

import com.robo4j.logging.SimpleLoggingUtil;
import com.robo4j.scheduler.RoboThreadFactory;
import com.robo4j.util.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -37,91 +38,91 @@
* @author Miroslav Wengner (@miragemiko)
*/
public final class RoboApplication {


private static final class ShutdownThread extends Thread {

private final CountDownLatch latch;

private ShutdownThread(CountDownLatch latch) {
this.latch = latch;
}

@Override
public void run() {
latch.countDown();
}
}

private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1,
new RoboThreadFactory(new ThreadGroup("Robo4J-Launcher"), "Robo4J-App-", false));
private static final CountDownLatch appLatch = new CountDownLatch(1);


public RoboApplication() {
}

/**
* the method is called by standalone robo launcher.
*
* @param context
* robo context
*/
public void launch(RoboContext context) {
// Create a new Launcher thread and then wait for that thread to finish
Runtime.getRuntime().addShutdownHook(new ShutdownThread(appLatch));
try {
// TODO : review, exception runtime?
Thread daemon = new Thread(() -> {
try {
System.in.read();
appLatch.countDown();
} catch (IOException e) {
e.printStackTrace();
}

});
daemon.setName("Robo4J-Launcher-listener");
daemon.setDaemon(true);
daemon.start();
context.start();

String logo = getBanner(Thread.currentThread().getContextClassLoader());
SimpleLoggingUtil.info(getClass(), logo);
SimpleLoggingUtil.info(RoboApplication.class, SystemUtil.printStateReport(context));
SimpleLoggingUtil.info(RoboApplication.class, "Press <Enter>...");
appLatch.await();
SimpleLoggingUtil.info(RoboApplication.class,"Going down...");
context.shutdown();
SimpleLoggingUtil.info(RoboApplication.class,"Bye!");
} catch (InterruptedException e) {
throw new RoboApplicationException("unexpected", e);
}
}

public void launchWithExit(RoboContext context, long delay, TimeUnit timeUnit) {
executor.schedule(() -> Runtime.getRuntime().exit(0), delay, timeUnit);
launch(context);

}

public void launchNoExit(RoboContext context, long delay, TimeUnit timeUnit) {
executor.schedule(appLatch::countDown, delay, timeUnit);
launch(context);
}

private String getBanner(ClassLoader classLoader){
final InputStream is = classLoader.getResourceAsStream("banner.txt");
final byte[] logoBytes;
try {
logoBytes = is == null ? new byte[0] : is.readAllBytes();
} catch (IOException e) {
throw new IllegalStateException("not allowed");
}

return new StringBuilder().append(BREAK).append(DELIMITER_HORIZONTAL)
.append(new String(logoBytes)).append(BREAK).append(DELIMITER_HORIZONTAL)
.toString();
}
private static final Logger LOGGER = LoggerFactory.getLogger(RoboApplication.class);

private static final class ShutdownThread extends Thread {

private final CountDownLatch latch;

private ShutdownThread(CountDownLatch latch) {
this.latch = latch;
}

@Override
public void run() {
latch.countDown();
}
}

private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1,
new RoboThreadFactory(new ThreadGroup("Robo4J-Launcher"), "Robo4J-App-", false));
private static final CountDownLatch appLatch = new CountDownLatch(1);


public RoboApplication() {
}

/**
* the method is called by standalone robo launcher.
*
* @param context robo context
*/
public void launch(RoboContext context) {
// Create a new Launcher thread and then wait for that thread to finish
Runtime.getRuntime().addShutdownHook(new ShutdownThread(appLatch));
try {
// TODO : review, exception runtime?
Thread daemon = new Thread(() -> {
try {
System.in.read();
appLatch.countDown();
} catch (IOException e) {
LOGGER.error("launch", e);
}

});
daemon.setName("Robo4J-Launcher-listener");
daemon.setDaemon(true);
daemon.start();
context.start();

String logo = getBanner(Thread.currentThread().getContextClassLoader());
LOGGER.info(logo);
LOGGER.info(SystemUtil.printStateReport(context));
LOGGER.info("Press <Enter>...");
// TODO : introduce timeout
appLatch.await();
LOGGER.info("Going down...");
context.shutdown();
LOGGER.info("Bye!");
} catch (InterruptedException e) {
throw new RoboApplicationException("unexpected", e);
}
}

public void launchWithExit(RoboContext context, long delay, TimeUnit timeUnit) {
executor.schedule(() -> Runtime.getRuntime().exit(0), delay, timeUnit);
launch(context);

}

public void launchNoExit(RoboContext context, long delay, TimeUnit timeUnit) {
executor.schedule(appLatch::countDown, delay, timeUnit);
launch(context);
}

private String getBanner(ClassLoader classLoader) {
final InputStream is = classLoader.getResourceAsStream("banner.txt");
final byte[] logoBytes;
try {
logoBytes = is == null ? new byte[0] : is.readAllBytes();
} catch (IOException e) {
throw new IllegalStateException("not allowed");
}

return new StringBuilder().append(BREAK).append(DELIMITER_HORIZONTAL)
.append(new String(logoBytes)).append(BREAK).append(DELIMITER_HORIZONTAL)
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
*/
package com.robo4j;

import java.io.Serial;

/**
* RoboApplicationException {@link RoboApplication}
*
* @author Marcus Hirt (@hirt)
* @author Miroslav Wengner (@miragemiko)
*/
public class RoboApplicationException extends RuntimeException {
private static final long serialVersionUID = 1L;
@Serial
private static final long serialVersionUID = 1L;

public RoboApplicationException(String message, Throwable cause) {
public RoboApplicationException(String message, Throwable cause) {
super(message, cause);
}
}
Loading
Loading