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

Small refactoring #21

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
petebankhead marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be better to avoid generating a new LogMessage, and ensure that the text is made lowercase outside of the lambda. Suggested (untested!):

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);
}

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