1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """
24 Simple log file.
25 """
26
27 import Biskit.tools as T
28 import sys
29
31 """
32 Simple log file that can be passed between objects.
33 It is flushed after each writing and should hence always be
34 up2date.
35 """
36
38 """
39 @param fname: name of log file
40 @type fname: str
41 @param mode: mode (default: w)
42 @type mode: str
43 """
44 self.fname = T.absfile( fname )
45 self.mode = mode
46 self._f = None
47
48
50 """
51 Open file only when needed for first time.
52
53 @return: open file handle
54 @rtype: object
55 """
56 if self._f is None:
57 self._f = open( self.fname, self.mode )
58
59 return self._f
60
61
63 """Synonym for add_nobreak.
64 @param s: line
65 @type s: str
66 """
67 self.add_nobreak( s )
68
70 """Synonym for add.
71 @param s: line
72 @type s: str
73 """
74 self.add(s)
75
77 """
78 Add new line to logfile and flush
79
80 @param s: line
81 @type s: str
82 """
83 if not type(s) is list:
84 s = [ s ]
85
86 for i in s:
87 self.f().write(i)
88 self.f().write('\n')
89
90 self.f().flush()
91
92
94 """
95 Add new line without creating a linebreak to logfile and flush
96
97 @param s: line
98 @type s: str
99 """
100 self.f().write(s)
101 self.f().flush()
102
103
105 """
106 Close file handle.
107 """
108 if self._f is not None:
109 self.f().close()
110 self._f = None
111
112
114 """
115 Print to stderr instead.
116 """
117
119 self.fname = None
120 self.mode = None
121 self._f = sys.stderr
122
124 pass
125
126
128 """
129 Print to std out.
130 """
132 self.fname = None
133 self.mode = None
134 self._f = sys.stdout
135
137 pass
138
139
140
141
142
143
144 import Biskit.test as BT
145
146 -class Test(BT.BiskitTest):
147
149 import tempfile
150 self.f_out = tempfile.mktemp( '_test_LogFile' )
151
153 if T.tryRemove( self.f_out ) and self.local:
154 print 'log file removed.'
155
157 """LogFile test """
158
159 self.l = LogFile( self.f_out, mode='w')
160
161 self.l.writeln('1')
162 self.l.write('2')
163 self.l.writeln('3')
164
165 if self.local:
166 print 'log file written to %s'%self.f_out
167
168 lines = open(self.f_out).readlines()
169 self.assertEqual( lines, ['1\n','23\n'] )
170
172 """StdLog test (only in interactive mode)"""
173 if self.local:
174 self.l = StdLog()
175 self.l.write(' test message ... ')
176
178 """ErrLog test"""
179 self.l = ErrLog()
180 self.l.write(' test message ... ')
181
182
183 if __name__ == '__main__':
184
185 BT.localTest()
186