solaar: Remove outdated logger enabled checks

Logger enabled checks clutter the code unnecessarily. The checks are
now handled in a custom logger class. Eventually they can be completely
removed in the future.

Related #2664
This commit is contained in:
MattHag 2025-01-02 14:20:19 +01:00 committed by Peter F. Patel-Schneider
parent f5d80c30fa
commit 382e0b6797
5 changed files with 38 additions and 65 deletions

View File

@ -62,8 +62,7 @@ def _load():
loaded_config = _convert_json(loaded_config) loaded_config = _convert_json(loaded_config)
else: else:
path = None path = None
if logger.isEnabledFor(logging.DEBUG): logger.debug("load => %s", loaded_config)
logger.debug("load => %s", loaded_config)
global _config global _config
_config = _parse_config(loaded_config, path) _config = _parse_config(loaded_config, path)
@ -78,14 +77,13 @@ def _parse_config(loaded_config, config_path):
loaded_version = loaded_config[0] loaded_version = loaded_config[0]
discard_derived_properties = loaded_version != current_version discard_derived_properties = loaded_version != current_version
if discard_derived_properties: if discard_derived_properties:
if logger.isEnabledFor(logging.INFO): logger.info(
logger.info( "config file '%s' was generated by another version of solaar "
"config file '%s' was generated by another version of solaar " "(config: %s, current: %s). refreshing detected device capabilities",
"(config: %s, current: %s). refreshing detected device capabilities", config_path,
config_path, loaded_version,
loaded_version, current_version,
current_version, )
)
for device in loaded_config[1:]: for device in loaded_config[1:]:
assert isinstance(device, dict) assert isinstance(device, dict)
@ -154,8 +152,7 @@ def do_save():
try: try:
with open(_yaml_file_path, "w") as config_file: with open(_yaml_file_path, "w") as config_file:
yaml.dump(_config, config_file, default_flow_style=None, width=150) yaml.dump(_config, config_file, default_flow_style=None, width=150)
if logger.isEnabledFor(logging.INFO): logger.info("saved %s to %s", _config, _yaml_file_path)
logger.info("saved %s to %s", _config, _yaml_file_path)
except Exception as e: except Exception as e:
logger.error("failed to save to %s: %s", _yaml_file_path, e) logger.error("failed to save to %s: %s", _yaml_file_path, e)
@ -251,11 +248,9 @@ def persister(device):
break break
if not entry: if not entry:
if not device.online: # don't create entry for offline devices if not device.online: # don't create entry for offline devices
if logger.isEnabledFor(logging.INFO): logger.info("not setting up persister for offline device %s", device._name)
logger.info("not setting up persister for offline device %s", device._name)
return return
if logger.isEnabledFor(logging.INFO): logger.info("setting up persister for device %s", device.name)
logger.info("setting up persister for device %s", device.name)
entry = _DeviceEntry() entry = _DeviceEntry()
_config.append(entry) _config.append(entry)
entry.update(device.name, device.wpid, device.serial, modelId, unitId) entry.update(device.name, device.wpid, device.serial, modelId, unitId)

View File

@ -68,8 +68,7 @@ def watch_suspend_resume(
dbus_interface=_LOGIND_INTERFACE, dbus_interface=_LOGIND_INTERFACE,
path=_LOGIND_PATH, path=_LOGIND_PATH,
) )
if logger.isEnabledFor(logging.INFO): logger.info("connected to system dbus, watching for suspend/resume events")
logger.info("connected to system dbus, watching for suspend/resume events")
_BLUETOOTH_PATH_PREFIX = "/org/bluez/hci0/dev_" _BLUETOOTH_PATH_PREFIX = "/org/bluez/hci0/dev_"

View File

@ -134,9 +134,8 @@ def _parse_arguments():
logging.getLogger("").addHandler(stream_handler) logging.getLogger("").addHandler(stream_handler)
if not args.action: if not args.action:
if logger.isEnabledFor(logging.INFO): language, encoding = locale.getlocale()
language, encoding = locale.getlocale() logger.info("version %s, language %s (%s)", __version__, language, encoding)
logger.info("version %s, language %s (%s)", __version__, language, encoding)
return args return args

View File

