Skip to content

Commit

Permalink
Changed logging to prevent log file creation at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
comrumino committed Apr 16, 2020
1 parent 2489aa6 commit 527e8ad
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 59 deletions.
5 changes: 3 additions & 2 deletions asinstaller/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@


__version__ = '2.2.7'
logger = setup_logger('asinstaller.{}'.format(__name__))
logger.debug('Version: {}'.format(__version__))


def main():
try:
init_logger_handles()
logger = get_logger(__name__)
logger.debug(f'Version: {__version__}')
# Pre-checks
if geteuid() != 0:
print_error("Please run as root")
Expand Down
68 changes: 31 additions & 37 deletions asinstaller/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import io
import inspect
import logging
import os
from functools import wraps

usr_cfg = {}

Expand Down Expand Up @@ -32,40 +30,9 @@
'10': 'ro_RO.UTF-8', '11': 'ru_RU.UTF-8',
'12': 'sv_SE.UTF-8'}

FNULL = open(os.devnull, 'w')


TOPLEVEL_NAME = 'asinstaller' # imports
FHANDLE_NAME = 'Installer Log'
SHANDLE_NAME = 'Unit Test String Buffer'
# by default the root logger is created with a log level of warning; gotta catch 'em all, so set to NOTSET
root_logger = logging.getLogger()
root_logger.setLevel(logging.NOTSET)
# asinstaller is the top-level logger and all modules follow the period-separated hierarchical structure. So,
# for all loggers asinstaller is an ancestor. Given messages are propagated to ancestors and their handles,
# it is sufficient to configure asinstaller.
logger = logging.getLogger(TOPLEVEL_NAME)
if all([hndlr.name not in set([FHANDLE_NAME, SHANDLE_NAME]) for hndlr in logger.handlers]) or True:
logger.setLevel(logging.DEBUG) # NOTSET only traverses until another level is found, so DEBUG is preferred
# add a file handle to LOG_FILE (fhandle), handle for standard io (stdio_handle), and a handle for unit testing (shandle)
fhandle = logging.FileHandler(LOG_FILE, mode='a+')
fhandle.name = FHANDLE_NAME
fhandle.setLevel(logging.DEBUG)
shandle = logging.StreamHandler(io.StringIO())
shandle.name = SHANDLE_NAME
shandle.setLevel(logging.DEBUG)
stdio_handle = logging.StreamHandler()
stdio_handle.setLevel(logging.INFO)
fmt = logging.Formatter(fmt='%(levelname)s - %(message)s', datefmt='%m-%d %H:%M')
fhandle.setFormatter(fmt)
shandle.setFormatter(fmt)
stdio_handle.setFormatter(fmt)
logger.addHandler(fhandle)
logger.addHandler(shandle)
logger.addHandler(stdio_handle)


logger = logging.getLogger(__name__)
FNULL = open(os.devnull, 'w') # TODO, oh my... resource acquisition and initialization please...
TOPLEVEL_NAME = 'asinstaller'


class WhitespaceRemovingFormatter(logging.Formatter):
Expand All @@ -74,5 +41,32 @@ def format(self, record):
return super(WhitespaceRemovingFormatter, self).format(record)


