Commit d04b84b5 authored by Matt Traudt's avatar Matt Traudt
Browse files

Make logger globally

parent 1d8a9ff6
import sbws.commands.client
import sbws.commands.init
import sbws.commands.server
from sbws.globals import make_logger
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
......@@ -9,6 +10,12 @@ VERSION = '0.0.1'
def create_parser():
p = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
p.add_argument(
'-v', '--verbose', action='count', default=0,
help='Increase log level verbosity from the configured value')
p.add_argument(
'-q', '--quiet', action='count', default=0,
help='Decrease log level verbosity from the configured value')
sub = p.add_subparsers(dest='command')
sbws.commands.client.gen_parser(sub)
sbws.commands.init.gen_parser(sub)
......@@ -19,7 +26,8 @@ def create_parser():
def main():
parser = create_parser()
args = parser.parse_args()
def_args = [args]
log = make_logger(args)
def_args = [args, log]
def_kwargs = {}
known_commands = {
'client': {'f': sbws.commands.client.main,
......
from ..lib.pastlylogger import PastlyLogger
from ..lib.circuitbuilder import GapsCircuitBuilder as CB
from ..lib.resultdump import ResultDump
from ..lib.resultdump import Result
......@@ -19,7 +18,6 @@ import time
import os
log = None
end_event = Event()
stream_building_lock = RLock()
......@@ -263,10 +261,9 @@ def gen_parser(sub):
'when authenticating to the server.')
def main(args):
def main(args, log_):
global log
log = PastlyLogger(debug='/dev/stdout', overwrite=['debug'],
log_threads=True)
log = log_
if not is_initted(os.getcwd()):
fail_hard('Directory isn\'t initted')
......
from sbws.globals import (G_INIT_FILE_MAP, is_initted, fail_hard)
from ..lib.pastlylogger import PastlyLogger
from argparse import ArgumentDefaultsHelpFormatter
import os
import shutil
log = None
def gen_parser(sub):
p = sub.add_parser('init', formatter_class=ArgumentDefaultsHelpFormatter)
def main(args):
def main(args, log_):
global log
log = PastlyLogger(debug='/dev/stdout', overwrite=['debug'],
log_threads=True)
log = log_
if is_initted(os.getcwd()):
fail_hard('Directory already seems to be initted')
......
from ..lib.pastlylogger import PastlyLogger
from ..util.simpleauth import authenticate_client
from ..util.simpleauth import is_good_serverside_password_file
from sbws.globals import (fail_hard, is_initted)
......@@ -9,8 +8,6 @@ import time
import os
log = None
MAX_SEND_PER_WRITE = 100*1024*1024
MAX_SEND_PER_WRITE = 4096
......@@ -98,10 +95,9 @@ def new_thread(args, sock):
return thread
def main(args):
def main(args, log_):
global log
log = PastlyLogger(debug='/dev/stdout', overwrite=['debug'],
log_threads=True)
log = log_
if not is_initted(os.getcwd()):
fail_hard('Directory isn\'t initted')
......
import os
from sbws.lib.pastlylogger import PastlyLogger
G_PKG_DIR = os.path.abspath(os.path.dirname(__file__))
......@@ -7,6 +8,7 @@ G_INIT_FILE_MAP = [
(os.path.join(G_PKG_DIR, 'passwords.txt.example'),
'passwords.txt', 'file'),
]
log = None
def is_initted(d):
......@@ -19,10 +21,56 @@ def is_initted(d):
return True
def fail_hard(*s):
def fail_hard(*s, log=None):
''' Optionally log something to stdout ... and then exit as fast as
possible '''
if s:
# log.error(*s)
print(*s)
if log:
log.error(*s)
else:
print(*s)
exit(1)
def make_logger(args): # noqa
def get_log_level_string(args):
arg_level = 2
if args.verbose:
arg_level += args.verbose
if args.quiet:
arg_level -= args.quiet
level = arg_level
if level <= 0:
return 'error'
elif level == 1:
return 'warn'
elif level == 2:
return 'notice'
elif level == 3:
return 'info'
elif level >= 4:
return 'debug'
fail_hard('This should not have been reached.')
def get_logger(level):
def_file = '/dev/stdout'
common_kwargs = {'log_threads': True}
if level == 'debug':
return PastlyLogger(debug=def_file, overwrite=['debug'],
**common_kwargs)
if level == 'info':
return PastlyLogger(info=def_file, overwrite=['info'],
**common_kwargs)
if level == 'notice':
return PastlyLogger(notice=def_file, overwrite=['notice'],
**common_kwargs)
if level == 'warn':
return PastlyLogger(warn=def_file, overwrite=['warn'],
**common_kwargs)
if level == 'error':
return PastlyLogger(error=def_file, overwrite=['error'],
**common_kwargs)
else:
fail_hard('Unknown log level', level)
level = get_log_level_string(args)
return get_logger(level)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment