summaryrefslogtreecommitdiff
path: root/kvmd/logging.py
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-10-28 06:51:51 +0300
committerDevaev Maxim <[email protected]>2018-10-28 07:03:14 +0300
commitab342111d0c88e13e0d0d4a79c1e14150a7d4ef3 (patch)
tree022ec24cc8b7fde70fba8833ef8a35fcad228e48 /kvmd/logging.py
parent1f54776ce0a1429a7ddcc4ac8b09054c48d67953 (diff)
log interface
Diffstat (limited to 'kvmd/logging.py')
-rw-r--r--kvmd/logging.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/kvmd/logging.py b/kvmd/logging.py
index 838e13d1..0efb9cb4 100644
--- a/kvmd/logging.py
+++ b/kvmd/logging.py
@@ -1,5 +1,13 @@
import sys
+import asyncio
import logging
+import time
+
+from typing import List
+from typing import Dict
+from typing import AsyncGenerator
+
+import systemd.journal
# =====
@@ -13,3 +21,41 @@ def get_logger(depth: int=1) -> logging.Logger:
break
name = frames[depth].f_globals["__name__"]
return logging.getLogger(name)
+
+
+class Log:
+ def __init__(
+ self,
+ services: List[str],
+ loop: asyncio.AbstractEventLoop,
+ ) -> None:
+
+ self.__services = services
+ self.__loop = loop
+
+ async def log(self, seek: int, follow: bool) -> AsyncGenerator[Dict, None]:
+ reader = systemd.journal.Reader()
+ reader.this_boot()
+ reader.this_machine()
+ reader.log_level(systemd.journal.LOG_DEBUG)
+ for service in self.__services:
+ reader.add_match(_SYSTEMD_UNIT=service)
+ if seek > 0:
+ reader.seek_realtime(float(time.time() - seek))
+
+ for entry in reader:
+ yield self.__entry_to_record(entry)
+
+ while follow:
+ entry = reader.get_next()
+ if entry:
+ yield self.__entry_to_record(entry)
+ else:
+ await asyncio.sleep(1)
+
+ def __entry_to_record(self, entry: Dict) -> Dict[str, Dict]:
+ return {
+ "dt": entry["__REALTIME_TIMESTAMP"],
+ "service": entry["_SYSTEMD_UNIT"],
+ "msg": entry["MESSAGE"].rstrip(),
+ }