mirror of https://github.com/nucypher/nucypher.git
Clean up StdoutEmitter and log to the logging system where appropriate.
parent
e8828f0c14
commit
83e0ea153a
|
@ -158,7 +158,6 @@ class BlockchainInterface:
|
|||
|
||||
@staticmethod
|
||||
def __default_on_broadcast(tx: PendingTx):
|
||||
# TODO review use of emitter - #3482
|
||||
emitter = StdoutEmitter()
|
||||
max_cost, max_price_gwei, tx_type = get_tx_cost_data(tx.params)
|
||||
emitter.message(
|
||||
|
@ -623,7 +622,6 @@ class BlockchainInterface:
|
|||
#
|
||||
# Broadcast
|
||||
#
|
||||
# TODO review use of emitter - #3482
|
||||
emitter.message(
|
||||
f"Broadcasting {transaction_name} {tx_type} Transaction ({max_cost} @ {max_price_gwei} gwei)",
|
||||
color="yellow",
|
||||
|
|
|
@ -1,33 +1,16 @@
|
|||
import os
|
||||
from functools import partial
|
||||
from typing import Callable
|
||||
|
||||
import click
|
||||
|
||||
from nucypher.utilities.logging import Logger
|
||||
|
||||
|
||||
def null_stream():
|
||||
return open(os.devnull, 'w')
|
||||
|
||||
|
||||
class StdoutEmitter:
|
||||
|
||||
class MethodNotFound(BaseException):
|
||||
"""Cannot find interface method to handle request"""
|
||||
|
||||
transport_serializer = str
|
||||
default_color = 'white'
|
||||
|
||||
# sys.stdout.write() TODO: doesn't work well with click_runner's output capture
|
||||
default_sink_callable = partial(print, flush=True)
|
||||
|
||||
def __init__(self,
|
||||
sink: Callable = None,
|
||||
verbosity: int = 1):
|
||||
|
||||
self.name = self.__class__.__name__.lower()
|
||||
self.sink = sink or self.default_sink_callable
|
||||
self.verbosity = verbosity
|
||||
self.log = Logger(self.name)
|
||||
|
||||
|
@ -41,7 +24,12 @@ class StdoutEmitter:
|
|||
bold: bool = False,
|
||||
verbosity: int = 1):
|
||||
self.echo(message, color=color or self.default_color, bold=bold, verbosity=verbosity)
|
||||
self.log.debug(message)
|
||||
# these are application messages that are desired to be
|
||||
# printed to stdout (with or w/o console logging); send to logger
|
||||
if verbosity > 1:
|
||||
self.log.debug(message)
|
||||
else:
|
||||
self.log.info(message)
|
||||
|
||||
def echo(self,
|
||||
message: str = None,
|
||||
|
@ -49,21 +37,18 @@ class StdoutEmitter:
|
|||
bold: bool = False,
|
||||
nl: bool = True,
|
||||
verbosity: int = 0):
|
||||
# these are user interactions; don't send to logger
|
||||
if verbosity <= self.verbosity:
|
||||
click.secho(message=message, fg=color or self.default_color, bold=bold, nl=nl)
|
||||
|
||||
def banner(self, banner):
|
||||
# these are purely for banners; don't send to logger
|
||||
if self.verbosity >= 1:
|
||||
click.echo(banner)
|
||||
|
||||
def error(self, e):
|
||||
e_str = str(e)
|
||||
if self.verbosity >= 1:
|
||||
e_str = str(e)
|
||||
click.echo(message=e_str, color="red")
|
||||
self.log.info(e_str)
|
||||
|
||||
def get_stream(self, verbosity: int = 0):
|
||||
if verbosity <= self.verbosity:
|
||||
return click.get_text_stream('stdout')
|
||||
else:
|
||||
return null_stream()
|
||||
# some kind of error; send to logger
|
||||
self.log.error(e_str)
|
||||
|
|
Loading…
Reference in New Issue