1
+ import logging
1
2
import shlex
2
3
import subprocess
3
4
4
5
from CherryTomato .settings import STATE_TOMATO , STATE_BREAK , STATE_LONG_BREAK
5
6
6
7
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
+
7
33
class CommandExecutor :
8
34
def __init__ (self , settings ):
35
+ self .logger = classLogger (__name__ , self .__class__ .__name__ )
9
36
self .settings = settings
10
37
11
38
def onStart (self , status ):
@@ -22,13 +49,15 @@ def onStateChange(self, status):
22
49
23
50
def _execute (self , command , status = None ):
24
51
if command :
25
- command = self ._applyMacros (command , status )
52
+ cmd_with_macros = self ._applyMacros (command , status )
26
53
try :
27
54
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
29
58
)
30
- except Exception as e :
31
- print ( e )
59
+ except Exception :
60
+ self . logger . exception ( f'Error while executing command " { command } "' , exc_info = True )
32
61
33
62
@classmethod
34
63
def _applyMacros (cls , cmd , status = None ):
0 commit comments