From 17365c1bf397423e4973f5b431bea44bf199076a Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 14 Mar 2017 00:46:04 -0400 Subject: match identification --- dslog/LogDir.java | 39 ++++++++++++++++++++++++++ dslog/LogSet.java | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ dslog/Logdir.java | 82 ------------------------------------------------------- dslog/Main.java | 4 +-- 4 files changed, 115 insertions(+), 84 deletions(-) create mode 100644 dslog/LogDir.java create mode 100644 dslog/LogSet.java delete mode 100644 dslog/Logdir.java diff --git a/dslog/LogDir.java b/dslog/LogDir.java new file mode 100644 index 0000000..d31d3a8 --- /dev/null +++ b/dslog/LogDir.java @@ -0,0 +1,39 @@ +package dslog; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +public class LogDir { + private static LogSet getSet(Map sets, String filename) { + String setname = filename.substring(0, filename.lastIndexOf('.')); + LogSet set; + set = sets.get(setname); + if (set == null) { + set = new LogSet(setname); + sets.put(set.name, set); + } + return set; + } + public static SortedMap opendir(String dirname) throws IOException { + SortedMap logsets = new TreeMap(); + List errs = new ArrayList(); + Files.list(Paths.get(dirname)).forEach(filepath->{ + String filename = filepath.getFileName().toString(); + if (filename.endsWith(".dslog")) { + getSet(logsets, filename).dslogPath = filepath; + } else if (filename.endsWith(".dsevents")) { + getSet(logsets, filename).dseventsPath = filepath; + } else if (filename.endsWith(".pdplog")) { + getSet(logsets, filename).pdplogPath = filepath; + } + }); + return logsets; + } +} diff --git a/dslog/LogSet.java b/dslog/LogSet.java new file mode 100644 index 0000000..e284eba --- /dev/null +++ b/dslog/LogSet.java @@ -0,0 +1,74 @@ +package dslog; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.ArrayList; + +public class LogSet { + public final String name; + public LogSet(String name) { + this.name = name; + } + public Path dslogPath, dseventsPath, pdplogPath; + + public String toString() { + return name+" "+verstr(this::dslog)+"/"+verstr(this::dsevents)+"/"+verstr(this::pdplog); + } + + + private static final Pattern fmsConnected = Pattern.compile("^FMS Connected: (?[A-Za-z].+) - (?[0-9:]+), Field Time: (?[0-9/]+ [0-9:]+)$"); + + public String[] matches() throws IOException { + ArrayList matches = new ArrayList(); + try (DseventsReader dsevents = dsevents()) { + if (dsevents == null) { + return matches.toArray(new String[matches.size()]); + } + while (dsevents.hasNext()) { + DseventsReader.Event event = dsevents.next(); + // do a (cheap) `.startsWith` check before doing an (expensive) regex check. + if (event.message.startsWith("FMS Connected:")) { + Matcher m = fmsConnected.matcher(event.message); + if (m.matches()) { + matches.add(m.group("matchname") + " - " + m.group("matchnumber")); + } + } + } + } + return matches.toArray(new String[matches.size()]); + } + + private static String verstr(Versioned.Supplier getter) { + try (Versioned thing = getter.get()) { + if (thing == null) { + return "␕"; + } + return ""+thing.version(); + } catch (IOException e) { + return "☠"; + } + } + + public DslogReader dslog() throws IOException { + if (dslogPath == null) { + return null; + } + return new DslogReader(Files.newInputStream(dslogPath)); + } + public DseventsReader dsevents() throws IOException { + if (dseventsPath == null) { + return null; + } + return new DseventsReader(Files.newInputStream(dseventsPath)); + } + public PdplogReader pdplog() throws IOException { + if (pdplogPath == null) { + return null; + } + return new PdplogReader(Files.newInputStream(pdplogPath)); + } +} diff --git a/dslog/Logdir.java b/dslog/Logdir.java deleted file mode 100644 index ea7683b..0000000 --- a/dslog/Logdir.java +++ /dev/null @@ -1,82 +0,0 @@ -package dslog; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.function.Supplier; - -class Logdir { - public static class LogSet { - public final String name; - public LogSet(String name) { - this.name = name; - } - public Path dslogPath, dseventsPath, pdplogPath; - - public String toString() { - return name+" "+verstr(this::dslog)+"/"+verstr(this::dsevents)+"/"+verstr(this::pdplog); - } - - private static String verstr(Versioned.Supplier getter) { - try (Versioned thing = getter.get()) { - if (thing == null) { - return "␕"; - } - return ""+thing.version(); - } catch (IOException e) { - return "☠"; - } - } - - public DslogReader dslog() throws IOException { - if (dslogPath == null) { - return null; - } - return new DslogReader(Files.newInputStream(dslogPath)); - } - public DseventsReader dsevents() throws IOException { - if (dseventsPath == null) { - return null; - } - return new DseventsReader(Files.newInputStream(dseventsPath)); - } - public PdplogReader pdplog() throws IOException { - if (pdplogPath == null) { - return null; - } - return new PdplogReader(Files.newInputStream(pdplogPath)); - } - } - - private static LogSet getSet(Map sets, String filename) { - String setname = filename.substring(0, filename.lastIndexOf('.')); - LogSet set; - set = sets.get(setname); - if (set == null) { - set = new LogSet(setname); - sets.put(set.name, set); - } - return set; - } - public static SortedMap opendir(String dirname) throws IOException { - SortedMap logsets = new TreeMap(); - List errs = new ArrayList(); - Files.list(Paths.get(dirname)).forEach(filepath->{ - String filename = filepath.getFileName().toString(); - if (filename.endsWith(".dslog")) { - getSet(logsets, filename).dslogPath = filepath; - } else if (filename.endsWith(".dsevents")) { - getSet(logsets, filename).dseventsPath = filepath; - } else if (filename.endsWith(".pdplog")) { - getSet(logsets, filename).pdplogPath = filepath; - } - }); - return logsets; - } -} diff --git a/dslog/Main.java b/dslog/Main.java index 159ee00..5fa0533 100644 --- a/dslog/Main.java +++ b/dslog/Main.java @@ -19,13 +19,13 @@ class Main { } public static void ls(String dirname) throws IOException { - Logdir.opendir(dirname).forEach((k,v)->{ + LogDir.opendir(dirname).forEach((k,v)->{ System.out.println(v); }); } public static void wch(String dirname) throws IOException { - Logdir.opendir(dirname).forEach((k,v)->{ + LogDir.opendir(dirname).forEach((k,v)->{ boolean match = false; try (DseventsReader r = v.dsevents()) { if (r == null || r.version() != 3) -- cgit v1.2.3