Fix traceback when notification daemon not running
When a desktop notification is displayed but there is no notification daemon running, an exception is raised. This fixes the traceback by adding a logger to the DesktopNotification class, and logging an error when the exception is caught. Fixes #453.
This commit is contained in:
parent
03df1a644a
commit
429c5d4865
@ -1,3 +1,6 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class BaseDesktopNotification:
|
class BaseDesktopNotification:
|
||||||
"""
|
"""
|
||||||
Class to display a desktop notification
|
Class to display a desktop notification
|
||||||
@ -9,12 +12,20 @@ class BaseDesktopNotification:
|
|||||||
:param timeout: Timeout in seconds for the notification. Zero means it needs to be dismissed by the user.
|
:param timeout: Timeout in seconds for the notification. Zero means it needs to be dismissed by the user.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, title, body, icon="dialog-information", urgency=1, timeout=0):
|
def __init__(self, title, body, icon="dialog-information", urgency=1,
|
||||||
|
timeout=0, log_level=logging.WARNING):
|
||||||
self.title = title
|
self.title = title
|
||||||
self.body = body
|
self.body = body
|
||||||
self.icon = icon
|
self.icon = icon
|
||||||
self.urgency = urgency
|
self.urgency = urgency
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
self.log_level = log_level
|
||||||
|
|
||||||
|
if self.__class__.__name__.startswith("i3pystatus"):
|
||||||
|
self.logger = logging.getLogger(self.__class__.__name__)
|
||||||
|
else:
|
||||||
|
self.logger = logging.getLogger("i3pystatus." + self.__class__.__name__)
|
||||||
|
self.logger.setLevel(self.log_level)
|
||||||
|
|
||||||
def display(self):
|
def display(self):
|
||||||
"""
|
"""
|
||||||
@ -55,4 +66,12 @@ else:
|
|||||||
if self.timeout:
|
if self.timeout:
|
||||||
notification.set_timeout(self.timeout)
|
notification.set_timeout(self.timeout)
|
||||||
notification.set_urgency(self.URGENCY_LUT[self.urgency])
|
notification.set_urgency(self.URGENCY_LUT[self.urgency])
|
||||||
|
try:
|
||||||
return notification.show()
|
return notification.show()
|
||||||
|
except Exception as exc:
|
||||||
|
self.logger.error(
|
||||||
|
'Failed to display desktop notification (is a '
|
||||||
|
'notification daemon running?)',
|
||||||
|
exc_info=self.log_level <= logging.DEBUG
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user