12 """Base class for watching the output of commands
13
14 Works like the UNIX-command 'tail -f <file>': the last lines of the file are output.
15 If the file grows then these lines are output as they arrive"""
16
17 - def __init__(self,filename,silent=False,tailLength=1000,sleep=0.1):
18 """@param filename: name of the logfile to watch
19 @param silent: if True no output is sent to stdout
20 @param tailLength: number of bytes at the end of the fail that should be output.
21 Because data is output on a per-line-basis
22 @param sleep: interval to sleep if no line is returned"""
23
24 self.filename=filename
25 self.silent=silent
26 self.tail=tailLength
27 self.sleep=sleep
28 self.isTailing=False
29
30 if not path.exists(self.filename):
31 print "Error: Logfile ",self.filename,"does not exist"
32
33 self.reader=LineReader()
34
36 """@return: the current size (in bytes) of the file"""
37 return os.stat(self.filename)[stat.ST_SIZE]
38
40 """Reads the file and does the processing"""
41
42 currSize=self.getSize()
43 fh=open(self.filename)
44
45 self.startHandle()
46
47 while 1:
48 status=self.reader.read(fh)
49 if status:
50 line=self.reader.line
51 if (currSize-self.reader.bytesRead())<self.tail:
52 if not self.isTailing:
53 self.isTailing=True
54 self.tailingHandle()
55
56 if not self.silent:
57 print line
58 self.lineHandle(line)
59 else:
60 if self.reader.userSaidStop():
61 break
62 try:
63 sleep(self.sleep)
64 except KeyboardInterrupt,e:
65 print "Keyboard interrupt"
66 break
67
68 self.stopHandle()
69
70 fh.close()
71
73 """to be called before the program is started"""
74 pass
75
77 """called after the program has stopped"""
78 pass
79
81 """called when the first line is output"""
82 pass
83
85 """called every time a new line is read"""
86 pass