Skip to content
Permalink
Browse files

[processing] improvements to log

  • Loading branch information
volaya committed Jan 15, 2016
1 parent 71b7ea9 commit a63cab77ba29bfddb6d426bdde72ee8684554585
@@ -142,7 +142,6 @@ def initialize():

# And initialize
AlgorithmClassification.loadClassification()
ProcessingLog.startLogging()
ProcessingConfig.initialize()
ProcessingConfig.readSettings()
RenderingStyles.loadStyles()
@@ -43,23 +43,16 @@ class ProcessingLog:
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
recentAlgs = []

@staticmethod
def startLogging():
if os.path.isfile(ProcessingLog.logFilename()):
logfile = codecs.open(ProcessingLog.logFilename(), 'a',
encoding='utf-8')
else:
logfile = codecs.open(ProcessingLog.logFilename(), 'w',
encoding='utf-8')
logfile.write('Started logging at ' +
datetime.datetime.now().strftime(
ProcessingLog.DATE_FORMAT) + '\n')
logfile.close()

@staticmethod
def logFilename():
batchfile = userFolder() + os.sep + 'processing.log'
return batchfile
logFilename = userFolder() + os.sep + 'processing.log'
if not os.path.isfile(logFilename):
logfile = codecs.open(logFilename, 'w', encoding='utf-8')
logfile.write('Started logging at ' +
datetime.datetime.now().strftime(ProcessingLog.DATE_FORMAT) + '\n')
logfile.close()

return logFilename

@staticmethod
def addToLog(msgtype, msg):
@@ -101,7 +94,9 @@ def getLogEntries():
algorithms = []
warnings = []
info = []
lines = tail(ProcessingLog.logFilename())

with open(ProcessingLog.logFilename()) as f:
lines = f.readlines()
for line in lines:
line = line.strip('\n').strip()
tokens = line.split('|')
@@ -133,7 +128,6 @@ def getRecentAlgorithms():
@staticmethod
def clearLog():
os.unlink(ProcessingLog.logFilename())
ProcessingLog.startLogging()

@staticmethod
def saveLog(fileName):
@@ -150,134 +144,4 @@ def __init__(self, date, text):
self.date = date
self.text = text

"""
***************************************************************************
This code has been take from pytailer
http://code.google.com/p/pytailer/
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***************************************************************************
"""


class Tailer(object):

"""Implements tailing and heading functionality like GNU tail and
head commands.
"""
line_terminators = ('\r\n', '\n', '\r')

def __init__(self, filename, read_size=1024, end=False):
self.read_size = read_size
self.file = codecs.open(filename, encoding='utf-8')
self.start_pos = self.file.tell()
if end:
self.seek_end()

def splitlines(self, data):
return re.split('|'.join(self.line_terminators), data)

def seek_end(self):
self.seek(0, 2)

def seek(self, pos, whence=0):
self.file.seek(pos, whence)

def read(self, read_size=None):
if read_size:
read_str = self.file.read(read_size)
else:
read_str = self.file.read()

return (len(read_str), read_str)

def seek_line(self):
"""Searches backwards from the current file position for a
line terminator and seeks to the charachter after it.
"""
pos = end_pos = self.file.tell()

read_size = self.read_size
if pos > read_size:
pos -= read_size
else:
pos = 0
read_size = end_pos

self.seek(pos)

(bytes_read, read_str) = self.read(read_size)

if bytes_read and read_str[-1] in self.line_terminators:
# The last charachter is a line terminator, don't count
# this one.
bytes_read -= 1

if read_str[-2:] == '\r\n' and '\r\n' in self.line_terminators:
# found CRLF
bytes_read -= 1

while bytes_read > 0:
# Scan backward, counting the newlines in this bufferfull
i = bytes_read - 1
while i >= 0:
if read_str[i] in self.line_terminators:
self.seek(pos + i + 1)
return self.file.tell()
i -= 1

if pos == 0 or pos - self.read_size < 0:
# Not enought lines in the buffer, send the whole file
self.seek(0)
return None

pos -= self.read_size
self.seek(pos)

(bytes_read, read_str) = self.read(self.read_size)

return None

def tail(self, lines=10):
"""Return the last lines of the file.
"""
self.seek_end()
end_pos = self.file.tell()

for i in xrange(lines):
if not self.seek_line():
break

data = self.file.read(end_pos - self.file.tell() - 1)
if data:
return self.splitlines(data)
else:
return []

def __iter__(self):
return self.follow()

def close(self):
self.file.close()


def tail(file, lines=200):
return Tailer(file).tail(lines)
@@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>History and log</string>
<string>History</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">

0 comments on commit a63cab7

Please sign in to comment.
You can’t perform that action at this time.