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