You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.4 KiB
118 lines
3.4 KiB
from __future__ import (absolute_import, division, |
|
print_function, unicode_literals) |
|
from builtins import * # noqa: F401 |
|
|
|
import logging |
|
from getpass import getpass |
|
|
|
from chromalog.log import ( |
|
ColorizingStreamHandler, |
|
ColorizingFormatter, |
|
) |
|
from chromalog.colorizer import GenericColorizer, MonochromaticColorizer |
|
from colorama import Fore, Back, Style |
|
|
|
# magic; importing e.g. 'info' actually instantiates |
|
# that as a function that uses the color map |
|
# defined below. ( noqa because flake doesn't understand) |
|
from chromalog.mark.helpers.simple import ( # noqa: F401 |
|
debug, |
|
info, |
|
important, |
|
success, |
|
warning, |
|
error, |
|
critical, |
|
) |
|
|
|
# our chosen colorings for log messages in JM: |
|
jm_color_map = { |
|
'debug': (Style.DIM + Fore.LIGHTBLUE_EX, Style.RESET_ALL), |
|
'info': (Style.BRIGHT + Fore.BLUE, Style.RESET_ALL), |
|
'important': (Style.BRIGHT, Style.RESET_ALL), |
|
'success': (Fore.GREEN, Style.RESET_ALL), |
|
'warning': (Fore.YELLOW, Style.RESET_ALL), |
|
'error': (Fore.RED, Style.RESET_ALL), |
|
'critical': (Back.RED, Style.RESET_ALL), |
|
} |
|
|
|
class JMColorizer(GenericColorizer): |
|
default_color_map = jm_color_map |
|
|
|
jm_colorizer = JMColorizer() |
|
|
|
logFormatter = ColorizingFormatter( |
|
"%(asctime)s [%(levelname)s] %(message)s") |
|
log = logging.getLogger('joinmarket') |
|
log.setLevel(logging.DEBUG) |
|
|
|
joinmarket_alert = [''] |
|
core_alert = [''] |
|
debug_silence = [False] |
|
|
|
#TODO pass this through from client, bitcoin paramater: |
|
DUST_THRESHOLD = 2730 |
|
|
|
class JoinMarketStreamHandler(ColorizingStreamHandler): |
|
|
|
def __init__(self): |
|
super(JoinMarketStreamHandler, self).__init__(colorizer=jm_colorizer) |
|
|
|
def emit(self, record): |
|
if joinmarket_alert[0]: |
|
print('JoinMarket Alert Message: ' + joinmarket_alert[0]) |
|
if core_alert[0]: |
|
print('Core Alert Message: ' + core_alert[0]) |
|
if not debug_silence[0]: |
|
super(JoinMarketStreamHandler, self).emit(record) |
|
|
|
handler = JoinMarketStreamHandler() |
|
handler.setFormatter(logFormatter) |
|
log.addHandler(handler) |
|
|
|
def jmprint(msg, level="info"): |
|
""" Provides the ability to print messages |
|
with consistent formatting, outside the logging system |
|
(in case you don't want the standard log format). |
|
Example applications are: REPL style stuff, and/or |
|
some very important / user workflow affecting communication. |
|
Note that this exclusively for console printout, NOT for |
|
logging to file (chromalog will handle file streams |
|
properly, but this will not). |
|
""" |
|
if not level in jm_color_map.keys(): |
|
raise Exception("Unsupported formatting") |
|
|
|
# .colorize_message function does a .format() on the string, |
|
# which does not work with string-ified json; this should |
|
# result in output as intended: |
|
msg = msg.replace('{', '{{') |
|
msg = msg.replace('}', '}}') |
|
|
|
fmtfn = eval(level) |
|
print(jm_colorizer.colorize_message(fmtfn(msg))) |
|
|
|
def get_log(): |
|
""" |
|
provides joinmarket logging instance |
|
:return: log instance |
|
""" |
|
return log |
|
|
|
def set_logging_level(level): |
|
handler.setLevel(level) |
|
|
|
def set_logging_color(colored=False): |
|
if colored: |
|
handler.colorizer = jm_colorizer |
|
else: |
|
handler.colorizer = MonochromaticColorizer() |
|
|
|
def chunks(d, n): |
|
return [d[x:x + n] for x in range(0, len(d), n)] |
|
|
|
def get_password(msg): #pragma: no cover |
|
password = getpass(msg) |
|
if not isinstance(password, bytes): |
|
password = password.encode('utf-8') |
|
return password
|
|
|