mirror of https://github.com/iKarith/cppo-ng.git
Create blocksfree package for logger
The section of cppo containing the logging code has been moved to its own very short module inside a (bare) Python package. This is messily done for now, but I wanted this to be a minimal commit.
This commit is contained in:
parent
60b9d1f3c6
commit
82d851e39a
|
@ -0,0 +1,33 @@
|
|||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
### LOGGING
|
||||
# *sigh* No clean/simple way to use str.format() type log strings without
|
||||
# jumping through a few hoops
|
||||
|
||||
class Message(object):
|
||||
def __init__(self, fmt, args):
|
||||
self.fmt = fmt
|
||||
self.args = args
|
||||
|
||||
def __str__(self):
|
||||
return self.fmt.format(*self.args)
|
||||
|
||||
class StyleAdapter(logging.LoggerAdapter):
|
||||
def __init__(self, logger, extra=None):
|
||||
super(StyleAdapter, self).__init__(logger, extra or {})
|
||||
|
||||
def log(self, level, msg, *args, **kwargs):
|
||||
if self.isEnabledFor(level):
|
||||
msg, kwargs = self.process(msg, kwargs)
|
||||
self.logger._log(level, Message(str(msg), args), (), **kwargs)
|
||||
|
||||
log = StyleAdapter(logging.getLogger(__name__))
|
||||
|
||||
# Set up our logging facility
|
||||
_handler = logging.StreamHandler(sys.stdout)
|
||||
_formatter = logging.Formatter('%(levelname)s: %(message)s')
|
||||
_handler.setFormatter(_formatter)
|
||||
log.logger.addHandler(_handler)
|
||||
log.setLevel(logging.DEBUG)
|
34
cppo
34
cppo
|
@ -35,12 +35,13 @@ import errno
|
|||
import uuid # for temp directory
|
||||
import subprocess
|
||||
#import tempfile # not used, but should be for temp directory?
|
||||
import logging
|
||||
import struct
|
||||
from typing import Sequence
|
||||
from collections import namedtuple
|
||||
from binascii import a2b_hex, b2a_hex
|
||||
|
||||
from blocksfree.logging import log
|
||||
|
||||
class Globals:
|
||||
pass
|
||||
|
||||
|
@ -1010,38 +1011,7 @@ def hexdump(
|
|||
return '\n'.join(out)
|
||||
|
||||
|
||||
### LOGGING
|
||||
# *sigh* No clean/simple way to use str.format() type log strings without
|
||||
# jumping through a few hoops
|
||||
|
||||
class Message(object):
|
||||
def __init__(self, fmt, args):
|
||||
self.fmt = fmt
|
||||
self.args = args
|
||||
|
||||
def __str__(self):
|
||||
return self.fmt.format(*self.args)
|
||||
|
||||
class StyleAdapter(logging.LoggerAdapter):
|
||||
def __init__(self, logger, extra=None):
|
||||
super(StyleAdapter, self).__init__(logger, extra or {})
|
||||
|
||||
def log(self, level, msg, *args, **kwargs):
|
||||
if self.isEnabledFor(level):
|
||||
msg, kwargs = self.process(msg, kwargs)
|
||||
self.logger._log(level, Message(str(msg), args), (), **kwargs)
|
||||
|
||||
log = StyleAdapter(logging.getLogger(__name__))
|
||||
|
||||
|
||||
def main(args: list):
|
||||
# Set up our logging facility
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
formatter = logging.Formatter('%(levelname)s: %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
log.logger.addHandler(handler)
|
||||
log.setLevel(logging.DEBUG)
|
||||
|
||||
while True: # breaks when there are no more arguments starting with dash
|
||||
if len(args) == 1:
|
||||
usage()
|
||||
|
|
Loading…
Reference in New Issue