8
8
__author__ = """Ed Rousseau <rousseau@redhat.com>,
9
Zack Cerza <zcerza@redhat.com,
9
Zack Cerza <zcerza@redhat.com,
10
10
David Malcolm <dmalcolm@redhat.com>
15
16
from config import config
17
19
# Timestamp class for file logs
20
Generates timestamps tempfiles and log entries
24
self.timetup = time.localtime()
26
def zeroPad(self, int, width = 2):
28
Pads an integer 'int' with zeroes, up to width 'width'.
32
It will not truncate. If you call zeroPad(100, 2), '100' will be returned.
35
return ("0" * (width - len(str(int))) ) + str(int)
40
def fileStamp(self, filename, addTime = True):
42
Generates a filename stamp in the format of filename_YYYYMMDD-hhmmss.
43
A format of filename_YYYYMMDD can be used instead by specifying addTime = False.
45
self.now = filename.strip() + "_"
46
self.timetup = time.localtime()
48
# Should produce rel-eng style filestamps
49
# format it all pretty by chopping the tuple
51
if addTime: fieldCount = fieldCount + 3
52
for i in range(fieldCount):
53
if i == 3: self.now = self.now + '-'
54
self.now = self.now + self.zeroPad(self.timetup[i])
60
Generates a logfile entry stamp of YYYY.MM.DD HH:MM:SS
62
self.timetup = time.localtime()
64
# This will return a log entry formatted string in YYYY.MM.DD HH:MM:SS
68
self.now = str(self.timetup[i])
69
# Format Month and Day
70
elif i == 1 or i == 2:
71
self.now = self.now + "." + self.zeroPad(self.timetup[i])
74
# make the " " between Day and Hour and put in the hour
76
self.now = self.now + " " + self.zeroPad(self.timetup[i])
77
# Otherwise Use the ":" divider
79
self.now = self.now + ":" + self.zeroPad(self.timetup[i])
83
# Class that writes to the Log
86
Writes entries into the Dogtail log
90
# Set the logDir - maybe we want to use mktemp(1) for this later.
91
self.logDir = config.logDir #'/tmp/dogtail-' + os.environ['LOGNAME'] + '/'
92
if not os.path.isdir(self.logDir): os.makedirs(self.logDir)
94
self.scriptName = config.scriptName
95
if not self.scriptName: self.scriptName = 'log'
96
self.loghandle = "" # Handle to the logfile
97
self.stamper = TimeStamp()
98
# check to see if we can write to the logDir
99
if os.path.isdir(self.logDir):
100
# generate a logfile name and check if it already exists
101
self.logfile = self.logDir + self.stamper.fileStamp(self.scriptName)
103
while os.path.exists(self.logfile):
104
# Append the pathname
106
self.logfile = self.logfile + "." + str(i)
108
logsplit = self.logfile.split(".")
109
logsplit[-1] = str(i)
110
self.logfile = ".".join(logsplit)
113
# If path doesn't exist, raise an exception
114
raise IOError, "Log path %s does not exist or is not a directory" % self.logDir
116
# Try to create the file and write the header info
118
print "Creating logfile at %s ..." % self.logfile
119
date = datetime.datetime.strftime(datetime.datetime.now(), '%d %b %Y %H:%M:%S')
120
self.loghandle = open(self.logfile, 'w')
121
self.loghandle.write("##### " + self.scriptName + " Created on: " + date + "\n")
122
self.loghandle.flush()
123
self.loghandle.close()
125
print "Could not create and write to " + self.logfile
128
# Writes the result of a test case comparison to the log
129
def writeResult(self, entry):
131
Writes the log entry. Requires a 1 {key: value} pair dict for an argument or else it will throw an exception.
134
# We require a 1 key: value dict
135
# Strip all leading and trailing witespace from entry dict and convert
136
# to string for writing
138
if len(self.entry) == 1:
139
key = self.entry.keys()
140
value = self.entry.values()
143
self.entry = str(key) + ": " + str(value)
146
print "Method argument requires a 1 {key: value} dict. Supplied argument not one {key: value}"
148
# Try to open and write the result to the log
150
self.loghandle = open(self.logfile, 'a')
151
self.loghandle.write(self.stamper.entryStamp() + " " + self.entry + "\n")
152
self.loghandle.flush()
153
self.loghandle.close()
156
print "Could not write to file " + self.logfile
22
Generates timestamps tempfiles and log entries
26
self.timetup = time.localtime()
28
def zeroPad(self, int, width = 2):
30
Pads an integer 'int' with zeroes, up to width 'width'.
34
It will not truncate. If you call zeroPad(100, 2), '100' will be returned.
37
return ("0" * (width - len(str(int))) ) + str(int)
42
def fileStamp(self, filename, addTime = True):
44
Generates a filename stamp in the format of filename_YYYYMMDD-hhmmss.
45
A format of filename_YYYYMMDD can be used instead by specifying addTime = False.
47
self.now = filename.strip() + "_"
48
self.timetup = time.localtime()
50
# Should produce rel-eng style filestamps
51
# format it all pretty by chopping the tuple
53
if addTime: fieldCount = fieldCount + 3
54
for i in range(fieldCount):
55
if i == 3: self.now = self.now + '-'
56
self.now = self.now + self.zeroPad(self.timetup[i])
62
Generates a logfile entry stamp of YYYY.MM.DD HH:MM:SS
64
self.timetup = time.localtime()
66
# This will return a log entry formatted string in YYYY.MM.DD HH:MM:SS
70
self.now = str(self.timetup[i])
71
# Format Month and Day
72
elif i == 1 or i == 2:
73
self.now = self.now + "." + self.zeroPad(self.timetup[i])
76
# make the " " between Day and Hour and put in the hour
78
self.now = self.now + " " + self.zeroPad(self.timetup[i])
79
# Otherwise Use the ":" divider
81
self.now = self.now + ":" + self.zeroPad(self.timetup[i])
160
Writes entries to the tooltip of an icon in the notification area or the desktop.
163
def __init__(self, config = None, module_list = None):
164
if not IconLogger.trayicon:
165
from trayicon import TrayIcon
166
IconLogger.trayicon = TrayIcon()
167
if IconLogger.trayicon.proc: self.works = True
168
else: self.works = False
169
iconName = 'dogtail-tail-48.png'
170
iconPath = '/usr/share/icons/hicolor/48x48/apps/' + iconName
171
if os.path.exists(iconPath):
172
IconLogger.trayicon.set_icon(iconPath)
173
self.message('dogtail running...')
175
def message(self, msg):
177
Display a message to the user
179
IconLogger.trayicon.set_tooltip(msg)
183
Writes entries to standard out, and to an IconLogger if possible.
186
self.iconLogger = IconLogger()
188
def log(self, message):
190
Hook used for logging messages. Might eventually be a virtual
191
function, but nice and simple for now.
193
# Try to use the IconLogger.
194
if self.iconLogger.works: self.iconLogger.message(message)
195
# Also write to standard out.
198
debugLogger = DebugLogger()
86
Writes entries to the tooltip of an icon in the notification area or the desktop.
90
if not IconLogger.trayicon:
91
from trayicon import TrayIcon
92
IconLogger.trayicon = TrayIcon()
93
if IconLogger.trayicon.proc: self.works = True
94
else: self.works = False
95
iconName = 'dogtail-tail-48.png'
96
iconPath = '/usr/share/icons/hicolor/48x48/apps/' + iconName
97
if os.path.exists(iconPath):
98
IconLogger.trayicon.set_icon(iconPath)
99
self.message('dogtail running...')
101
def message(self, msg):
103
Display a message to the user
105
IconLogger.trayicon.set_tooltip(msg)
108
IconLogger.trayicon.close()
112
Writes entries to standard out, and to an IconLogger if desired.
115
stamper = TimeStamp()
116
def __init__(self, logName, file = False, stdOut = True):
118
FIXME! make this log to a file based on the name arg.
120
name: the name of the log
122
file: The file object to log to.
124
stdOut: Whether to log to standard out.
126
self.logName = logName
128
self.file = file # Handle to the logfile
129
if not self.file: return
131
logDir = config.logDir
132
if not os.path.isdir(logDir): os.makedirs(logDir)
134
scriptName = config.scriptName
135
if not scriptName: scriptName = 'log'
136
self.fileName = scriptName
138
# check to see if we can write to the logDir
139
if os.path.isdir(logDir):
140
# generate a logfile name and check if it already exists
141
self.fileName = logDir + self.stamper.fileStamp(self.fileName) + '_' + self.logName
143
while os.path.exists(self.fileName):
144
# Append the pathname
146
self.fileName = self.fileName + "." + str(i)
148
logsplit = self.fileName.split(".")
149
logsplit[-1] = str(i)
150
self.fileName = ".".join(logsplit)
153
# If path doesn't exist, raise an exception
154
raise IOError, "Log path %s does not exist or is not a directory" % logDir
156
# Try to create the file and write the header info
158
print "Creating logfile at %s ..." % self.fileName
159
self.file = codecs.open(self.fileName, mode = 'wb', encoding = 'utf-8')
160
self.file.write("##### " + os.path.basename(self.fileName) + '\n')
164
print "Could not create and write to " + self.fileName
166
def log(self, message):
168
Hook used for logging messages. Might eventually be a virtual
169
function, but nice and simple for now.
171
message = message.decode('utf-8', 'replace')
173
# Try to use the IconLogger.
174
if self.iconLogger and self.iconLogger.works:
175
self.iconLogger.message(message)
177
# Also write to standard out.
178
if self.stdOut: print message
180
# Try to open and write the result to the log file.
181
if not self.file: return
183
#self.file = open(self.fileName, 'a')
184
self.file.write(message + '\n')
188
print "Could not write to file " + self.fileName
190
class ResultsLogger(Logger):
192
Writes entries into the Dogtail log
194
def __init__(self, stdOut = True):
195
Logger.__init__(self, 'results', file = True, stdOut = stdOut)
196
# Set the logDir - maybe we want to use mktemp(1) for this later.
198
# Writes the result of a test case comparison to the log
199
def log(self, entry):
201
Writes the log entry. Requires a 1 {key: value} pair dict for an argument or else it will throw an exception.
203
# We require a 1 key: value dict
204
# Strip all leading and trailing witespace from entry dict and convert
205
# to string for writing
209
value = entry.values()
212
entry = str(key) + ": " + str(value)
214
raise ValueError, entry
215
print "Method argument requires a 1 {key: value} dict. Supplied argument not one {key: value}"
217
Logger.log(self, self.stamper.entryStamp() + " " + entry)
219
debugLogger = Logger('debug', file = True)
222
def exceptionHook(exc, value, tb):
223
tbStringList = traceback.format_exception(exc, value, tb)
224
tbString = ''.join(tbStringList)
225
debugLogger.log(tbString)
228
sys.excepthook = exceptionHook