#!/usr/bin/env python3
# Eryn Wells <eryn@erynwells.me>
'''
Logging for my shells.
'''

import argparse
import logging
import os
import os.path

_LOGFILE = os.path.expanduser('~/.shell.log')
_LOGGER = None

LOG_LEVELS = {
    'crit': logging.CRITICAL,
    'error': logging.ERROR,
    'warn': logging.WARNING,
    'info': logging.INFO,
    'debug': logging.DEBUG,
}

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-b', '--bold', action='store_true')
    parser.add_argument('-f', '--file', default=_LOGFILE)
    parser.add_argument('-l', '--level', default='info', choices=list(LOG_LEVELS.keys()))
    parser.add_argument('-n', '--noeol', action='store_true')
    parser.add_argument('msg')
    parser.add_argument('args', nargs=argparse.REMAINDER)
    args = parser.parse_args()
    return args

def logger(args):
    logger = logging.getLogger('')

    stdout_handler = logging.StreamHandler()
    formatter = logging.Formatter('\033[34m==>\033[39m {msg}', style='{')
    stdout_handler.setFormatter(formatter)
    logger.addHandler(stdout_handler)

    # TODO: Add file handler

    return logger

def main():
    args = parse_args()
    log = logger(args)

    level = LOG_LEVELS[os.environ.get('SHELL_LOG_LEVEL', 'info')]
    log.setLevel(level)

    try:
        message_log_level = LOG_LEVELS[args.level]
    except KeyError:
        message_log_level = logging.INFO

    log.log(message_log_level, args.msg, *args.args)

if __name__ == '__main__':
    main()
