Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

XIVY-3061 #11

Merged
merged 14 commits into from
Mar 6, 2019
Merged
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.axonivy.ivy.supplements</groupId>
<artifactId>ivy-log-viewer</artifactId>
<version>0.7.1</version>
<version>0.7.2</version>
<packaging>jar</packaging>

<name>ivy-log-viewer</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@

public class ExceptionDialog {
public void showException(Exception ex) {
showException(ex, null);
}

public void showException(Exception ex, String msg){
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception");
alert.setHeaderText("An exception occured");
alert.setContentText(ex.getMessage());

if(!msg.isEmpty()){
alert.setHeaderText("An exception occured" + "\n" + msg);
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void main(String[] args) {
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/ivylogviewer.fxml"));

Scene scene = new Scene(root, 640, 320);
Scene scene = new Scene(root, 800, 320);

stage.setTitle("IvyLogViewer");
stage.getIcons().add(new Image("/images/icon.png"));
Expand Down
149 changes: 127 additions & 22 deletions src/main/java/com/axonivy/ivy/supplements/logviewer/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;

Expand Down Expand Up @@ -70,11 +73,19 @@ public class MainController implements Initializable {

private List<MainLogEntry> logEntries;

private File currentFile;
private List<File> currentFiles;

@FXML
private Button applyNewFilter;

@FXML
private Button searchButton;

@FXML
private TextField startTimeField;
@FXML
private TextField endTimeField;

@FXML
private TextField searchField;

Expand All @@ -91,6 +102,7 @@ public void initialize(URL arg0, ResourceBundle arg1) {
configureDragAndDrop();
configureMinLogLevelSelection();
configureSearch();
configureFilter();
configureSelectionMode();
addCtrlCSupport();
}
Expand Down Expand Up @@ -252,21 +264,37 @@ private void configureSearch() {
});
}

private void configureFilter() {
startTimeField.setPromptText("StartTime");
endTimeField.setPromptText("EndTime");
applyNewFilter.setOnAction(event -> {
if (validateTimeString(startTimeField.getText(), endTimeField.getText())) {
List<MainLogEntry> filteredEntries = filterByTime(startTimeField.getText(), endTimeField.getText(),
logEntries);
displayFilteredEntries(filteredEntries);
setMinAndMaxTimeToTextboxes(filteredEntries);
}
});
}

private void configureMinLogLevelSelection() {
minimalLevel.getItems().addAll(FXCollections.observableArrayList(LogLevel.valuesDesc()));

minimalLevel.setOnAction(event -> {
selectedLogLevel = minimalLevel.getValue();
clearSearch();
displayLogEntries();
if (validateTimeString(startTimeField.getText(), endTimeField.getText())) {
displayFilteredEntries(filterByTime(startTimeField.getText(), endTimeField.getText(), logEntries));
} else {
displayLogEntries();
}
});

minimalLevel.setValue(selectedLogLevel);
}

private void clearSearch() {
textToSearch = "";
searchField.setText(textToSearch);
displayLogEntries();
}

private void buildMenu() {
Expand Down Expand Up @@ -317,37 +345,40 @@ private void configureDragAndDrop() {
boolean success = false;
if (dragboard.hasFiles()) {
success = true;
openFile(dragboard.getFiles().get(0));
List<File> files = dragboard.getFiles();
openFiles(files);
event.setDropCompleted(success);
event.consume();
}
event.setDropCompleted(success);
event.consume();
});
}

private void openFileDialog() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Log File");
fileChooser.setTitle("Open Log File(s)");

Stage stage = (Stage) treeAnchorPane.getScene().getWindow();
currentFile = fileChooser.showOpenDialog(stage);

if (currentFile != null) {
openFile(currentFile);
currentFiles = fileChooser.showOpenMultipleDialog(stage);
if (!currentFiles.isEmpty()) {
openFiles(currentFiles);
}
}

private void openFile(File file) {
if (file == null) {
private void openFiles(List<File> files) {
if (files.isEmpty()) {
return;
}
LogFileParser logFileParser = new LogFileParser(file);
LogFileParser logFileParser = new LogFileParser(files);
try {
logEntries = logFileParser.parse();
displayLogEntries();
filepathLabel.setText(file.getAbsolutePath());
} catch (Exception ex) {
new ExceptionDialog().showException(ex);
} catch (IOException e) {
new ExceptionDialog().showException(e);
e.printStackTrace();
}
for (File file : files) {
filepathLabel.setText(filepathLabel.getText() + " " + file.getAbsolutePath());
}
displayLogEntries();
}

private void displayLogEntries() {
Expand All @@ -359,14 +390,14 @@ private void displayLogEntries() {
if (logEntries == null) {
return;
}

orderEntryListByChronology(logEntries);
for (MainLogEntry entry : logEntries) {
LogLevel logLevel = selectedLogLevel;
if (entry.getSeverity().ordinal() < logLevel.ordinal()) {
continue;
}

if (!textToSearch.equals("")) {
if (!textToSearch.isEmpty()) {
if (!entry.getDetails().toLowerCase().contains(textToSearch.toLowerCase())) {
continue;
}
Expand All @@ -378,18 +409,35 @@ private void displayLogEntries() {
TreeItem<Object> detailItem = new TreeItem<Object>(entry.getDetails());
item.getChildren().add(detailItem);
}
setMinAndMaxTimeToTextboxes(logEntries);
rootItem.getChildren().add(item);
}
}

private MainLogEntry getLastMainLogEntry(List<MainLogEntry> logEntries) {
return logEntries.stream()
.reduce((first, second) -> second)
.get();
}

private MainLogEntry getFirstMainLogEntry(List<MainLogEntry> logEntries) {
return logEntries.stream()
.findFirst()
.orElse(null);
}

private void setMinAndMaxTimeToTextboxes(List<MainLogEntry> logEntries) {
endTimeField.setText(getLastMainLogEntry(logEntries).getTime());
startTimeField.setText(getFirstMainLogEntry(logEntries).getTime());
}

private void copySelectionToClipboard() {
String selectedEntries = new String();
for (TreeItem<Object> selectedEntry : logTreeView.getSelectionModel().getSelectedItems()) {
if (!(selectedEntry.getValue() instanceof MainLogEntry)) {
selectedEntries = selectedEntries.concat(selectedEntry.getValue() + "\n");
}
}

StringSelection selection = new StringSelection(selectedEntries);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
Expand All @@ -398,4 +446,61 @@ private void copySelectionToClipboard() {
private static ImageView getIcon(LogLevel level) {
return IconUtil.getIcon(level);
}

public static List<MainLogEntry> filterByTime(String start, String end, List<MainLogEntry> logEntries) {
LocalTime startTime = LocalTime.parse(start);
LocalTime endTime = LocalTime.parse(end);
if (startTime.isAfter(endTime) || endTime.isBefore(startTime)) {
startTime = LocalTime.parse(end);
endTime = LocalTime.parse(start);
}
List<MainLogEntry> filteredEntries = new ArrayList<>();
for (MainLogEntry entry : logEntries) {
LocalTime entryTime = LocalTime.parse(entry.getTime());
if ((!entryTime.isBefore(startTime)) && (!entryTime.isAfter(endTime))) {
filteredEntries.add(entry);
}
}
return filteredEntries;
}

private void displayFilteredEntries(List<MainLogEntry> filteredEntries) {
TreeItem<Object> rootItem = new TreeItem<Object>(new MainLogEntry("All", "All", LogLevel.DEBUG));
logTreeView.setRoot(rootItem);

for (MainLogEntry entry : filteredEntries) {
LogLevel logLevel = selectedLogLevel;
if (entry.getSeverity().ordinal() < logLevel.ordinal()) {
continue;
}
TreeItem<Object> item = new TreeItem<Object>(entry, getIcon(entry.getSeverity()));
if (entry.getDetailLogEntry() != null) {
TreeItem<Object> detailItem = new TreeItem<Object>(entry.getDetails());
item.getChildren().add(detailItem);
}
rootItem.getChildren().add(item);
}

}

public static List<MainLogEntry> orderEntryListByChronology(List<MainLogEntry> logEntryList) {
Collections.sort(logEntryList);
return logEntryList;
}

public static Boolean validateTimeString(String time1, String time2) {
if (!time1.isEmpty() && !time2.isEmpty()) {
try {
LocalTime.parse(time1);
LocalTime.parse(time2);
return true;
} catch (Exception e) {
new ExceptionDialog().showException(e, "Please enter a valid timeformat (HH:mm:ss or HH:mm:ss.sss)");
return false;
}
} else {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class DetailLogEntry {

private MainLogEntry parentEntry;
private String detailText;

public DetailLogEntry(MainLogEntry parentEntry, String detailText) {
this.parentEntry = parentEntry;
this.detailText = detailText;
Expand All @@ -17,11 +17,11 @@ public MainLogEntry getParentEntry() {
public void addDetailLine(String detailLine) {
detailText += "\n" + detailLine;
}

public String getDetailText() {
return detailText + "\n";
}

@Override
public String toString() {
return getDetailText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
import java.util.stream.Stream;

public class LogFileParser {
private File file;
private List<MainLogEntry> logEntries = new ArrayList<>();

public LogFileParser(File file) {
this.file = file;
private List<File> files;

public LogFileParser(List<File> files) {
this.files = files;
}

public List<MainLogEntry> parse() throws IOException {
// TODO check if parsing like that is performant enough, else only read headers
// TODO also support other encodings
try (Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.ISO_8859_1)) {
lines.forEachOrdered(line -> {

if (!line.startsWith(" ")) {
createNewEntry(line);
} else {
appendToLastEntry(line);
}

// TODO check that last entry before EOF is included
});
for (File file : files) {
try (Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.ISO_8859_1)) {
lines.forEachOrdered(line -> {
if (!line.startsWith(" ")) {
createNewEntry(line);
} else {
appendToLastEntry(line);
}
// TODO check that last entry before EOF is included
});
}
}

return logEntries;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public String toString() {

/**
* Turns a given String into the corresponding LogLevel
*
*
* @param value
* e.g. "WARN"
* @return the figured out LogLevel, DEBUG if it could not find out which one
*/
public static LogLevel fromValue(String value) {
if (value == null || "".equals(value)) {
if (value == null || "".equals(value)){
return DEBUG;
}

Expand All @@ -39,7 +39,7 @@ public static LogLevel fromValue(String value) {

return DEBUG;
}

public static LogLevel[] valuesDesc() {
List<LogLevel> logLevels = Arrays.asList(LogLevel.values());
Collections.reverse(logLevels);
Expand Down
Loading