@@ -43,23 +43,16 @@ class ProcessingLog:
43
43
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
44
44
recentAlgs = []
45
45
46
- @staticmethod
47
- def startLogging ():
48
- if os .path .isfile (ProcessingLog .logFilename ()):
49
- logfile = codecs .open (ProcessingLog .logFilename (), 'a' ,
50
- encoding = 'utf-8' )
51
- else :
52
- logfile = codecs .open (ProcessingLog .logFilename (), 'w' ,
53
- encoding = 'utf-8' )
54
- logfile .write ('Started logging at ' +
55
- datetime .datetime .now ().strftime (
56
- ProcessingLog .DATE_FORMAT ) + '\n ' )
57
- logfile .close ()
58
-
59
46
@staticmethod
60
47
def logFilename ():
61
- batchfile = userFolder () + os .sep + 'processing.log'
62
- return batchfile
48
+ logFilename = userFolder () + os .sep + 'processing.log'
49
+ if not os .path .isfile (logFilename ):
50
+ logfile = codecs .open (logFilename , 'w' , encoding = 'utf-8' )
51
+ logfile .write ('Started logging at ' +
52
+ datetime .datetime .now ().strftime (ProcessingLog .DATE_FORMAT ) + '\n ' )
53
+ logfile .close ()
54
+
55
+ return logFilename
63
56
64
57
@staticmethod
65
58
def addToLog (msgtype , msg ):
@@ -101,7 +94,9 @@ def getLogEntries():
101
94
algorithms = []
102
95
warnings = []
103
96
info = []
104
- lines = tail (ProcessingLog .logFilename ())
97
+
98
+ with open (ProcessingLog .logFilename ()) as f :
99
+ lines = f .readlines ()
105
100
for line in lines :
106
101
line = line .strip ('\n ' ).strip ()
107
102
tokens = line .split ('|' )
@@ -133,7 +128,6 @@ def getRecentAlgorithms():
133
128
@staticmethod
134
129
def clearLog ():
135
130
os .unlink (ProcessingLog .logFilename ())
136
- ProcessingLog .startLogging ()
137
131
138
132
@staticmethod
139
133
def saveLog (fileName ):
@@ -150,134 +144,4 @@ def __init__(self, date, text):
150
144
self .date = date
151
145
self .text = text
152
146
153
- """
154
- ***************************************************************************
155
- This code has been take from pytailer
156
- http://code.google.com/p/pytailer/
157
-
158
- Permission is hereby granted, free of charge, to any person
159
- obtaining a copy of this software and associated documentation
160
- files (the "Software"), to deal in the Software without
161
- restriction, including without limitation the rights to use, copy,
162
- modify, merge, publish, distribute, sublicense, and/or sell copies
163
- of the Software, and to permit persons to whom the Software is
164
- furnished to do so, subject to the following conditions:
165
-
166
- The above copyright notice and this permission notice shall be
167
- included in all copies or substantial portions of the Software.
168
-
169
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
170
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
171
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
172
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
173
- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
174
- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
175
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
176
- SOFTWARE.
177
- ***************************************************************************
178
- """
179
-
180
-
181
- class Tailer (object ):
182
-
183
- """Implements tailing and heading functionality like GNU tail and
184
- head commands.
185
- """
186
- line_terminators = ('\r \n ' , '\n ' , '\r ' )
187
-
188
- def __init__ (self , filename , read_size = 1024 , end = False ):
189
- self .read_size = read_size
190
- self .file = codecs .open (filename , encoding = 'utf-8' )
191
- self .start_pos = self .file .tell ()
192
- if end :
193
- self .seek_end ()
194
-
195
- def splitlines (self , data ):
196
- return re .split ('|' .join (self .line_terminators ), data )
197
-
198
- def seek_end (self ):
199
- self .seek (0 , 2 )
200
-
201
- def seek (self , pos , whence = 0 ):
202
- self .file .seek (pos , whence )
203
-
204
- def read (self , read_size = None ):
205
- if read_size :
206
- read_str = self .file .read (read_size )
207
- else :
208
- read_str = self .file .read ()
209
-
210
- return (len (read_str ), read_str )
211
-
212
- def seek_line (self ):
213
- """Searches backwards from the current file position for a
214
- line terminator and seeks to the charachter after it.
215
- """
216
- pos = end_pos = self .file .tell ()
217
-
218
- read_size = self .read_size
219
- if pos > read_size :
220
- pos -= read_size
221
- else :
222
- pos = 0
223
- read_size = end_pos
224
-
225
- self .seek (pos )
226
-
227
- (bytes_read , read_str ) = self .read (read_size )
228
-
229
- if bytes_read and read_str [- 1 ] in self .line_terminators :
230
- # The last charachter is a line terminator, don't count
231
- # this one.
232
- bytes_read -= 1
233
-
234
- if read_str [- 2 :] == '\r \n ' and '\r \n ' in self .line_terminators :
235
- # found CRLF
236
- bytes_read -= 1
237
-
238
- while bytes_read > 0 :
239
- # Scan backward, counting the newlines in this bufferfull
240
- i = bytes_read - 1
241
- while i >= 0 :
242
- if read_str [i ] in self .line_terminators :
243
- self .seek (pos + i + 1 )
244
- return self .file .tell ()
245
- i -= 1
246
-
247
- if pos == 0 or pos - self .read_size < 0 :
248
- # Not enought lines in the buffer, send the whole file
249
- self .seek (0 )
250
- return None
251
-
252
- pos -= self .read_size
253
- self .seek (pos )
254
-
255
- (bytes_read , read_str ) = self .read (self .read_size )
256
-
257
- return None
258
-
259
- def tail (self , lines = 10 ):
260
- """Return the last lines of the file.
261
- """
262
- self .seek_end ()
263
- end_pos = self .file .tell ()
264
-
265
- for i in xrange (lines ):
266
- if not self .seek_line ():
267
- break
268
-
269
- data = self .file .read (end_pos - self .file .tell () - 1 )
270
- if data :
271
- return self .splitlines (data )
272
- else :
273
- return []
274
-
275
- def __iter__ (self ):
276
- return self .follow ()
277
-
278
- def close (self ):
279
- self .file .close ()
280
-
281
147
282
- def tail (file , lines = 200 ):
283
- return Tailer (file ).tail (lines )
0 commit comments