import ConfigParser
import logging
import os

from lala import config
from lala.factory import LalaFactory
from twisted.application import service, internet
from twisted.python import log
from twisted.python.usage import Options


CONFIG_DEFAULTS = {
        "channels": "",
        "plugins": "",
        "nickserv_password": None,
        "log_folder": os.path.expanduser("~/.lala/logs"),
        "log_file": os.path.expanduser("~/.lala/lala.log"),
        "encoding": "utf-8",
        "fallback_encoding": "utf-8",
        "max_log_days": 2,
}


class LalaOptions(Options):
    optFlags = [
        ["verbose", "v", "Log debugging information"]
    ]


def getService(options):
    observer = log.PythonLoggingObserver(loggerName="")
    observer.start()

    # Set up the config
    cfg = ConfigParser.SafeConfigParser(CONFIG_DEFAULTS)
    try:
        configfile = os.path.join(os.getenv("XDG_CONFIG_HOME"), "lala", "config")
    except AttributeError:
        configfile = os.path.join(os.getenv("HOME"), ".lala", "config")
    files = cfg.read([configfile, "/etc/lala.config"])

    config._CFG = cfg
    config._FILENAME = files[0]

    # Set up logging
    handler = logging.FileHandler(filename=config._get("base", "log_file"),
                                  encoding="utf-8")
    if options["verbose"]:
        handler.setFormatter(logging.Formatter(
            "%(asctime)s %(levelname)s %(filename)s: %(funcName)s:%(lineno)d %(message)s"))
    else:
        handler.setFormatter(logging.Formatter("%(message)s"))
    logging.getLogger("").addHandler(handler)
    logging.getLogger("").setLevel(logging.DEBUG)

    f = LalaFactory(cfg.get("base", "channels"),
            cfg.get("base", "nick"),
            cfg.get("base", "plugins").split(","))

    return internet.TCPClient(cfg.get("base", "server"),
            int(cfg.get("base", "port")),
            f)


def getApplication():
    application = service.Application("lala")

    _service = getService(LalaOptions())

    _service.setServiceParent(application)
    return application

@@ -1310,16 +1310,70 @@
 bose%22%5D:%0A
+        logging.getLogger(%22%22).setLevel(logging.DEBUG)%0A
         
@@ -1412,16 +1412,16 @@
 matter(%0A
-
         
@@ -1514,16 +1514,69 @@
   else:%0A
+        logging.getLogger(%22%22).setLevel(logging.INFO)%0A
         
@@ -1626,24 +1626,24 @@
 essage)s%22))%0A
+
     logging.
@@ -1679,58 +1679,8 @@
 ler)
-%0A    logging.getLogger(%22%22).setLevel(logging.DEBUG)
 %0A%0A  

set the log level correctly