diff options
author | Cow <cow@volloeko.de> | 2020-06-22 04:22:47 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-06-22 04:22:47 +0200 |
commit | 77735fd8edacd9dc996db8de424f5ee7c73dff77 (patch) | |
tree | 8b63bc2b6140faf12a37804ce31a108356816bda /src | |
parent | 3d3f861866c2196bdb83bdfbd73e58aa56be2966 (diff) | |
download | Cowlection-77735fd8edacd9dc996db8de424f5ee7c73dff77.tar.gz Cowlection-77735fd8edacd9dc996db8de424f5ee7c73dff77.tar.bz2 Cowlection-77735fd8edacd9dc996db8de424f5ee7c73dff77.zip |
Mc log file search: now also analyzes latest.log
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/eu/olli/cowmoonication/search/GuiSearch.java | 28 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowmoonication/search/LogFilesSearcher.java | 16 |
2 files changed, 33 insertions, 11 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/search/GuiSearch.java b/src/main/java/eu/olli/cowmoonication/search/GuiSearch.java index 7932b29..d0c9597 100644 --- a/src/main/java/eu/olli/cowmoonication/search/GuiSearch.java +++ b/src/main/java/eu/olli/cowmoonication/search/GuiSearch.java @@ -31,6 +31,8 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.List; @@ -480,21 +482,29 @@ public class GuiSearch extends GuiScreen { } byte[] buffer = new byte[1024]; String logFileName = Utils.toRealPath(searchResult.getFilePath()); - try (GZIPInputStream logFileGzipped = new GZIPInputStream(new FileInputStream(logFileName)); - FileOutputStream logFileUnGzipped = new FileOutputStream(mcLogOutputFile)) { + if (logFileName.endsWith("latest.log")) { + try { + Files.copy(searchResult.getFilePath(), mcLogOutputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + e.printStackTrace(); + } + } else { // .log.gz String newLine = System.getProperty("line.separator"); - logFileUnGzipped.write(("# Original filename: " + logFileName + newLine + "# Use CTRL + F to search for specific words" + newLine + newLine).getBytes()); - int len; - while ((len = logFileGzipped.read(buffer)) > 0) { - logFileUnGzipped.write(buffer, 0, len); + String fileHeader = "# Original filename: " + logFileName + newLine + "# Use CTRL + F to search for specific words" + newLine + newLine; + try (GZIPInputStream logFileGzipped = new GZIPInputStream(new FileInputStream(logFileName)); + FileOutputStream logFileUnGzipped = new FileOutputStream(mcLogOutputFile)) { + logFileUnGzipped.write(fileHeader.getBytes()); + int len; + while ((len = logFileGzipped.read(buffer)) > 0) { + logFileUnGzipped.write(buffer, 0, len); + } + } catch (IOException e) { + e.printStackTrace(); } - } catch (IOException e) { - e.printStackTrace(); } try { Desktop.getDesktop().open(mcLogOutputFile); - System.out.println("Opened " + mcLogOutputFile); } catch (IOException e) { setErrorMessage("File extension .txt has no associated default editor"); e.printStackTrace(); diff --git a/src/main/java/eu/olli/cowmoonication/search/LogFilesSearcher.java b/src/main/java/eu/olli/cowmoonication/search/LogFilesSearcher.java index a8ebef8..04ee88a 100644 --- a/src/main/java/eu/olli/cowmoonication/search/LogFilesSearcher.java +++ b/src/main/java/eu/olli/cowmoonication/search/LogFilesSearcher.java @@ -10,8 +10,11 @@ import java.io.*; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -57,9 +60,13 @@ class LogFilesSearcher { List<LogEntry> searchResults = new ArrayList<>(); for (Path path : paths) { boolean foundSearchTermInFile = false; - try (BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(path.toFile()))))) { + try (BufferedReader in = (path.endsWith("latest.log") + ? new BufferedReader(new InputStreamReader(new FileInputStream(path.toFile()))) // latest.log + : new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(path.toFile())))))) { // ....log.gz String fileName = path.getFileName().toString(); // 2020-04-20-3.log.gz - String date = fileName.substring(0, fileName.lastIndexOf('-')); + String date = fileName.equals("latest.log") + ? LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE) + : fileName.substring(0, fileName.lastIndexOf('-')); String content; LogEntry logEntry = null; while ((content = in.readLine()) != null) { @@ -155,6 +162,11 @@ class LogFilesSearcher { } else { System.err.println("Error with " + path.toString()); } + } else if (path.getFileName().toString().equals("latest.log")) { + LocalDate lastModified = Instant.ofEpochMilli(path.toFile().lastModified()).atZone(ZoneId.systemDefault()).toLocalDate(); + if (!lastModified.isBefore(startDate) && !lastModified.isAfter(endDate)) { + fileNames.add(path); + } } } } |