aboutsummaryrefslogtreecommitdiff
path: root/releng/__init__.py
blob: 179ea3e2b914671a308bd8689c2aa553445c5a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from xonsh.main import setup

setup()
del setup

import logging
import sys

import xonsh.base_shell

from . import environment
from . import create_release
from . import keys
from . import version
from . import cli
from . import docker
from . import docker_assemble
from . import gitutils


def setup_logging():
    """
    Sets up logging to work properly. The following are intended to work:
    - ipython/xonsh configuration files adding log handlers out of band
    - Reloading the module in xonsh/ipython not causing Bonus Loggers (which is
      why we check if there is already a handler. This also helps the previous
      case)
    - Importing the releng module from xonsh and poking at it interactively
    """

    LEVELS = {
        # Root logger must be DEBUG so that anything else can be DEBUG
        None: logging.DEBUG,
        # Everything in releng
        __name__: logging.DEBUG,
        # Log spam caused by prompt_toolkit
        'asyncio': logging.INFO,
    }

    for name, level in LEVELS.items():
        logger = logging.getLogger(name)
        logger.setLevel(level)

    root_logger = logging.getLogger()

    fmt = logging.Formatter('{asctime} {levelname} {name}: {message}',
                            datefmt='%b %d %H:%M:%S',
                            style='{')

    if not any(
            isinstance(h, logging.StreamHandler) for h in root_logger.handlers):
        stderr = sys.stderr
        # XXX: Horrible hack required by the virtual stderr xonsh uses for each entered
        # command getting closed after the command is run: we need to pull out
        # the real stderr because this survives across multiple command runs.
        #
        # This only applies when running xonsh in interactive mode and importing releng.
        if isinstance(sys.stderr, xonsh.base_shell._TeeStd):
            stderr = stderr.std  # type: ignore

        hand = logging.StreamHandler(stream=stderr)
        hand.set_name('releng root handler')
        hand.setFormatter(fmt)
        root_logger.addHandler(hand)


setup_logging()


def reload():
    import importlib
    importlib.reload(environment)
    importlib.reload(create_release)
    importlib.reload(keys)
    importlib.reload(version)
    importlib.reload(cli)
    importlib.reload(docker)
    importlib.reload(docker_assemble)
    importlib.reload(gitutils)