81
81
self.now = self.now + ":" + self.zeroPad(self.timetup[i])
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.
86
Writes entries to standard out.
115
88
stamper = TimeStamp()
116
89
def __init__(self, logName, file = False, stdOut = True):
118
FIXME! make this log to a file based on the name arg.
120
91
name: the name of the log
122
92
file: The file object to log to.
124
93
stdOut: Whether to log to standard out.
126
95
self.logName = logName
128
97
self.file = file # Handle to the logfile
129
98
if not self.file: return
131
logDir = config.logDir
132
if not os.path.isdir(logDir): os.makedirs(logDir)
134
100
scriptName = config.scriptName
135
101
if not scriptName: scriptName = 'log'
136
102
self.fileName = scriptName
138
104
# 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)
105
if os.path.isdir(config.logDir):
106
self.findUniqueName()
153
108
# If path doesn't exist, raise an exception
154
raise IOError, "Log path %s does not exist or is not a directory" % logDir
110
"Log path %s does not exist or is not a directory" % logDir
112
def findUniqueName(self):
113
# generate a logfile name and check if it already exists
114
self.fileName = config.logDir + self.stamper.fileStamp(self.fileName) \
117
while os.path.exists(self.fileName):
118
# Append the pathname
120
self.fileName = self.fileName + "." + str(i)
122
logsplit = self.fileName.split(".")
123
logsplit[-1] = str(i)
124
self.fileName = ".".join(logsplit)
127
def createFile(self):
156
128
# 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
129
print "Creating logfile at %s ..." % self.fileName
130
self.file = codecs.open(self.fileName, mode = 'wb', encoding = \
132
self.file.write("##### " + os.path.basename(self.fileName) + '\n')
166
def log(self, message):
135
def log(self, message, newline = True, force = False):
168
137
Hook used for logging messages. Might eventually be a virtual
169
138
function, but nice and simple for now.
140
If force is True, log to a file irrespective of config.logDebugToFile.
171
142
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
144
# 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')
145
if isinstance(self.file, bool) and (force or config.logDebugToFile):
148
if force or config.logDebugToFile:
149
if newline: self.file.write(message + '\n')
150
else: self.file.write(message + ' ')
185
151
self.file.flush()
188
print "Could not write to file " + self.fileName
153
if self.stdOut and config.logDebugToStdOut:
154
if newline: print message
190
157
class ResultsLogger(Logger):
214
180
raise ValueError, entry
215
181
print "Method argument requires a 1 {key: value} dict. Supplied argument not one {key: value}"
217
Logger.log(self, self.stamper.entryStamp() + " " + entry)
183
Logger.log(self, self.stamper.entryStamp() + " " + entry, force = True)
219
debugLogger = Logger('debug', file = True)
185
debugLogger = Logger('debug', config.logDebugToFile)
222
188
def exceptionHook(exc, value, tb):