Skip to content

Commit

Permalink
added some very basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
grillbaer committed Nov 1, 2021
1 parent bf11ed3 commit f3f732f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions spectracle-app/src/main/java/grillbaer/spectracle/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import grillbaer.spectracle.camera.Camera;
import grillbaer.spectracle.ui.MainPanel;
import nu.pattern.OpenCV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.swing.*;
Expand All @@ -12,12 +14,24 @@
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Application entry point.
*/
public class Main {
private final static Logger LOG = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) throws InterruptedException, InvocationTargetException {
LOG.info("Spectracle starting up ...");
LOG.info("OS Name: {}", System.getProperty("os.name"));
LOG.info("OS Arch: {}", System.getProperty("os.arch"));
LOG.info("OS Version: {}", System.getProperty("os.version"));
LOG.info("Java Version: {}", System.getProperty("java.version"));

Thread.setDefaultUncaughtExceptionHandler((t, e) -> LOG.error("Uncaught exception from thread {}", t.getName(), e));

LOG.info("Initializing OpenCV ...");
OpenCV.loadLocally();

final var context = new Context();
Expand All @@ -28,20 +42,21 @@ public static void main(String[] args) throws InterruptedException, InvocationTa
context.getModel().setCameraPaused(false);

SwingUtilities.invokeAndWait(() -> {
LOG.info("UI starting up ...");
FlatDarkLaf.install();
final var mainPanel = new MainPanel(context);
final var frame = new JFrame("Spectracle");
frame.setIconImages(getWindowIcons());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel.getComponent(), BorderLayout.CENTER);
setDefaultWindowBounds(frame);
LOG.info("Setting main window visible");
frame.setVisible(true);
});
}

private static List<? extends Image> getWindowIcons() {
return List.of(16, 24, 32, 48, 64, 128, 256)
.stream()
return Stream.of(16, 24, 32, 48, 64, 128, 256)
.map(size -> {
try {
return ImageIO.read(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class Camera implements Closeable {
}

public Camera(int id) {
LOG.info("Camera id={}: opening ...", id);
this.id = id;
this.videoCapture = new VideoCapture(id);

Expand Down Expand Up @@ -127,4 +128,9 @@ private String formatPropValue(int propId, double value) {
return String.format(Locale.ROOT, "%-40s %5d = %6.1f",
PROP_NAMES_BY_ID.get(propId), propId, value);
}

@Override
public String toString() {
return "Camera[" + "id=" + this.id + ", cameraProps=" + this.cameraProps + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.util.*;
Expand All @@ -22,6 +24,8 @@
*/
@Getter
public final class Model {
private final static Logger LOG = LoggerFactory.getLogger(Model.class);

private Camera camera;
private final Observers<Camera> cameraObservers = new Observers<>();

Expand Down Expand Up @@ -94,6 +98,7 @@ private void updateSampleLineFromGrabbedFrame() {
*/
public void setCamera(Camera camera) {
if (this.camera != camera) {
LOG.info("Setting camera {}", camera);
this.camera = camera;
final var cameraProps = getCameraProps();
if (cameraProps != null) {
Expand Down
34 changes: 34 additions & 0 deletions spectracle-app/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<configuration>

<property name="LOG_DIR" value="${java.io.tmpdir}/spectracle"/>
<property name="PATTERN" value="%date{ISO8601,UTC} %-5level [%-20thread] %-36logger{35} - %msg%n"/>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/spectracle.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_DIR}/spectracle.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>2</maxIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>

</configuration>

0 comments on commit f3f732f

Please sign in to comment.