1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-09-11 15:08:33 +01:00

nixos/test-driver: add AbstractLogger interface

This commit is contained in:
Stefan Hertrampf 2024-04-08 15:41:33 +02:00
parent a6160e5763
commit b505db6f6d

View file

@ -3,6 +3,7 @@ import os
import sys import sys
import time import time
import unicodedata import unicodedata
from abc import ABC, abstractmethod
from contextlib import contextmanager from contextlib import contextmanager
from queue import Empty, Queue from queue import Empty, Queue
from typing import Any, Dict, Iterator from typing import Any, Dict, Iterator
@ -12,7 +13,43 @@ from xml.sax.xmlreader import AttributesImpl
from colorama import Fore, Style from colorama import Fore, Style
class Logger: class AbstractLogger(ABC):
@abstractmethod
def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
pass
@abstractmethod
@contextmanager
def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
pass
@abstractmethod
@contextmanager
def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
pass
@abstractmethod
def info(self, *args, **kwargs) -> None: # type: ignore
pass
@abstractmethod
def warning(self, *args, **kwargs) -> None: # type: ignore
pass
@abstractmethod
def error(self, *args, **kwargs) -> None: # type: ignore
pass
@abstractmethod
def log_serial(self, message: str, machine: str) -> None:
pass
@abstractmethod
def print_serial_logs(self, enable: bool) -> None:
pass
class Logger(AbstractLogger):
def __init__(self) -> None: def __init__(self) -> None:
self.logfile = os.environ.get("LOGFILE", "/dev/null") self.logfile = os.environ.get("LOGFILE", "/dev/null")
self.logfile_handle = codecs.open(self.logfile, "wb") self.logfile_handle = codecs.open(self.logfile, "wb")
@ -110,4 +147,4 @@ class Logger:
self.xml.endElement("nest") self.xml.endElement("nest")
rootlog = Logger() rootlog: AbstractLogger = Logger()