blob: 1372d7a9c2020439e34e64faa4c0eef75f0092c1 [file] [log] [blame]
Valentin Schneider0d3a0222017-09-11 16:51:33 +01001import os
2import re
3import shutil
4
5from devlib.trace import TraceCollector
6from devlib.utils.android import LogcatMonitor
7
8class LogcatCollector(TraceCollector):
9
10 def __init__(self, target, regexps=None):
11 super(LogcatCollector, self).__init__(target)
12 self.regexps = regexps
13 self._collecting = False
14 self._prev_log = None
15
16 def reset(self):
17 """
18 Clear Collector data but do not interrupt collection
19 """
20 if not self._monitor:
21 return
22
23 if self._collecting:
24 self._monitor.clear_log()
25 elif self._prev_log:
26 os.remove(self._prev_log)
27 self._prev_log = None
28
29 def start(self):
30 """
31 Start collecting logcat lines
32 """
33 self._monitor = LogcatMonitor(self.target, self.regexps)
34 if self._prev_log:
35 # Append new data collection to previous collection
36 self._monitor.start(self._prev_log)
37 else:
38 self._monitor.start()
39
40 self._collecting = True
41
42 def stop(self):
43 """
44 Stop collecting logcat lines
45 """
46 if not self._collecting:
47 raise RuntimeError('Logcat monitor not running, nothing to stop')
48
49 self._monitor.stop()
50 self._collecting = False
51 self._prev_log = self._monitor.logfile
52
53 def get_trace(self, outfile):
54 """
55 Output collected logcat lines to designated file
56 """
57 # copy self._monitor.logfile to outfile
58 shutil.copy(self._monitor.logfile, outfile)