Skip to content

Commit

Permalink
Merge pull request #21 from Rylern/main
Browse files Browse the repository at this point in the history
Small refactoring
  • Loading branch information
petebankhead authored May 2, 2023
2 parents 36481d5 + 32fb964 commit 8f8d590
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 125 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ javafx {
}

application {
mainModule = 'demo'
mainModule = 'logviewer'
mainClass = 'io.github.qupath.logviewer.LogViewerApp'
}

Expand All @@ -60,6 +60,6 @@ tasks.named('test') {
jlink {
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'demo'
name = 'logviewer'
}
}
1 change: 0 additions & 1 deletion src/main/java/io/github/qupath/logviewer/LogMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public record LogMessage(
String message,
Throwable throwable
) {

public String toReadableString() {
String readableString = level.toString() + "\t" + threadName + "\t" + loggerName + "\t" + new Date(timestamp) + "\t" + message;

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/io/github/qupath/logviewer/LogMessagePredicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.qupath.logviewer;

import java.util.regex.Pattern;
import java.util.function.Predicate;
import java.util.regex.PatternSyntaxException;

public class LogMessagePredicates {
public static Predicate<LogMessage> createPredicateFromRegex(String regex) {
if (regex == null || regex.isEmpty())
return logMessage -> true;

try {
Pattern pattern = Pattern.compile(regex);
return logMessage -> pattern.matcher(logMessage.message()).find();
} catch (PatternSyntaxException e) {
return logMessage -> false;
}
}

public static Predicate<LogMessage> createPredicateContains(String text) {
if (text == null || text.isEmpty())
return logMessage -> true;

return logMessage -> logMessage.message().contains(text);
}

public static Predicate<LogMessage> createPredicateContainsIgnoreCase(String text) {
if (text == null || text.isEmpty())
return logMessage -> true;

String textLower = text.toLowerCase();
return logMessage -> logMessage.message().toLowerCase().contains(textLower);
}
}
32 changes: 28 additions & 4 deletions src/main/java/io/github/qupath/logviewer/LogViewerApp.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.github.qupath.logviewer;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

import java.util.Objects;
import java.util.ResourceBundle;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class LogViewerApp extends Application {
private final static Logger logger = LoggerFactory.getLogger(LogViewerApp.class);
Expand All @@ -28,14 +32,10 @@ public void start(Stage primaryStage) throws Exception {
}

ResourceBundle resources = ResourceBundle.getBundle("io.github.qupath.logviewer.strings");

FXMLLoader loader = new FXMLLoader(url, resources);
loader.load();

Parent root = loader.getRoot();

Scene scene = new Scene(root);

String stylesheet = Objects.requireNonNull(getClass().getResource("css/styles.css")).toExternalForm();
scene.getStylesheets().add(stylesheet);

Expand All @@ -44,6 +44,16 @@ public void start(Stage primaryStage) throws Exception {
primaryStage.show();

scene.addEventHandler(MouseEvent.ANY, LogViewerApp::logMouseEvent);

logger.info("Here's my first log message, for information");
try {
throw new RuntimeException("Here is a runtime exception");
} catch (Exception e) {
logger.error("Exception", e);
}
Platform.runLater(() -> logRandomMessages(1000));
logRandomMessages(1000);
logger.warn("Here's a final message. With a warning.");
}

private static void logMouseEvent(MouseEvent event) {
Expand All @@ -53,4 +63,18 @@ private static void logMouseEvent(MouseEvent event) {
}
logger.info("Mouse event: {} at ({}, {})", event.getEventType(), event.getX(), event.getY());
}

private static void logRandomMessages(int maxMessages) {
IntStream.range(0, maxMessages)
.parallel()
.forEach(LogViewerApp::logSingleRandomMessage);
}

private static void logSingleRandomMessage(int index) {
Level[] allLogLevels = Level.values();
ThreadLocalRandom random = ThreadLocalRandom.current();
Level level = allLogLevels[random.nextInt(allLogLevels.length)];
logger.atLevel(level)
.log("This is a test message {}", index);
}
}
Loading

0 comments on commit 8f8d590

Please sign in to comment.