This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
112 lines (98 loc) · 3.75 KB
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
### log.py --- Logging
## Copyright (C) 2005 Brailcom, o.p.s.
##
## Author: Milan Zamazal <[email protected]>
##
## COPYRIGHT NOTICE
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the Free
## Software Foundation; either version 2 of the License, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
## more details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the Free Software Foundation, Inc., 51
## Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import StringIO
import os
import util
class Logger (object):
def __init__ (self, stream):
self._stream = stream
self._eol = True # new line just started
self._stream_position = 0 # number of characters written so far
def _terpri (self):
if not self._eol:
self._write (os.linesep)
def _write (self, text):
if text:
stream = self._stream
stream.write (text)
if hasattr (stream, 'flush'):
stream.flush ()
# There's no terpri in Python, so we have to implement it
# ourselves. :-(
self._eol = (text[-len(os.linesep):] == os.linesep)
# Again, 'tell' doesn't work for some kinds of file objects in
# Python, so we have to implement our own version. :-(
self._stream_position += len (text)
def _write_formatted_message (self, format, message, eol=True):
self._terpri ()
text = format % (message,)
self._write (format % (message,))
if eol:
self._write (os.linesep)
def log_info (self, message):
"""Log an informational 'message'.
"""
self._write_formatted_message ('= %s', message)
def log_line (self, message):
"""Log 'message' on a separate line.
"""
self._write_formatted_message (' %s', message)
def log_text (self, message):
"""Log a multiline text 'message'.
"""
self._terpri ()
s = StringIO.StringIO (message)
while True:
line = s.readline ()
if not line:
break
self.log_line (line[:-len(os.linesep)])
def with_action_log (self, message, function):
"""Log 'message' and call 'function'.
'function' is called without arguments and must return a pair
(ERROR, RETVAL,), where ERROR is a single line string or None and
RETVAL is a return value. Alternatively, None can be returned which is
equivalent to returning (None, None,).
If MESSAGE is false, log success of the action, otherwise log error.
Return RETVAL.
"""
self._write_formatted_message ('* %s...', message, eol=False)
def finalize (error, _spos=self._stream_position):
if _spos != self._stream_position:
self._write_formatted_message ('+ %s...', message, eol=False)
self._write ('%s.' % (util.if_ (error, 'ERROR', 'OK'),))
self._terpri ()
try:
result = function ()
except Exception:
finalize ('exception')
raise
if result:
error, retval = result
else:
error = retval = None
finalize (error)
return retval
def log_debug (self, message):
"""Log a debug 'message'.
"""
if __debug__:
self._write_formatted_message ('DEBUG: %s', message)