diff --git a/src/main.py b/src/main.py index 6d0cf3dab..c8dfa6858 100755 --- a/src/main.py +++ b/src/main.py @@ -33,6 +33,7 @@ ################################################################################ import argparse, logging, sys, multiprocessing, tempfile +from datetime import datetime import packages.buskill ################################################################################ @@ -42,6 +43,19 @@ #BUSKILL_VERSION = '0.1' from buskill_version import BUSKILL_VERSION +################################################################################ +# FUNCTIONS # +################################################################################ + +def start_logging(file_name): + logging.basicConfig( + filename = file_name, + filemode = 'a', + format = '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', + datefmt = '%H:%M:%S', + level = logging.DEBUG + ) + ################################################################################ # MAIN BODY # ################################################################################ @@ -54,15 +68,18 @@ # TODO: disable logging by default; enable it with an argument # TODO: be able to override the path to the log file with an env var or argument value; make these just the defaults - log_file_path = os.path.join( tempfile.gettempdir(), 'buskill.log' ) - logging.basicConfig( - filename = log_file_path, - filemode = 'a', - format = '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', - datefmt = '%H:%M:%S', - level = logging.DEBUG - ) + try: + log_file_path = os.path.join( tempfile.gettempdir() , 'buskill.log' ) + start_logging(log_file_path) + except PermissionError: + # adding a fallback log to avoid PermissionError + # This creates a new file that appends the timestamp to the log file + timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') + log_file_path = os.path.join( tempfile.gettempdir() , f'buskill.{timestamp}.log') + + start_logging(log_file_path) + msg = "===============================================================================" print( msg ); logging.info( msg ) msg = "INFO: Writing to log file '" +str(log_file_path)+ "'" diff --git a/src/packages/buskill/__init__.py b/src/packages/buskill/__init__.py index 4306890de..a39aaafe4 100644 --- a/src/packages/buskill/__init__.py +++ b/src/packages/buskill/__init__.py @@ -410,7 +410,6 @@ def __init__(self): # create a data dir in some safe place where we have write access self.setupDataDir() - # path to buskill's config file self.CONF_FILE = os.path.join( self.DATA_DIR, "config.ini" )