Fix the -l, --logfile option

This commit is contained in:
Andrew Resch 2009-01-16 00:35:07 +00:00
parent f990fd1a9a
commit f16ff06083
2 changed files with 46 additions and 44 deletions

View File

@ -27,12 +27,32 @@
import logging
# Setup the logger
logging.basicConfig(
level=logging.ERROR,
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"
)
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

View File

@ -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)