#!/usr/bin/env python3
import logging
import asyncio
import json
import sys

import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

import aiohttp
from aiohttp import web

import litecord

logging.basicConfig(level=logging.DEBUG, \
    format='[%(levelname)7s] [%(name)s] %(message)s')

loggers_to_info = ['websockets.protocol']

log = logging.getLogger('litecord')

handler = logging.FileHandler('litecord.log')
handler.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - [%(levelname)s] [%(name)s] %(message)s')
handler.setFormatter(formatter)

log.addHandler(handler)

app = web.Application()

async def index(request):
    return web.Response(text='beep boop this is litecord!')

def shush_loggers():
    """Set some specific loggers to `INFO` level."""
    for logger in loggers_to_info:
        logging.getLogger(logger).setLevel(logging.INFO)

def main():    
    try:
        config_path = sys.argv[1]
    except IndexError:
        config_path = 'litecord_config.json'

    try:
        cfgfile = open(config_path, 'r')
    except FileNotFoundError:
        cfgfile = open('litecord_config.sample.json', 'r')

    shush_loggers()
    loop = asyncio.get_event_loop()
    flags = json.load(open(config_path, 'r'))
    app.router.add_get('/', index)

    litecord.init_server(app, flags, loop)

    try:
        loop.run_until_complete(litecord.start_all(app))
        server = app.litecord_server
        server.compliance()

        server.http_server = loop.run_until_complete(server.http_server)
        server.ws_server = loop.run_until_complete(server.ws_server)
        loop.create_task(litecord.server_sentry(server))
        loop.run_forever()
    except KeyboardInterrupt:
        log.info("Exiting from a CTRL-C...")
    except:
        log.error("Oh no! We received an error. Exiting.", exc_info=True)
    finally:
        app.litecord_server.shutdown()

    return 0

if __name__ == "__main__":
    sys.exit(main())

@@ -1456,24 +1456,61 @@
 mpliance()%0A%0A
+        log.debug('Running servers')%0A
         serv
@@ -1630,32 +1630,76 @@
 erver.ws_server)
+%0A%0A        log.debug('Running server sentry')
 %0A        loop.cr
@@ -1739,16 +1739,51 @@
 server))
+%0A%0A        log.debug('Running loop')
 %0A       
@@ -1849,17 +1849,17 @@
 og.info(
-%22
+'
 Exiting 
@@ -1874,17 +1874,17 @@
 TRL-C...
-%22
+'
 )%0A    ex
@@ -1906,14 +1906,18 @@
 og.e
-rror(%22
+xception('
 Oh n
@@ -1953,24 +1953,9 @@
 ing.
-%22, exc_info=True
+'
 )%0A  

add more logging