From 17eefcffbe1a51d6e771ee02ea00b61a9161c335 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 22 Sep 2014 21:44:26 -0500 Subject: [PATCH] Home Assistant no longer crashes if it cannot write its log file --- homeassistant/bootstrap.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 441a52c8d40..f6d7047c7e0 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -152,21 +152,32 @@ def from_config_file(config_path, hass=None, enable_logging=True): hass = homeassistant.HomeAssistant() # Set config dir to directory holding config file - hass.config_dir = os.path.dirname(config_path) + hass.config_dir = os.path.abspath(os.path.dirname(config_path)) if enable_logging: # Setup the logging for home assistant. logging.basicConfig(level=logging.INFO) - # Log errors to a file - err_handler = logging.FileHandler( - hass.get_config_path("home-assistant.log"), mode='w', delay=True) + # Log errors to a file if we have write access to file or config dir + err_log_path = hass.get_config_path("home-assistant.log") + err_path_exists = os.path.isfile(err_log_path) - err_handler.setLevel(logging.ERROR) - err_handler.setFormatter( - logging.Formatter('%(asctime)s %(name)s: %(message)s', - datefmt='%H:%M %d-%m-%y')) - logging.getLogger('').addHandler(err_handler) + if (err_path_exists and os.access(err_log_path, os.W_OK)) or \ + (not err_path_exists and os.access(hass.config_dir, os.W_OK)): + + err_handler = logging.FileHandler( + err_log_path, mode='w', delay=True) + + err_handler.setLevel(logging.ERROR) + err_handler.setFormatter( + logging.Formatter('%(asctime)s %(name)s: %(message)s', + datefmt='%H:%M %d-%m-%y')) + logging.getLogger('').addHandler(err_handler) + + else: + logging.getLogger(__name__).error( + "Unable to setup error log {} (access denied)".format( + err_log_path)) # Read config config = configparser.ConfigParser()