package de.uhilger.calypso; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author ulrich */ public class OMXLogLeser { private static final Logger logger = Logger.getLogger(OMXLogLeser.class.getName()); public static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); private static final long MILLIS = (long) 1000; private static final long MINSEC = (long) 60; public String logDirLesen(File logDir) throws IOException, FileNotFoundException, ParseException { Blocks blocks = new Blocks(); File[] files = logDir.listFiles(); for(File file : files) { lesen(file, blocks); } return blocks.getTimeString(); } /** * *

Die Spieldauer laut OMX-Log ergibt sich aus dem Zeitstempel des * letzten Eintrags abzueglich des Zeitstempels des * ersten Eintrags abzueglich der Pausen

* *

Eine Pause ist der Zeitraum vom Zeitstempel eines Eintrags mit * Keyboard: character p (0x70) bis zum Zeitstempel des * nächsten Eintrags mit Keyboard: character p

* * * @param logfile * @param blocks * @return die Spieldauer als String im Format H:MM:SS * @throws FileNotFoundException * @throws IOException * @throws ParseException */ public void lesen(File logfile, Blocks blocks) throws FileNotFoundException, IOException, ParseException { boolean inPause = false; logger.info("Starting to parse log.."); Date parseStart = new Date(); long lineCount = 0; InputStream is = new FileInputStream(logfile); BufferedReader r = new BufferedReader(new InputStreamReader(is)); String firstLine = r.readLine(); ++lineCount; String lastLine = ""; String line = r.readLine(); //Blocks blocks = new Blocks(); Block currentBlock = new Block(); while(line != null) { ++lineCount; if(currentBlock.getStart() == null) { currentBlock.setStart(line.substring(0, 8)); } if(line.contains("Keyboard: character p")) { if(inPause) { currentBlock.setStart(line.substring(0, 8)); } else { currentBlock.setEnd(line.substring(0, 8)); blocks.add(currentBlock); currentBlock = new Block(); } inPause = !inPause; } lastLine = line; line = r.readLine(); } currentBlock.setEnd(lastLine.substring(0, 8)); blocks.add(currentBlock); Date parseEnd = new Date(); long timeMillis = parseEnd.getTime() - parseStart.getTime(); long timeSeconds = timeMillis / MILLIS; long secMillis = timeSeconds * MILLIS; long restMillis = timeMillis - secMillis; logger.log(Level.INFO, "{0} lines parsed in {1} seconds and {2} milliseconds.", new Object[]{lineCount, timeSeconds, restMillis}); //return blocks.getTimeString(); } class Block { private String start = null; private String end = null; public long getDuration() throws ParseException { Date startDate = sdf.parse(start); Date endDate = sdf.parse(end); return endDate.getTime() - startDate.getTime(); } public String getStart() { return start; } public void setStart(String start) { //logger.log(Level.FINER, "Start: ''{0}''", start); this.start = start; } public String getEnd() { return end; } public void setEnd(String end) { //logger.log(Level.FINER, "End: ''{0}''", end); this.end = end; } } class Blocks { private final List blocks = new ArrayList(); public void add(Block block) { blocks.add(block); } public long getDuration() throws ParseException { long duration = (long) 0; Iterator i = blocks.iterator(); while(i.hasNext()) { Block b = i.next(); duration += b.getDuration(); } return duration; } public String getTimeString() throws ParseException { long diff = getDuration(); long h = diff / MILLIS / MINSEC / MINSEC; long hmillis = h * MINSEC * MINSEC * MILLIS; long m = (diff - hmillis) / MILLIS / MINSEC; long mmillis = m * MINSEC * MILLIS; long s = (diff - hmillis - mmillis) / MILLIS; //logger.log(Level.FINER, "{0}:{1}:{2}", new Object[]{h, m, s}); return h + ":" + m + ":" + s; } } }