From f16ff06083edbeb3ced3dc765ea65830938e5bac Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Fri, 16 Jan 2009 00:35:07 +0000 Subject: [PATCH] Fix the -l, --logfile option --- deluge/log.py | 40 ++++++++++++++++++++++++++-------------- deluge/main.py | 50 ++++++++++++++++++++------------------------------ 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/deluge/log.py b/deluge/log.py index 2f415158c..be2244c02 100644 --- a/deluge/log.py +++ b/deluge/log.py @@ -27,12 +27,32 @@ import logging -# Setup the logger -logging.basicConfig( - level=logging.ERROR, - format="[%(levelname)-8s] %(asctime)s %(module)s:%(lineno)d %(message)s", - datefmt="%H:%M:%S" -) +levels = { + "info": logging.INFO, + "warning": logging.WARNING, + "error": logging.ERROR, + "none": logging.CRITICAL, + "debug": logging.DEBUG +} +def setupLogger(level="error", filename=None): + """ + Sets up the basic logger and if `:param:filename` is set, then it will log + to that file instead of stdout. + + :param level: str, the level to log + :param filename: str, the file to log to + """ + + if not level: + level = "error" + + logging.basicConfig( + level=levels[level], + format="[%(levelname)-8s] %(asctime)s %(module)s:%(lineno)d %(message)s", + datefmt="%H:%M:%S", + filename=filename, + filemode="w" + ) def setLoggerLevel(level): """ @@ -41,14 +61,6 @@ def setLoggerLevel(level): :param level: str, a string representing the desired level """ - levels = { - "info": logging.INFO, - "warning": logging.WARNING, - "error": logging.ERROR, - "none": logging.CRITICAL, - "debug": logging.DEBUG - } - if level not in levels: return diff --git a/deluge/main.py b/deluge/main.py index b087ffef4..7cb504374 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -33,6 +33,7 @@ import os.path import sys from optparse import OptionParser +import deluge.log import deluge.common import deluge.configmanager @@ -91,28 +92,24 @@ def start_ui(): # Always log to a file in Windows if deluge.common.windows_check() and not options.logfile: - options.logfile = "deluge.log" - - if options.logfile: if options.config: - logfile = os.path.join(options.config, options.logfile) + options.logfile = os.path.join(options.config, "deluge.log") else: config_dir = deluge.common.get_default_config_dir() - logfile = os.path.join(config_dir, options.logfile) - sys.stdout = open(logfile, "wb") - sys.stderr = sys.stdout - sys.stdin = None + options.logfile = os.path.join(config_dir, "deluge.log") - from deluge.log import LOG as log - # Set the log level if necessary - if options.loglevel: - import deluge.log - deluge.log.setLoggerLevel(options.loglevel) + # Setup the logger + deluge.log.setupLogger(level=options.loglevel, filename=options.logfile) + if options.logfile: + sys.stdout = None + sys.stderr = None + sys.stdin = None version = deluge.common.get_version() if deluge.common.get_revision() != "": version = version + "r" + deluge.common.get_revision() + from deluge.log import LOG as log log.info("Deluge ui %s", version) log.debug("options: %s", options) log.debug("args: %s", args) @@ -161,23 +158,14 @@ def start_daemon(): if not os.path.exists(deluge.common.get_default_config_dir()): os.makedirs(deluge.common.get_default_config_dir()) - # Opens a log file and redirects stdout to it + # Sets the options.logfile to point to the default location def open_logfile(): - path = None - if options.logfile: - path = options.logfile - else: + if not options.logfile: if options.config: - path = os.path.join(options.config, "deluged.log") + options.logfile = os.path.join(options.config, "deluged.log") else: config_dir = deluge.common.get_default_config_dir() - path = os.path.join(config_dir, "deluged.log") - - # Open a logfile - if path: - sys.stdout = open(path, "wb") - sys.stderr = sys.stdout - sys.stdin = None + options.logfile = os.path.join(config_dir, "deluged.log") # Writes out a pidfile if necessary def write_pidfile(): @@ -204,10 +192,12 @@ def start_daemon(): # Do not daemonize write_pidfile() - # Set the log level if necessary - if options.loglevel: - import deluge.log - deluge.log.setLoggerLevel(options.loglevel) + # Setup the logger + deluge.log.setupLogger(level=options.loglevel, filename=options.logfile) + if options.logfile: + sys.stdout = None + sys.stderr = None + sys.stdin = None from deluge.core.daemon import Daemon Daemon(options, args)