-
Notifications
You must be signed in to change notification settings - Fork 9
/
resultlog.py
67 lines (56 loc) · 2.17 KB
/
resultlog.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
.. module:: resultlog
:platform: Linux, Windows
:synopsis: main module
.. moduleauthor:: Zoltan Siki <[email protected]>
"""
import datetime
import time
from PyQt4.QtCore import QDir, QFile, QIODevice, QTextStream
class ResultLog(object):
""" File based logging for Surveying Calculations. Events & calculation results are logged into this file.
"""
resultlog_message = ""
def __init__(self, logfile, repeat_count=3):
""" initialize log file if the given file cannot be opened for output then a SurveyingCalculation.log file in the temperary directory will be used
:param logfile: name of the log file it will be created if neccessary, messages will be appended to the end
:param repeat_count: retry count on fail accessing log file
"""
self.repeat_count = repeat_count # retry count for i/o operations
self.set_log_path(logfile)
def set_log_path(self, log_path):
for i in range(self.repeat_count * 2):
f = QFile( log_path )
if not f.open(QIODevice.Append | QIODevice.Text):
f = None
if i == self.repeat_count:
log_path = QDir.temp().absoluteFilePath("SurveyingCalculation.log")
f.close()
self.logfile = log_path
def reset(self):
""" Delete content of log file
"""
for i in range(self.repeat_count):
if QFile(self.logfile).remove():
break
def write(self, msg = ""):
""" Write a simple message to log
:param msg: message to write
"""
for i in range(self.repeat_count):
f = QFile( self.logfile )
if not f.open(QIODevice.Append | QIODevice.Text):
continue
stream = QTextStream(f)
stream << (msg+'\n')
break
f.close()
def write_log(self, msg):
""" Write log message with date & time
:param msg: message to write
"""
d = time.strftime("%Y-%m-%d %H:%M:%S",
datetime.datetime.now().timetuple())
self.write(d + " - " + msg)