import logging
from logging import Handler, LogRecord

from systools import sys_log_error, sys_log_debug, sys_log_info, sys_log_warning


class HvLogFormatter(logging.Formatter):

    def __init__(self, fmt=None, datefmt=None, style='%'):
        if fmt is None:
            fmt = "%(filename)s:%(lineno)s:%(funcName)s - %(message)s"
        super(HvLogFormatter, self).__init__(fmt, datefmt)


class HvLogHandler(Handler):

    def __init__(
        self,                     # type: HvLogHandler
        level_no=logging.ERROR,   # type: int
    ):
        super(HvLogHandler, self).__init__(level_no)
        self.log_func = {
            logging.DEBUG: sys_log_debug,
            logging.INFO: sys_log_info,
            logging.WARNING: sys_log_warning,
            logging.ERROR: sys_log_error,
        }
        self.setFormatter(HvLogFormatter())

    def emit(
        self,    # type: HvLogHandler
        record,  # type: LogRecord
    ):
        # type: (...) -> None

        log_func = sys_log_debug
        if record.levelno in self.log_func:
            log_func = self.log_func[record.levelno]
        log_func(self.format(record))
