diff options
author | Devaev Maxim <[email protected]> | 2018-06-28 03:58:34 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2018-06-28 03:58:34 +0300 |
commit | ab87784b75d6e51704f4314d7354e0051be8cd24 (patch) | |
tree | 79e8a02c50169bf0a743ab625e42ea93393384ca | |
parent | 055f3a141a80eae7ce29fa09bb06465a7f69fcd3 (diff) |
using default logger
-rw-r--r-- | kvmd/dev_requirements.txt | 1 | ||||
-rw-r--r-- | kvmd/kvmd.yaml | 2 | ||||
-rw-r--r-- | kvmd/kvmd/__init__.py | 37 | ||||
-rw-r--r-- | kvmd/kvmd/atx.py | 12 | ||||
-rw-r--r-- | kvmd/kvmd/streamer.py | 20 | ||||
-rw-r--r-- | kvmd/requirements.txt | 1 |
6 files changed, 32 insertions, 41 deletions
diff --git a/kvmd/dev_requirements.txt b/kvmd/dev_requirements.txt index 8452410a..4e3e1940 100644 --- a/kvmd/dev_requirements.txt +++ b/kvmd/dev_requirements.txt @@ -1,6 +1,5 @@ git+git://github.com/willbuckner/rpi-gpio-development-mock@master#egg=rpi aiohttp -contextlog pyyaml bumpversion tox diff --git a/kvmd/kvmd.yaml b/kvmd/kvmd.yaml index ecc181c8..8012d5e6 100644 --- a/kvmd/kvmd.yaml +++ b/kvmd/kvmd.yaml @@ -50,7 +50,7 @@ logging: (): contextlog.SmartFormatter style: "{" datefmt: "%H:%M:%S" - format: "[{asctime}] {app:10.10} {fg_bold_purple}{name:20.20} {log_color}{levelname:>7}{reset} {message} -- {cyan}{_extra}{reset}" + format: "[{asctime}] {name:15.15} {levelname:>7} --- {message}" handlers: console: diff --git a/kvmd/kvmd/__init__.py b/kvmd/kvmd/__init__.py index 5376ca28..0074f6bf 100644 --- a/kvmd/kvmd/__init__.py +++ b/kvmd/kvmd/__init__.py @@ -10,10 +10,6 @@ from typing import Set from typing import Callable from typing import Optional -from contextlog import get_logger -from contextlog import patch_logging -from contextlog import patch_threading - from RPi import GPIO import aiohttp @@ -25,6 +21,9 @@ from .streamer import Streamer # ===== +_logger = logging.getLogger(__name__) + + def _system_task(method: Callable) -> Callable: async def wrap(self: "_Application") -> None: try: @@ -32,7 +31,7 @@ def _system_task(method: Callable) -> Callable: except asyncio.CancelledError: pass except Exception: - get_logger().exception("Unhandled exception") + _logger.exception("Unhandled exception") raise SystemExit(1) return wrap @@ -83,7 +82,7 @@ class _Application: app=app, host=self.__config["server"]["host"], port=self.__config["server"]["port"], - print=(lambda text: [get_logger().info(line.strip()) for line in text.strip().splitlines()]), + print=(lambda text: [_logger.info(line.strip()) for line in text.strip().splitlines()]), # type: ignore ) async def __root_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response: @@ -103,27 +102,23 @@ class _Application: return ws async def __on_shutdown(self, _: aiohttp.web.Application) -> None: - logger = get_logger() - - logger.info("Cancelling system tasks ...") + _logger.info("Cancelling system tasks ...") for task in self.__system_tasks: task.cancel() await asyncio.gather(*self.__system_tasks) - logger.info("Disconnecting clients ...") + _logger.info("Disconnecting clients ...") for ws in list(self.__sockets): await self.__remove_socket(ws) async def __on_cleanup(self, _: aiohttp.web.Application) -> None: - logger = get_logger() - if self.__streamer.is_running(): await self.__streamer.stop() - logger.info("Cleaning up GPIO ...") + _logger.info("Cleaning up GPIO ...") GPIO.cleanup() - logger.info("Bye-bye") + _logger.info("Bye-bye") @_system_task async def __stream_controller(self) -> None: @@ -175,31 +170,27 @@ class _Application: if method: await method() return None - get_logger().warning("Received incorrect command: %r", command) + _logger.warning("Received an incorrect command: %r", command) return "ERROR incorrect command" async def __register_socket(self, ws: aiohttp.web.WebSocketResponse) -> None: async with self.__sockets_lock: self.__sockets.add(ws) - get_logger().info("Registered new client socket: remote=%s; id=%d; active=%d", - ws._req.remote, id(ws), len(self.__sockets)) # pylint: disable=protected-access + _logger.info("Registered new client socket: remote=%s; id=%d; active=%d", + ws._req.remote, id(ws), len(self.__sockets)) # pylint: disable=protected-access async def __remove_socket(self, ws: aiohttp.web.WebSocketResponse) -> None: async with self.__sockets_lock: try: self.__sockets.remove(ws) - get_logger().info("Removed client socket: remote=%s; id=%d; active=%d", - ws._req.remote, id(ws), len(self.__sockets)) # pylint: disable=protected-access + _logger.info("Removed client socket: remote=%s; id=%d; active=%d", + ws._req.remote, id(ws), len(self.__sockets)) # pylint: disable=protected-access await ws.close() except Exception: pass def main() -> None: - patch_logging() - patch_threading() - get_logger(app="kvmd") - parser = argparse.ArgumentParser() parser.add_argument("-c", "--config", default="kvmd.yaml", metavar="<path>") options = parser.parse_args() diff --git a/kvmd/kvmd/atx.py b/kvmd/kvmd/atx.py index 237eacec..ff596a87 100644 --- a/kvmd/kvmd/atx.py +++ b/kvmd/kvmd/atx.py @@ -1,13 +1,15 @@ import asyncio +import logging from typing import Tuple -from contextlog import get_logger - from RPi import GPIO # ===== +_logger = logging.getLogger(__name__) + + class Atx: def __init__( self, @@ -42,15 +44,15 @@ class Atx: async def click_power(self) -> None: if (await self.__click(self.__power_switch, self.__click_delay)): - get_logger().info("Clicked power") + _logger.info("Clicked power") async def click_power_long(self) -> None: if (await self.__click(self.__power_switch, self.__long_click_delay)): - get_logger().info("Clicked power (long press)") + _logger.info("Clicked power (long press)") async def click_reset(self) -> None: if (await self.__click(self.__reset_switch, self.__click_delay)): - get_logger().info("Clicked reset") + _logger.info("Clicked reset") async def __click(self, pin: int, delay: float) -> bool: if not self.__lock.locked(): diff --git a/kvmd/kvmd/streamer.py b/kvmd/kvmd/streamer.py index 19eb31c7..8e542e5f 100644 --- a/kvmd/kvmd/streamer.py +++ b/kvmd/kvmd/streamer.py @@ -1,15 +1,17 @@ import asyncio import asyncio.subprocess +import logging from typing import Dict from typing import Optional -from contextlog import get_logger - from RPi import GPIO # ===== +_logger = logging.getLogger(__name__) + + class Streamer: # pylint: disable=too-many-instance-attributes def __init__( self, @@ -41,13 +43,13 @@ class Streamer: # pylint: disable=too-many-instance-attributes async def start(self) -> None: assert not self.__proc_task - get_logger().info("Starting mjpg_streamer ...") + _logger.info("Starting mjpg_streamer ...") await self.__set_hw_enabled(True) self.__proc_task = self.__loop.create_task(self.__process()) async def stop(self) -> None: assert self.__proc_task - get_logger().info("Stopping mjpg_streamer ...") + _logger.info("Stopping mjpg_streamer ...") self.__proc_task.cancel() await asyncio.gather(self.__proc_task, return_exceptions=True) await self.__set_hw_enabled(False) @@ -65,8 +67,6 @@ class Streamer: # pylint: disable=too-many-instance-attributes await asyncio.sleep(self.__sync_delay) async def __process(self) -> None: - logger = get_logger() - proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member while True: try: @@ -75,13 +75,13 @@ class Streamer: # pylint: disable=too-many-instance-attributes stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, ) - logger.info("Started mjpg_streamer pid=%d: %s", proc.pid, self.__cmd) + _logger.info("Started mjpg_streamer pid=%d: %s", proc.pid, self.__cmd) empty = 0 while proc.returncode is None: line = (await proc.stdout.readline()).decode(errors="ignore").strip() if line: - logger.info("mjpg_streamer: %s", line) + _logger.info("mjpg_streamer: %s", line) empty = 0 else: empty += 1 @@ -95,9 +95,9 @@ class Streamer: # pylint: disable=too-many-instance-attributes break except Exception as err: if proc: - logger.error("Unexpected finished mjpg_streamer pid=%d with retcode=%d", proc.pid, proc.returncode) + _logger.error("Unexpected finished mjpg_streamer pid=%d with retcode=%d", proc.pid, proc.returncode) else: - logger.error("Can't start mjpg_streamer: %s", err) + _logger.error("Can't start mjpg_streamer: %s", err) await asyncio.sleep(1) if proc: diff --git a/kvmd/requirements.txt b/kvmd/requirements.txt index f7a1faff..f688298d 100644 --- a/kvmd/requirements.txt +++ b/kvmd/requirements.txt @@ -1,4 +1,3 @@ RPi.GPIO aiohttp -contextlog pyyaml |