commit | author | age
|
15ed25
|
1 |
package de.uhilger.avdirektor; |
U |
2 |
|
|
3 |
import java.io.BufferedReader; |
|
4 |
import java.io.File; |
|
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.FileNotFoundException; |
|
7 |
import java.io.IOException; |
|
8 |
import java.io.InputStream; |
|
9 |
import java.io.InputStreamReader; |
94c45c
|
10 |
import java.text.ParseException; |
U |
11 |
import java.text.SimpleDateFormat; |
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.Date; |
|
14 |
import java.util.Iterator; |
|
15 |
import java.util.List; |
|
16 |
import java.util.logging.Level; |
15ed25
|
17 |
import java.util.logging.Logger; |
U |
18 |
|
|
19 |
/** |
|
20 |
* |
|
21 |
* @author ulrich |
|
22 |
*/ |
|
23 |
public class OMXLogLeser { |
|
24 |
|
|
25 |
private static final Logger logger = Logger.getLogger(OMXLogLeser.class.getName()); |
94c45c
|
26 |
|
U |
27 |
public static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
|
28 |
|
|
29 |
private static final long MILLIS = (long) 1000; |
|
30 |
|
|
31 |
private static final long MINSEC = (long) 60; |
15ed25
|
32 |
|
94c45c
|
33 |
/** |
U |
34 |
* |
|
35 |
* <p>Die Spieldauer laut OMX-Log ergibt sich aus dem Zeitstempel des |
|
36 |
* letzten Eintrags abzueglich des Zeitstempels des |
|
37 |
* ersten Eintrags abzueglich der Pausen</p> |
|
38 |
* |
|
39 |
* <p>Eine Pause ist der Zeitraum vom Zeitstempel eines Eintrags mit |
|
40 |
* <code>Keyboard: character p (0x70)</code> bis zum Zeitstempel des |
|
41 |
* nächsten Eintrags mit <code>Keyboard: character p</code></p> |
|
42 |
* |
|
43 |
* |
|
44 |
* @param logfile |
|
45 |
* @return die Spieldauer als String im Format H:MM:SS |
|
46 |
* @throws FileNotFoundException |
|
47 |
* @throws IOException |
|
48 |
* @throws ParseException |
|
49 |
*/ |
|
50 |
public String lesen(File logfile) throws FileNotFoundException, IOException, ParseException { |
|
51 |
boolean inPause = false; |
|
52 |
logger.info("Starting to parse log.."); |
|
53 |
Date parseStart = new Date(); |
|
54 |
long lineCount = 0; |
15ed25
|
55 |
InputStream is = new FileInputStream(logfile); |
U |
56 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
57 |
String firstLine = r.readLine(); |
94c45c
|
58 |
++lineCount; |
15ed25
|
59 |
String lastLine = ""; |
94c45c
|
60 |
String line = r.readLine(); |
U |
61 |
Blocks blocks = new Blocks(); |
|
62 |
Block currentBlock = new Block(); |
|
63 |
while(line != null) { |
|
64 |
++lineCount; |
|
65 |
if(currentBlock.getStart() == null) { |
|
66 |
currentBlock.setStart(line.substring(0, 8)); |
15ed25
|
67 |
} |
94c45c
|
68 |
if(line.contains("Keyboard: character p")) { |
U |
69 |
if(inPause) { |
|
70 |
currentBlock.setStart(line.substring(0, 8)); |
|
71 |
} else { |
|
72 |
currentBlock.setEnd(line.substring(0, 8)); |
|
73 |
blocks.add(currentBlock); |
|
74 |
currentBlock = new Block(); |
|
75 |
} |
|
76 |
inPause = !inPause; |
|
77 |
} |
|
78 |
lastLine = line; |
|
79 |
line = r.readLine(); |
15ed25
|
80 |
} |
94c45c
|
81 |
currentBlock.setEnd(lastLine.substring(0, 8)); |
U |
82 |
blocks.add(currentBlock); |
|
83 |
Date parseEnd = new Date(); |
|
84 |
long timeMillis = parseEnd.getTime() - parseStart.getTime(); |
|
85 |
long timeSeconds = timeMillis / MILLIS; |
|
86 |
long secMillis = timeSeconds * MILLIS; |
|
87 |
long restMillis = timeMillis - secMillis; |
|
88 |
logger.log(Level.INFO, |
|
89 |
"{0} lines parsed in {1} seconds and {2} milliseconds.", |
|
90 |
new Object[]{lineCount, timeSeconds, restMillis}); |
|
91 |
return blocks.getTimeString(); |
15ed25
|
92 |
} |
U |
93 |
|
94c45c
|
94 |
class Block { |
U |
95 |
private String start = null; |
|
96 |
private String end = null; |
|
97 |
|
|
98 |
public long getDuration() throws ParseException { |
|
99 |
Date startDate = sdf.parse(start); |
|
100 |
Date endDate = sdf.parse(end); |
|
101 |
return endDate.getTime() - startDate.getTime(); |
|
102 |
} |
|
103 |
|
|
104 |
public String getStart() { |
|
105 |
return start; |
|
106 |
} |
|
107 |
|
|
108 |
public void setStart(String start) { |
|
109 |
//logger.log(Level.FINER, "Start: ''{0}''", start); |
|
110 |
this.start = start; |
|
111 |
} |
|
112 |
|
|
113 |
public String getEnd() { |
|
114 |
return end; |
|
115 |
} |
|
116 |
|
|
117 |
public void setEnd(String end) { |
|
118 |
//logger.log(Level.FINER, "End: ''{0}''", end); |
|
119 |
this.end = end; |
|
120 |
} |
|
121 |
|
|
122 |
|
|
123 |
} |
|
124 |
|
|
125 |
class Blocks { |
|
126 |
|
|
127 |
private final List<Block> blocks = new ArrayList(); |
|
128 |
|
|
129 |
public void add(Block block) { |
|
130 |
blocks.add(block); |
|
131 |
} |
|
132 |
|
|
133 |
public long getDuration() throws ParseException { |
|
134 |
long duration = (long) 0; |
|
135 |
Iterator<Block> i = blocks.iterator(); |
|
136 |
while(i.hasNext()) { |
|
137 |
Block b = i.next(); |
|
138 |
duration += b.getDuration(); |
|
139 |
} |
|
140 |
return duration; |
|
141 |
} |
|
142 |
|
|
143 |
public String getTimeString() throws ParseException { |
|
144 |
long diff = getDuration(); |
|
145 |
long h = diff / MILLIS / MINSEC / MINSEC; |
|
146 |
long hmillis = h * MINSEC * MINSEC * MILLIS; |
|
147 |
long m = (diff - hmillis) / MILLIS / MINSEC; |
|
148 |
long mmillis = m * MINSEC * MILLIS; |
|
149 |
long s = (diff - hmillis - mmillis) / MILLIS; |
|
150 |
//logger.log(Level.FINER, "{0}:{1}:{2}", new Object[]{h, m, s}); |
|
151 |
return h + ":" + m + ":" + s; |
|
152 |
} |
|
153 |
} |
15ed25
|
154 |
} |