Skip to content

Commit 5f83395

Browse files
committed
Use SIGUSR1 to toggle debug mode
1 parent 9822dad commit 5f83395

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

emailproxy.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
__author__ = 'Simon Robinson'
77
__copyright__ = 'Copyright (c) 2023 Simon Robinson'
88
__license__ = 'Apache 2.0'
9-
__version__ = '2023-09-21' # ISO 8601 (YYYY-MM-DD)
9+
__version__ = '2023-10-05' # ISO 8601 (YYYY-MM-DD)
1010

1111
import abc
1212
import argparse
@@ -2225,8 +2225,7 @@ def __init__(self):
22252225
self.args = parser.parse_args()
22262226

22272227
Log.initialise(self.args.log_file)
2228-
if self.args.debug:
2229-
Log.set_level(logging.DEBUG)
2228+
self.toggle_debug(self.args.debug, log_message=False)
22302229

22312230
if self.args.config_file:
22322231
CONFIG_FILE_PATH = CACHE_STORE = self.args.config_file
@@ -2300,6 +2299,7 @@ def init_platforms(self):
23002299
PyObjCTools.MachSignals.signal(signal.SIGTERM, lambda _signum: self.exit(self.icon))
23012300
PyObjCTools.MachSignals.signal(signal.SIGQUIT, lambda _signum: self.exit(self.icon))
23022301
PyObjCTools.MachSignals.signal(signal.SIGHUP, lambda _signum: self.load_and_start_servers(self.icon))
2302+
PyObjCTools.MachSignals.signal(signal.SIGUSR1, lambda _: self.toggle_debug(Log.get_level() == logging.INFO))
23032303

23042304
else:
23052305
# for other platforms, or in no-GUI mode, just try to exit gracefully if SIGINT/SIGTERM/SIGQUIT is received
@@ -2311,6 +2311,10 @@ def init_platforms(self):
23112311
# allow config file reloading without having to stop/start - e.g.: pkill -SIGHUP -f emailproxy.py
23122312
# (we don't use linux_restart() here as it exits then uses nohup to restart, which may not be desirable)
23132313
signal.signal(signal.SIGHUP, lambda _signum, _frame: self.load_and_start_servers(self.icon))
2314+
if hasattr(signal, 'SIGUSR1'):
2315+
# use SIGUSR1 as a toggle for debug mode (e.g.: pkill -USR1 -f emailproxy.py) - please note that the
2316+
# proxy's handling of this signal may change in future if other actions are seen as more suitable
2317+
signal.signal(signal.SIGHUP, lambda _signum, _frame: self.toggle_debug(Log.get_level() == logging.INFO))
23142318

23152319
# noinspection PyUnresolvedReferences,PyAttributeOutsideInit
23162320
def macos_nsworkspace_notification_listener_(self, notification):
@@ -2343,7 +2347,8 @@ def create_icon(self):
23432347
pystray.MenuItem('Authorise account', pystray.Menu(self.create_authorisation_menu)),
23442348
pystray.Menu.SEPARATOR,
23452349
pystray.MenuItem('Start at login', self.toggle_start_at_login, checked=self.started_at_login),
2346-
pystray.MenuItem('Debug mode', self.toggle_debug, checked=lambda _: Log.get_level() == logging.DEBUG),
2350+
pystray.MenuItem('Debug mode', lambda _, item: self.toggle_debug(not item.checked),
2351+
checked=lambda _: Log.get_level() == logging.DEBUG),
23472352
pystray.Menu.SEPARATOR,
23482353
pystray.MenuItem('Quit %s' % APP_NAME, self.exit)))
23492354

@@ -2758,9 +2763,12 @@ def started_at_login(_):
27582763

27592764
return False
27602765

2761-
@staticmethod
2762-
def toggle_debug(_, item):
2763-
Log.set_level(logging.INFO if item.checked else logging.DEBUG)
2766+
def toggle_debug(self, enable_debug_mode, log_message=True):
2767+
Log.set_level(logging.DEBUG if enable_debug_mode else logging.INFO)
2768+
if log_message:
2769+
Log.info('Setting debug mode:', Log.get_level() == logging.DEBUG)
2770+
if hasattr(self, 'icon') and self.icon:
2771+
self.icon.update_menu()
27642772

27652773
# noinspection PyUnresolvedReferences
27662774
def notify(self, title, text):
@@ -2808,7 +2816,9 @@ def stop_servers(self):
28082816
def load_and_start_servers(self, icon=None, reload=True):
28092817
# we allow reloading, so must first stop any existing servers
28102818
self.stop_servers()
2811-
Log.info('Initialising', APP_NAME, '(version %s)' % __version__, 'from config file', CONFIG_FILE_PATH)
2819+
Log.info('Initialising', APP_NAME,
2820+
'(version %s)%s' % (__version__, ' in debug mode' if Log.get_level() == logging.DEBUG else ''),
2821+
'from config file', CONFIG_FILE_PATH)
28122822
if reload:
28132823
AppConfig.unload()
28142824
config = AppConfig.get()

0 commit comments

Comments
 (0)