# Copyright (c) 2014 Wieland Hoffmann
# License: MIT, see LICENSE for details
import argparse
import logging
import multiprocessing


from . import config
from .indexing import reindex
from .schema import SCHEMA


logger = logging.getLogger("sir")


def watch(args):
    raise NotImplementedError


def main():
    loghandler = logging.StreamHandler()
    formatter = logging.Formatter(fmt="%(processName)s %(asctime)s  %(levelname)s: %(message)s")
    loghandler.setFormatter(formatter)
    logger.addHandler(loghandler)

    mplogger = multiprocessing.get_logger()
    mplogger.setLevel(logging.ERROR)
    mplogger.addHandler(loghandler)

    parser = argparse.ArgumentParser()
    parser.add_argument("-d", "--debug", action="store_true")
    parser.add_argument("--sqltimings", action="store_true")
    subparsers = parser.add_subparsers()

    reindex_parser = subparsers.add_parser("reindex", help="Reindexes all or a single entity type")
    reindex_parser.set_defaults(func=reindex)
    reindex_parser.add_argument('--entities', action='append', help="""Which
        entity types to index.

        Available are: %s""" % (", ".join(SCHEMA.keys())))

    watch_parser = subparsers.add_parser("watch", help="Watches for incoming messages on an AMQP queue")
    watch_parser.set_defaults(func=watch)

    args = parser.parse_args()
    if args.debug:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if args.sqltimings:
        from sqlalchemy import event
        from sqlalchemy.engine import Engine
        import time

        sqltimelogger = logging.getLogger("sqltimer")
        sqltimelogger.setLevel(logging.DEBUG)
        sqltimelogger.addHandler(loghandler)

        @event.listens_for(Engine, "before_cursor_execute")
        def before_cursor_execute(conn, cursor, statement,
                                  parameters, context, executemany):
            conn.info.setdefault('query_start_time', []).append(time.time())
            sqltimelogger.debug("Start Query: %s" % statement)

        @event.listens_for(Engine, "after_cursor_execute")
        def after_cursor_execute(conn, cursor, statement,
                                 parameters, context, executemany):
            total = time.time() - conn.info['query_start_time'].pop(-1)
            sqltimelogger.debug("Query Complete!")
            sqltimelogger.debug("Total Time: %f" % total)

    config.read_config()
    func = args.func
    args = vars(args)
    func(args["entities"])

if __name__ == '__main__':
    main()
@@ -307,337 +307,8 @@
 n():
-%0A    loghandler = logging.StreamHandler()%0A    formatter = logging.Formatter(fmt=%22%25(processName)s %25(asctime)s  %25(levelname)s: %25(message)s%22)%0A    loghandler.setFormatter(formatter)%0A    logger.addHandler(loghandler)%0A%0A    mplogger = multiprocessing.get_logger()%0A    mplogger.setLevel(logging.ERROR)%0A    mplogger.addHandler(loghandler)
 %0A%0A  
@@ -873,16 +873,24 @@
 %22watch%22,
+%0A       
  help=%22W
@@ -1118,16 +1118,449 @@
 .INFO)%0A%0A
+    loghandler = logging.StreamHandler()%0A    if args.debug:%0A        formatter = logging.Formatter(fmt=%22%25(processName)s %25(asctime)s  %25(levelname)s: %25(message)s%22)%0A    else:%0A        formatter = logging.Formatter(fmt=%22%25(asctime)s: %25(message)s%22)%0A    loghandler.setFormatter(formatter)%0A    logger.addHandler(loghandler)%0A%0A    mplogger = multiprocessing.get_logger()%0A    mplogger.setLevel(logging.ERROR)%0A    mplogger.addHandler(loghandler)%0A%0A
     if a
@@ -2605,20 +2605,8 @@
 args
-%5B%22entities%22%5D
 )%0A%0Ai
@@ -2631,16 +2631,16 @@
 ain__':%0A
-
     main
@@ -2637,12 +2637,13 @@
 :%0A    main()
+%0A

Use a more readable logging format if debug is not set