@ -79,15 +79,13 @@ class SolaarListener(listener.EventsListener):
receiver.status_callback = self._status_changed receiver.status_callback = self._status_changed
def has_started(self): def has_started(self):
if logger.isEnabledFor(logging.INFO): logger.info("%s: notifications listener has started (%s)", self.receiver, self.receiver.handle)
logger.info("%s: notifications listener has started (%s)", self.receiver, self.receiver.handle)
nfs = self.receiver.enable_connection_notifications() nfs = self.receiver.enable_connection_notifications()
if logger.isEnabledFor(logging.WARNING): if not self.receiver.isDevice and not ((nfs if nfs else 0) & hidpp10_constants.NotificationFlag.WIRELESS.value):
if not self.receiver.isDevice and not ((nfs if nfs else 0) & hidpp10_constants.NotificationFlag.WIRELESS.value): logger.warning(
logger.warning( "Receiver on %s might not support connection notifications, GUI might not show its devices",
"Receiver on %s might not support connection notifications, GUI might not show its devices", self.receiver.path,
self.receiver.path, )
)
self.receiver.notification_flags = nfs self.receiver.notification_flags = nfs
self.receiver.notify_devices() self.receiver.notify_devices()
self._status_changed(self.receiver) self._status_changed(self.receiver)
@ -95,8 +93,7 @@ class SolaarListener(listener.EventsListener):
def has_stopped(self): def has_stopped(self):
r, self.receiver = self.receiver, None r, self.receiver = self.receiver, None
assert r is not None assert r is not None
if logger.isEnabledFor(logging.INFO): logger.info("%s: notifications listener has stopped", r)
logger.info("%s: notifications listener has stopped", r)
# because udev is not notifying us about device removal, make sure to clean up in _all_listeners # because udev is not notifying us about device removal, make sure to clean up in _all_listeners
_all_listeners.pop(r.path, None) _all_listeners.pop(r.path, None)
@ -144,8 +141,7 @@ class SolaarListener(listener.EventsListener):
if not device: if not device:
# Device was unpaired, and isn't valid anymore. # Device was unpaired, and isn't valid anymore.
# We replace it with a ghost so that the UI has something to work with while cleaning up. # We replace it with a ghost so that the UI has something to work with while cleaning up.
if logger.isEnabledFor(logging.INFO): logger.info("device %s was unpaired, ghosting", device)
logger.info("device %s was unpaired, ghosting", device)
device = _ghost(device) device = _ghost(device)
self.status_changed_callback(device, alert, reason) self.status_changed_callback(device, alert, reason)
@ -163,20 +159,17 @@ class SolaarListener(listener.EventsListener):
# a notification that came in to the device listener - strange, but nothing needs to be done here # a notification that came in to the device listener - strange, but nothing needs to be done here
if self.receiver.isDevice: if self.receiver.isDevice:
if logger.isEnabledFor(logging.DEBUG): logger.debug("Notification %s via device %s being ignored.", n, self.receiver)
logger.debug("Notification %s via device %s being ignored.", n, self.receiver)
return return
# DJ pairing notification - ignore - hid++ 1.0 pairing notification is all that is needed # DJ pairing notification - ignore - hid++ 1.0 pairing notification is all that is needed
if n.sub_id == 0x41 and n.report_id == base.DJ_MESSAGE_ID: if n.sub_id == 0x41 and n.report_id == base.DJ_MESSAGE_ID:
if logger.isEnabledFor(logging.INFO): logger.info("ignoring DJ pairing notification %s", n)
logger.info("ignoring DJ pairing notification %s", n)
return return
# a device notification # a device notification
if not (0 < n.devnumber <= 16): # some receivers have devices past their max # devices if not (0 < n.devnumber <= 16): # some receivers have devices past their max # devices
if logger.isEnabledFor(logging.WARNING): logger.warning("Unexpected device number (%s) in notification %s.", n.devnumber, n)
logger.warning("Unexpected device number (%s) in notification %s.", n.devnumber, n)
return return
already_known = n.devnumber in self.receiver already_known = n.devnumber in self.receiver
@ -221,8 +214,7 @@ class SolaarListener(listener.EventsListener):
# Apply settings every time the device connects # Apply settings every time the device connects
if n.sub_id == 0x41: if n.sub_id == 0x41:
if logger.isEnabledFor(logging.INFO): logger.info("connection %s for device wpid %s kind %s serial %s", n, dev.wpid, dev.kind, dev._serial)
logger.info("connection %s for device wpid %s kind %s serial %s", n, dev.wpid, dev.kind, dev._serial)
# If there are saved configs, bring the device's settings up-to-date. # If there are saved configs, bring the device's settings up-to-date.
# They will be applied when the device is marked as online. # They will be applied when the device is marked as online.
configuration.attach_to(dev) configuration.attach_to(dev)
@ -234,10 +226,8 @@ class SolaarListener(listener.EventsListener):
if self.receiver.pairing.lock_open and not already_known: if self.receiver.pairing.lock_open and not already_known:
# this should be the first notification after a device was paired # this should be the first notification after a device was paired
if logger.isEnabledFor(logging.WARNING): logger.warning("first notification was not a connection notification")
logger.warning("first notification was not a connection notification") logger.info("%s: pairing detected new device", self.receiver)
if logger.isEnabledFor(logging.INFO):
logger.info("%s: pairing detected new device", self.receiver)
self.receiver.pairing.new_device = dev self.receiver.pairing.new_device = dev
elif dev.online is None: elif dev.online is None:
dev.ping() dev.ping()
@ -253,19 +243,16 @@ def _process_bluez_dbus(device: Device, path, dictionary: dict, signature):
if device: if device:
if dictionary.get("Connected") is not None: if dictionary.get("Connected") is not None:
connected = dictionary.get("Connected") connected = dictionary.get("Connected")
if logger.isEnabledFor(logging.INFO): logger.info("bluez dbus for %s: %s", device, "CONNECTED" if connected else "DISCONNECTED")
logger.info("bluez dbus for %s: %s", device, "CONNECTED" if connected else "DISCONNECTED")
device.changed(connected, reason=i18n._("connected") if connected else i18n._("disconnected")) device.changed(connected, reason=i18n._("connected") if connected else i18n._("disconnected"))
elif device is not None: elif device is not None:
if logger.isEnabledFor(logging.INFO): logger.info("bluez cleanup for %s", device)
logger.info("bluez cleanup for %s", device)
_cleanup_bluez_dbus(device) _cleanup_bluez_dbus(device)
def _cleanup_bluez_dbus(device: Device): def _cleanup_bluez_dbus(device: Device):
"""Remove dbus signal receiver for device""" """Remove dbus signal receiver for device"""
if logger.isEnabledFor(logging.INFO): logger.info("bluez cleanup for %s", device)
logger.info("bluez cleanup for %s", device)
dbus.watch_bluez_connect(device.hid_serial, None) dbus.watch_bluez_connect(device.hid_serial, None)
@ -296,8 +283,7 @@ def _start(device_info: DeviceInfo):
def start_all(): def start_all():
stop_all() # just in case this it called twice in a row... stop_all() # just in case this it called twice in a row...
if logger.isEnabledFor(logging.INFO): logger.info("starting receiver listening threads")
logger.info("starting receiver listening threads")
for device_info in base.receivers_and_devices(): for device_info in base.receivers_and_devices():
_process_receiver_event(ACTION_ADD, device_info) _process_receiver_event(ACTION_ADD, device_info)
@ -306,8 +292,7 @@ def stop_all():
listeners = list(_all_listeners.values()) listeners = list(_all_listeners.values())
_all_listeners.clear() _all_listeners.clear()
if listeners: if listeners:
if logger.isEnabledFor(logging.INFO): logger.info("stopping receiver listening threads %s", listeners)
logger.info("stopping receiver listening threads %s", listeners)
for listener_thread in listeners: for listener_thread in listeners:
listener_thread.stop() listener_thread.stop()
configuration.save() configuration.save()
@ -319,8 +304,7 @@ def stop_all():
# after a resume, the device may have been off so mark its saved status to ensure # after a resume, the device may have been off so mark its saved status to ensure
# that the status is pushed to the device when it comes back # that the status is pushed to the device when it comes back
def ping_all(resuming=False): def ping_all(resuming=False):
if logger.isEnabledFor(logging.INFO): logger.info("ping all devices%s", " when resuming" if resuming else "")
logger.info("ping all devices%s", " when resuming" if resuming else "")
for listener_thread in _all_listeners.values(): for listener_thread in _all_listeners.values():
if listener_thread.receiver.isDevice: if listener_thread.receiver.isDevice:
if resuming: if resuming:
@ -363,8 +347,7 @@ def _process_add(device_info: DeviceInfo, retry):
if e.errno == errno.EACCES: if e.errno == errno.EACCES:
try: try:
output = subprocess.check_output(["/usr/bin/getfacl", "-p", device_info.path], text=True) output = subprocess.check_output(["/usr/bin/getfacl", "-p", device_info.path], text=True)
if logger.isEnabledFor(logging.WARNING): logger.warning("Missing permissions on %s\n%s.", device_info.path, output)
logger.warning("Missing permissions on %s\n%s.", device_info.path, output)
except Exception: except Exception:
pass pass
if retry: if retry:
@ -382,8 +365,7 @@ def _process_receiver_event(action, device_info):
assert action is not None assert action is not None
assert device_info is not None assert device_info is not None
assert _error_callback assert _error_callback
if logger.isEnabledFor(logging.INFO): logger.info("receiver event %s %s", action, device_info)
logger.info("receiver event %s %s", action, device_info)
# whatever the action, stop any previous receivers at this path # whatever the action, stop any previous receivers at this path
listener_thread = _all_listeners.pop(device_info.path, None) listener_thread = _all_listeners.pop(device_info.path, None)
if listener_thread is not None: if listener_thread is not None:

View File

@ -46,8 +46,7 @@ class TaskRunner(Thread):
def run(self): def run(self):
self.alive = True self.alive = True
if logger.isEnabledFor(logging.DEBUG): logger.debug("started")
logger.debug("started")
while self.alive: while self.alive:
task = self.queue.get() task = self.queue.get()
@ -59,5 +58,4 @@ class TaskRunner(Thread):
except Exception: except Exception:
logger.exception("calling %s", function) logger.exception("calling %s", function)
if logger.isEnabledFor(logging.DEBUG): logger.debug("stopped")
logger.debug("stopped")