Skip to content

Commit 4e21caa

Browse files
authored
Add logging (#8)
1 parent cfdf8cb commit 4e21caa

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

CherryTomato/main.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
2+
import logging
33
import sys
44

55
from PyQt5 import Qt
@@ -10,7 +10,12 @@
1010
from CherryTomato.settings import CherryTomatoSettings
1111
from CherryTomato.timer_proxy import TomatoTimerProxy
1212
from CherryTomato.tomato_timer import TomatoTimer
13-
from CherryTomato.utils import CommandExecutor
13+
from CherryTomato.utils import CommandExecutor, CompactFormatter, makeLogger
14+
15+
logFmt = '%(asctime)s:%(levelname)s:%(name)s:%(message)s'
16+
handler = logging.StreamHandler(sys.stdout)
17+
handler.setFormatter(CompactFormatter(logFmt, "%Y-%m-%d %H:%M:%S"))
18+
logger = makeLogger('CherryTomato', handler=handler, level='INFO')
1419

1520

1621
def main():

CherryTomato/main_window.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
from functools import lru_cache
3-
from warnings import warn
43

54
from PyQt5 import Qt, QtCore
65
from PyQt5.QtGui import QBrush, QColor, QPalette, QIcon, QKeySequence
@@ -9,11 +8,13 @@
98
from CherryTomato import about_window, APP_ICON, MEDIA_DIR, settings_window
109
from CherryTomato.main_ui import Ui_MainWindow
1110
from CherryTomato.timer_proxy import AbstractTimerProxy
11+
from CherryTomato.utils import classLogger
1212

1313

1414
class CherryTomatoMainWindow(Qt.QMainWindow, Ui_MainWindow):
1515
def __init__(self, timerProxy: AbstractTimerProxy, settings, parent=None):
1616
super().__init__(parent=parent)
17+
self.logger = classLogger(__name__, self.__class__.__name__)
1718
self.settings = settings
1819

1920
self.setupUi(self)
@@ -39,18 +40,21 @@ def __init__(self, timerProxy: AbstractTimerProxy, settings, parent=None):
3940

4041
def setWindowSizeAndPosition(self):
4142
# Initial window size/pos last saved. Use default values for first time
42-
for _ in range(3):
43+
for _ in range(2):
4344
try:
4445
self.resize(self.settings.size)
4546
self.move(self.settings.position)
4647
except TypeError:
48+
msg = f"Can't read window size and position settings. Restore to defaults"
49+
self.logger.warning(msg)
4750
del self.settings.size
4851
del self.settings.position
4952
continue
5053
else:
5154
break
5255
else:
53-
warn(UserWarning("Can't restore window settings"))
56+
msg = "Can't restore window settings"
57+
self.logger.error(msg)
5458

5559
def closeEvent(self, e):
5660
self.saveWindowSizeAndPosition()

CherryTomato/utils.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
import logging
12
import shlex
23
import subprocess
34

45
from CherryTomato.settings import STATE_TOMATO, STATE_BREAK, STATE_LONG_BREAK
56

67

8+
class CompactFormatter(logging.Formatter):
9+
"""Leave class name only
10+
"""
11+
12+
def format(self, record):
13+
record.name = record.name.rpartition('.')[-1]
14+
return super().format(record)
15+
16+
17+
def makeLogger(name, *, handler, level):
18+
"""Create the root logger
19+
"""
20+
logger = logging.getLogger(name)
21+
logger.addHandler(handler)
22+
logger.setLevel(level)
23+
logger.propagate = False
24+
return logger
25+
26+
27+
def classLogger(path, classname):
28+
"""Return logger for a class
29+
"""
30+
return logging.getLogger(path).getChild(classname)
31+
32+
733
class CommandExecutor:
834
def __init__(self, settings):
35+
self.logger = classLogger(__name__, self.__class__.__name__)
936
self.settings = settings
1037

1138
def onStart(self, status):
@@ -22,13 +49,15 @@ def onStateChange(self, status):
2249

2350
def _execute(self, command, status=None):
2451
if command:
25-
command = self._applyMacros(command, status)
52+
cmd_with_macros = self._applyMacros(command, status)
2653
try:
2754
subprocess.Popen(
28-
shlex.split(command), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
55+
shlex.split(cmd_with_macros),
56+
stdout=subprocess.DEVNULL,
57+
stderr=subprocess.STDOUT
2958
)
30-
except Exception as e:
31-
print(e)
59+
except Exception:
60+
self.logger.exception(f'Error while executing command "{command}"', exc_info=True)
3261

3362
@classmethod
3463
def _applyMacros(cls, cmd, status=None):

0 commit comments

Comments
 (0)