def setup_logger(filename):
return logging.getLogger(filename)
def init_logger_handles():
"""scopes loggers by package.filename, scope collision is an accepted flaw"""
FHANDLE_NAME = 'Installer Log'
SHANDLE_NAME = 'Unit Test String Buffer'
root_logger = logging.getLogger()
root_logger.setLevel(logging.NOTSET)
top_logger = logging.getLogger(TOPLEVEL_NAME)
if all([hndlr.name not in set([FHANDLE_NAME, SHANDLE_NAME]) for hndlr in top_logger.handlers]):
top_logger.setLevel(logging.DEBUG) # NOTSET only traverses until another level is found, so DEBUG is preferred
# add a file handler to LOG_FILE, handle for standard io (stdio_handle), and a handle for units (shandle)
fhandle = logging.FileHandler(LOG_FILE, mode='a+')
fhandle.name = FHANDLE_NAME
fhandle.setLevel(logging.DEBUG)
shandle = logging.StreamHandler(io.StringIO())
shandle.name = SHANDLE_NAME
shandle.setLevel(logging.DEBUG)
stdio_handle = logging.StreamHandler()
stdio_handle.setLevel(logging.INFO)
fmt = logging.Formatter(fmt='%(levelname)s - %(message)s', datefmt='%m-%d %H:%M')
fhandle.setFormatter(fmt)
shandle.setFormatter(fmt)
stdio_handle.setFormatter(fmt)
top_logger.addHandler(fhandle)
top_logger.addHandler(shandle)
top_logger.addHandler(stdio_handle)


def get_logger(filename):
return logging.getLogger(f'{TOPLEVEL_NAME}.{filename}')
2 changes: 1 addition & 1 deletion asinstaller/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import shutil
import os

logger = setup_logger(__name__)
logger = get_logger(__name__)


def base(): # noqa
Expand Down
2 changes: 1 addition & 1 deletion asinstaller/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .config import *


logger = setup_logger(__name__)
logger = get_logger(__name__)


class LogHandler(object):
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/auto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from . import mbr
from ... import menus
from ...utils import system
from ...config import usr_cfg, setup_logger
from ...config import usr_cfg, get_logger


logger = setup_logger(__name__)
logger = get_logger(__name__)


def partition():
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/auto/gpt.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from asinstaller.utils import *
from asinstaller.config import usr_cfg, setup_logger
from asinstaller.config import usr_cfg, get_logger

logger = setup_logger(__name__)
logger = get_logger(__name__)

def uefi():
if usr_cfg['swap_space']:
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/auto/mbr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from asinstaller.utils import *
from asinstaller.config import usr_cfg, setup_logger
from asinstaller.config import usr_cfg, get_logger

logger = setup_logger(__name__)
logger = get_logger(__name__)

def format():
if usr_cfg['swap_space']:
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/encrypted/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from . import gpt
from . import mbr
from ...utils import *
from ...config import usr_cfg, setup_logger
from ...config import usr_cfg, get_logger


logger = setup_logger(__name__)
logger = get_logger(__name__)


def partition():
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/encrypted/gpt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from ...utils import *
from ...config import usr_cfg, setup_logger
from ...config import usr_cfg, get_logger


logger = setup_logger(__name__)
logger = get_logger(__name__)


def uefi():
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/encrypted/mbr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from ...utils import *
from ...config import usr_cfg, setup_logger
from ...config import usr_cfg, get_logger


logger = setup_logger(__name__)
logger = get_logger(__name__)


def format():
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/manual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import time
from .utils import format_partition, mount
from ...utils import *
from ...config import usr_cfg, setup_logger
from ...config import usr_cfg, get_logger


logger = setup_logger(__name__)
logger = get_logger(__name__)


def partition():
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/partitions/manual/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

import logging
import time
from ...config import usr_cfg, setup_logger, COLORS
from ...config import usr_cfg, get_logger, COLORS
from ...utils import print_error, print_warning, print_title, print_info,\
cinput, query_yes_no, system, satisfy_dep


logger = setup_logger(__name__)
logger = get_logger(__name__)


def format_partition_type(i):
Expand Down
4 changes: 2 additions & 2 deletions asinstaller/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from threading import Thread, Lock
# installer modules
from . import menus
from .config import COLORS, setup_logger, CONFIG_FILE, usr_cfg, FNULL, CRASH_FILE
from .config import COLORS, get_logger, CONFIG_FILE, usr_cfg, FNULL, CRASH_FILE


logger = setup_logger(__name__)
logger = get_logger(__name__)


def print_error(msg):
Expand Down

0 comments on commit 527e8ad

Please sign in to comment.