Changes for improved logging.

This commit is contained in:
Eratosthenes 2009-11-23 19:29:56 -05:00
parent e10db69a9c
commit 2da6e44a75

View File

@ -58,29 +58,29 @@ def get_exec_path():
return os.path.dirname(sys.path[0]) return os.path.dirname(sys.path[0])
def get_config(file_name, fallback = True): def get_config(file_name, fallback = True):
"""Looks in cwd and in self.default_config_path for a config file.""" """Looks in exec dir and in self.default_config_path for a config file."""
config_path = os.path.join(get_exec_path(), file_name) config_path = os.path.join(DIR_SELF, file_name) # look in exec dir
if os.path.exists(config_path): # there is a file in the cwd if os.path.exists(config_path) and os.path.isfile(config_path):
return config_path # so we use it return config_path # there is a file in the exec dir so we use it
else: # no file in the cwd, look where it should be in the first place else:
config_path = os.path.join(get_default_config_path(), file_name) config_path = os.path.join(DIR_CONFIG, file_name) # look in config dir
if os.path.exists(config_path): if os.path.exists(config_path) and os.path.isfile(config_path):
return config_path return config_path
# No file found # No file found
if not fallback: if not fallback:
return False return False
# OK, fall back to the .example file, should be in the start dir # OK, fall back to the .example file, should be in the exec dir
if os.path.exists(file_name + ".example"): if os.path.exists(os.path.join(DIR_SELF, file_name + ".example")):
try: try:
shutil.copyfile(file_name + ".example", file_name) shutil.copyfile(os.path.join(DIR_SELF, file_name + ".example"), os.path.join(DIR_CONFIG, file_name))
print "No %s found, using %s.example.\n" % (file_name, file_name) print "No %s found, using %s.example.\n" % (file_name, file_name)
print "A %s file has been created. You will probably have to edit it." % file_name print "A %s file has been created. You will probably have to edit it." % os.path.join(DIR_CONFIG, file_name)
sys.stderr.write("No %s found, using %s.example.\n" % (file_name, file_name) ) log.error("No %s found, using %s.example.\n" % (file_name, file_name) )
except: except:
print "No %s found, cannot fall back. Exiting.\n" % file_name print "No %s found, cannot fall back. Exiting.\n" % file_name
sys.stderr.write("No %s found, cannot fall back. Exiting.\n" % file_name) log.critical("No %s found, cannot fall back. Exiting.\n" % file_name)
sys.exit() sys.exit()
return file_name return file_name
@ -90,18 +90,26 @@ def get_logger(file_name, config = "config", fallback = False):
try: try:
logging.config.fileConfig(conf) logging.config.fileConfig(conf)
log = logging.getLogger(config) log = logging.getLogger(config)
log.debug("%s logger initialised" % config)
return log return log
except: except:
pass pass
log = logging.basicConfig() log = logging.basicConfig()
log = logging.getLogger() log = logging.getLogger()
log.debug("config logger initialised") log.error("basicConfig logger initialised")
return log return log
# find a logging.conf file and set up logging def check_dir(path, create = True):
log = get_logger("logging.conf") """Check if a dir exists, optionally creates if not."""
if os.path.exists(path):
if os.path.isdir(path):
return path
else:
return False
if create:
print "creating directory %s" % path
else:
return False
######################################################################## ########################################################################
# application wide consts # application wide consts
@ -109,19 +117,31 @@ log = get_logger("logging.conf")
APPLICATION_NAME_SHORT = 'fpdb' APPLICATION_NAME_SHORT = 'fpdb'
APPLICATION_VERSION = 'xx.xx.xx' APPLICATION_VERSION = 'xx.xx.xx'
DIR_SELF = os.path.dirname(get_exec_path()) DIR_SELF = get_exec_path()
#TODO: imo no good idea to place 'database' in parent dir DIR_CONFIG = check_dir(get_default_config_path())
DIR_DATABASES = os.path.join(os.path.dirname(DIR_SELF), 'database') DIR_DATABASE = check_dir(os.path.join(DIR_CONFIG, 'database'))
DIR_LOG = check_dir(os.path.join(DIR_CONFIG, 'log'))
DATABASE_TYPE_POSTGRESQL = 'postgresql' DATABASE_TYPE_POSTGRESQL = 'postgresql'
DATABASE_TYPE_SQLITE = 'sqlite' DATABASE_TYPE_SQLITE = 'sqlite'
DATABASE_TYPE_MYSQL = 'mysql' DATABASE_TYPE_MYSQL = 'mysql'
#TODO: should this be a tuple or a dict
DATABASE_TYPES = ( DATABASE_TYPES = (
DATABASE_TYPE_POSTGRESQL, DATABASE_TYPE_POSTGRESQL,
DATABASE_TYPE_SQLITE, DATABASE_TYPE_SQLITE,
DATABASE_TYPE_MYSQL, DATABASE_TYPE_MYSQL,
) )
# find a logging.conf file and set up logging
log = get_logger("logging.conf", config = "config")
log.debug("config logger initialised")
# and then log our consts
log.info("DIR SELF = %s" % DIR_SELF)
log.info("DIR CONFIG = %s" % DIR_CONFIG)
log.info("DIR DATABASE = %s" % DIR_DATABASE)
log.info("DIR LOG = %s" % DIR_LOG)
######################################################################## ########################################################################
def string_to_bool(string, default=True): def string_to_bool(string, default=True):
"""converts a string representation of a boolean value to boolean True or False """converts a string representation of a boolean value to boolean True or False
@ -398,8 +418,7 @@ class Config:
if file is not None: # config file path passed in if file is not None: # config file path passed in
file = os.path.expanduser(file) file = os.path.expanduser(file)
if not os.path.exists(file): if not os.path.exists(file):
print "Configuration file %s not found. Using defaults." % (file) log.error("Specified configuration file %s not found. Using defaults." % (file))
sys.stderr.write("Configuration file %s not found. Using defaults." % (file))
file = None file = None
if file is None: file = get_config("HUD_config.xml") if file is None: file = get_config("HUD_config.xml")