Skip to content

Commit

Permalink
sezanzeb#197 timeouts, service Requires, added key-mapper-control deb…
Browse files Browse the repository at this point in the history
…ug log file
  • Loading branch information
sezanzeb committed Nov 14, 2021
1 parent 10700d8 commit a150b06
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 25 deletions.
8 changes: 6 additions & 2 deletions bin/key-mapper-control
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import sys
import argparse
import logging

from keymapper.logger import logger, update_verbosity, log_info
from keymapper.logger import logger, update_verbosity, log_info, add_filehandler
from keymapper.config import config

# import keymapper modules as late as possible to make sure the correct
Expand Down Expand Up @@ -133,11 +133,13 @@ def communicate(options, daemon):
# if device was specified, autoload for that one. if None autoload
# for all devices.
if options.device is None:
logger.info('Autoloading all')
# timeout is not documented, for more info see
# https://github.com/LEW21/pydbus/blob/master/pydbus/proxy_method.py
daemon.autoload(timeout=10)
else:
group = require_group()
logger.info('Autoloading %s', options.device)
daemon.autoload_single(group.key, timeout=2)

if options.command == START:
Expand Down Expand Up @@ -234,7 +236,9 @@ if __name__ == '__main__':
log_info()
sys.exit(0)

update_verbosity(options.debug)
if options.debug:
add_filehandler('/var/log/key-mapper-control')
update_verbosity(True)

logger.debug('Call for "%s"', sys.argv)

Expand Down
3 changes: 2 additions & 1 deletion data/99-key-mapper.rules
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# udevadm monitor --property
# udevadm info --query=all --name=/dev/input/event3
# to test changes:
# sudo udevadm control --log-priority=debug
# sudo udevadm control --reload-rules
# journalctl -f
ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/key-mapper-control --command autoload --device $env{DEVNAME}"
ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/key-mapper-control --command autoload --device $env{DEVNAME} -d"
3 changes: 3 additions & 0 deletions data/key-mapper.service
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[Unit]
Description=Service to inject keycodes without the GUI application
# dbus is required for ipc between gui and key-mapper-control
Requires=dbus.service
After=dbus.service

[Service]
Type=dbus
Expand Down
14 changes: 9 additions & 5 deletions keymapper/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@


BUS_NAME = "keymapper.Control"
# timeout in seconds, see
# https://github.com/LEW21/pydbus/blob/cc407c8b1d25b7e28a6d661a29f9e661b1c9b964/pydbus/proxy.py
BUS_TIMEOUT = 10


class AutoloadHistory:
Expand Down Expand Up @@ -183,7 +186,7 @@ def connect(cls, fallback=True):
"""
try:
bus = SystemBus()
interface = bus.get(BUS_NAME)
interface = bus.get(BUS_NAME, timeout=BUS_TIMEOUT)
logger.info("Connected to the service")
except GLib.GError as error:
if not fallback:
Expand All @@ -208,7 +211,7 @@ def connect(cls, fallback=True):
# try a few times if the service was just started
for attempt in range(3):
try:
interface = bus.get(BUS_NAME)
interface = bus.get(BUS_NAME, timeout=BUS_TIMEOUT)
break
except GLib.GError as error:
logger.debug(
Expand All @@ -221,9 +224,10 @@ def connect(cls, fallback=True):
logger.error("Failed to connect to the service")
sys.exit(1)

config_path = get_config_path()
logger.debug('Telling service about "%s"', config_path)
interface.set_config_dir(get_config_path())
if USER != "root":
config_path = get_config_path()
logger.debug('Telling service about "%s"', config_path)
interface.set_config_dir(get_config_path())

return interface

Expand Down
34 changes: 22 additions & 12 deletions keymapper/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import time
import logging
import pkg_resources
from datetime import datetime

from keymapper.user import HOME

Expand Down Expand Up @@ -194,28 +195,37 @@ def update_verbosity(debug):
# since this is optional, just skip all exceptions
if not isinstance(error, ImportError):
logger.debug("Cannot use rich.traceback: %s", error)

logger.debug("Started debug logs at: %s", str(datetime.now()))
else:
logger.setLevel(logging.INFO)


def add_filehandler(log_path=LOG_PATH):
"""Clear the existing logfile and start logging to it."""
logger.info('This output is also stored in "%s"', LOG_PATH)

log_path = os.path.expanduser(log_path)
os.makedirs(os.path.dirname(log_path), exist_ok=True)
try:
log_path = os.path.expanduser(log_path)
os.makedirs(os.path.dirname(log_path), exist_ok=True)

if os.path.exists(log_path):
# keep the log path small, start from scratch each time
if os.path.isdir(log_path):
# used to be a folder < 0.8.0
shutil.rmtree(log_path)
else:
os.remove(log_path)

file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(Formatter())
if os.path.exists(log_path):
# the logfile should not be longer than 1000 lines to avoid overflowing
# the storage
with open(log_path, "r") as file:
content = file.readlines()[-200:] + ["---\n"]

with open(log_path, "w") as file:
file.truncate(0)
file.writelines(content)

file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(Formatter())

logger.info('Logging to "%s"', log_path)
logger.info('Logging to "%s"', log_path)

logger.addHandler(file_handler)
logger.addHandler(file_handler)
except PermissionError:
logger.debug('No permission to log to "%s"', log_path)
4 changes: 2 additions & 2 deletions readme/pylint.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testcases/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def set_config_dir(self, *args, **kwargs):
nonlocal set_config_dir_callcount
set_config_dir_callcount += 1

type(SystemBus()).get = lambda *args: FakeConnection()
type(SystemBus()).get = lambda *args, **kwargs: FakeConnection()
self.assertIsInstance(Daemon.connect(), FakeConnection)
self.assertEqual(set_config_dir_callcount, 1)

Expand Down
10 changes: 8 additions & 2 deletions tests/testcases/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ def test_clears_log(self):
path = os.path.join(tmp, "logger-test")
os.makedirs(os.path.dirname(path), exist_ok=True)
os.mknod(path)

with open(path, "w") as f:
f.write("foo")
f.write("line\n" * 1000 + "end")

add_filehandler(os.path.join(tmp, "logger-test"))
with open(path, "r") as f:
self.assertEqual(f.read(), "")
# it only keeps the newest information
content = f.readlines()
self.assertLess(len(content), 500)
self.assertEqual(content[-1], "end---\n")
# after "---" new log will appear

def test_debug(self):
path = os.path.join(tmp, "logger-test")
Expand Down

0 comments on commit a150b06

Please sign in to comment.