-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogger.py
More file actions
77 lines (63 loc) · 2.46 KB
/
logger.py
File metadata and controls
77 lines (63 loc) · 2.46 KB
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
68
69
70
71
72
73
74
75
76
77
import logging
import os
import time
log = logging.getLogger('AmbientLightPyClient')
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s]\t%(message)s')
# Add console handler
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
log.addHandler(consoleHandler)
# Add file handler
if not os.path.exists('logs'):
os.makedirs('logs')
fileHandler = logging.FileHandler('logs/ambilight_client.log')
fileHandler.setFormatter(formatter)
log.addHandler(fileHandler)
class StatusBarLogger(logging.Handler):
def __init__(self, mainWindow):
logging.Handler.__init__(self)
self.setLevel(logging.DEBUG)
log.addHandler(self)
self.mainWindow = mainWindow
self.prevLevel = 0
self.prevLogTime = time.time()
self.logCount = 0
def createLock(self):
from PyQt4 import QtCore
self.mutex = QtCore.QMutex()
def acquire(self):
if 'mutex' not in self.__dict__.keys():
self.createLock()
self.mutex.lock()
def release(self):
if 'mutex' not in self.__dict__.keys():
self.createLock()
self.mutex.unlock()
def emit(self, record):
# Set log count
self.logCount += 1
self.mainWindow.ui.statusBarCountLabel.setText(str(self.logCount))
if record.levelno == logging.DEBUG:
return
# Don't log if level is lower and has not logged for 5 secs yet
if record.levelno < self.prevLevel:
if self.prevLogTime + 5 > time.time():
return
# Switch icon if needed
if record.levelno != self.prevLevel:
if record.levelno == logging.WARNING:
self.mainWindow.ui.statusBarIconInfo.hide()
self.mainWindow.ui.statusBarIconWarning.show()
self.mainWindow.ui.statusBarIconError.hide()
elif record.levelno in [logging.ERROR, logging.CRITICAL]:
self.mainWindow.ui.statusBarIconInfo.hide()
self.mainWindow.ui.statusBarIconWarning.hide()
self.mainWindow.ui.statusBarIconError.show()
else:
self.mainWindow.ui.statusBarIconInfo.show()
self.mainWindow.ui.statusBarIconWarning.hide()
self.mainWindow.ui.statusBarIconError.hide()
self.prevLogTime = time.time()
self.prevLevel = record.levelno
self.mainWindow.ui.statusLabel.setText(self.format(record))