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.
This commit is contained in:
T. Joseph Carter 2017-07-18 17:44:07 -07:00
parent a8d52b4216
commit 894225d1fa
4 changed files with 28 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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'))

16
cppo
View File

@ -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()