From 894225d1fa9eeb0d5b424720a19715f39a9013db Mon Sep 17 00:00:00 2001 From: "T. Joseph Carter" Date: Tue, 18 Jul 2017 17:44:07 -0700 Subject: [PATCH] Use LOG, move logging setup to cppo The actual logging configuration belongs in the application, not a module. In a module, it's always going to do what the module says to do. And that's fine for cppo which is a script that works the way it does, but we have bigger plans for this code. It's now in the cppo module. We've stopped using log from blocksfree.logging, favoring LOG instead, so we have removed it. --- blocksfree/__init__.py | 2 +- blocksfree/diskimg.py | 10 +++++----- blocksfree/logging.py | 21 +++++++++------------ cppo | 16 +++++++++++++--- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/blocksfree/__init__.py b/blocksfree/__init__.py index b43af2c..c31b6b2 100644 --- a/blocksfree/__init__.py +++ b/blocksfree/__init__.py @@ -17,4 +17,4 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from . import legacy, diskimg -from .logging import log +from .logging import LOG diff --git a/blocksfree/diskimg.py b/blocksfree/diskimg.py index 3715884..89cae1c 100644 --- a/blocksfree/diskimg.py +++ b/blocksfree/diskimg.py @@ -82,7 +82,7 @@ class Disk: hdr.comment_offset : hdr.comment_offset + hdr.comment_len] if len(self.twoimg_comment) != hdr.comment_len: - log.warn('invalid 2mg comment: {} bytes ' + LOG.warn('invalid 2mg comment: {} bytes ' '(expected {} bytes)'.format( len(self.twoimg_comment), hdr.comment_len)) @@ -94,7 +94,7 @@ class Disk: hdr.creator_offset : hdr.creator_offset + hdr.creator_len] if len(self.twoimg_creator) != hdr.creator_len: - log.warn('invalid 2mg creator: {} bytes ' + LOG.warn('invalid 2mg creator: {} bytes ' '(expected {} bytes)'.format( len(self.twoimg_creator), hdr.creator_len)) @@ -104,12 +104,12 @@ class Disk: self.twoimg = hdr else: - log.warn('2mg header length: {} (expected 64 ' + LOG.warn('2mg header length: {} (expected 64 ' 'for version 1)'.format(hdr.hdr_len)) else: - log.warn('2mg version unsupported: {} (only support ' + LOG.warn('2mg version unsupported: {} (only support ' 'version 1)'.format(hdr.version)) else: - log.warn('2mg header not found: magic is {}'.format(hdr.magic)) + LOG.warn('2mg header not found: magic is {}'.format(hdr.magic)) self._raw_twoimg = None diff --git a/blocksfree/logging.py b/blocksfree/logging.py index 262ac26..e13b72f 100644 --- a/blocksfree/logging.py +++ b/blocksfree/logging.py @@ -44,9 +44,15 @@ special case, just use a line continuation immediately after your opening quotes. Another imperfect solution, but it does the job. """ -import sys -import logging import textwrap +import logging +# pylint: disable=unused-import +from logging import ( + CRITICAL, DEBUG, ERROR, FATAL, INFO, WARNING, + Formatter, + StreamHandler + ) # For export +# pylint: enable=unused-import from typing import List, Dict # pylint: disable=too-few-public-methods,missing-docstring @@ -98,13 +104,4 @@ class StyleAdapter(logging.LoggerAdapter): self.logger._log(level, Message(str(msg), args), (), **kwargs) # pylint: enable=protected-access -LOG = StyleAdapter(logging.getLogger(__name__)) - -# Set up our logging facility -# FIXME(tjcarter): get rid of log, let caller handle where it's going -log = LOG -_HANDLER = logging.StreamHandler(sys.stdout) -_FORMATTER = logging.Formatter('{message}', style='{') -_HANDLER.setFormatter(_FORMATTER) -LOG.logger.addHandler(_HANDLER) -LOG.setLevel(logging.DEBUG) +LOG = StyleAdapter(logging.getLogger('blocksfree')) diff --git a/cppo b/cppo index 75081a3..e035348 100755 --- a/cppo +++ b/cppo @@ -42,18 +42,28 @@ options: + after a file name indicates a GS/OS or Mac OS extended (forked) file. Wildcard matching (*) is not supported and images are not validated. -ShrinkIt support requires Nulib2. cppo requires xxxPython 3.5+.""" +ShrinkIt support requires Nulib2. cppo requires Python 3.5+.""" import sys import os import blocksfree.legacy +import blocksfree.logging as logging + +LOG = logging.LOG def usage(exitcode=1): print(sys.modules[__name__].__doc__) sys.exit(exitcode) def cppo(args: list): + # Setup logging + handler = logging.StreamHandler(sys.stdout) + formatter = logging.Formatter('{message}', style='{') + handler.setFormatter(formatter) + LOG.logger.addHandler(handler) + LOG.setLevel(logging.DEBUG) + g = blocksfree.legacy.g while True: # breaks when there are no more arguments starting with dash @@ -121,13 +131,13 @@ def cppo(args: list): elif len(target_path.rsplit("/", 1)) > 1: g.target_dir, g.target_name = target_path.rsplit("/", 1) if not os.path.isdir(g.target_dir): - log.critical("Directory {} not found.".format(g.target_dir)) + LOG.critical("Directory {} not found.".format(g.target_dir)) sys.exit(2) else: if not g.catalog_only: g.target_dir = args[2] if not os.path.isdir(g.target_dir): - log.critical("Directory {} not found.".format(g.target_dir)) + LOG.critical("Directory {} not found.".format(g.target_dir)) sys.exit(2) blocksfree.legacy.run_cppo()