diff --git a/src/main.py b/src/main.py index aa29a4ab6..253e77f7f 100755 --- a/src/main.py +++ b/src/main.py @@ -68,17 +68,26 @@ def start_logging(file_name): # 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 + + # instantiate the buskill object EARLY + # Moved to top so that DATA_DIR path can be found from the buskill class + global bk + bk = packages.buskill.BusKill() + + # Decision whether to write to DATA_DIR or TEMP file + log_dir = bk.DATA_DIR if bk.PERSISTENT_LOG else tempfile.gettempdir() try: - log_file_path = os.path.join( tempfile.gettempdir() , 'buskill.log' ) + log_file_path = os.path.join( log_dir , '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') - + log_file_path = os.path.join( log_dir , f'buskill.{timestamp}.log') start_logging(log_file_path) + + bk.LOG_FILE_PATH = log_file_path msg = "===============================================================================" print( msg ); logging.info( msg ) @@ -160,10 +169,6 @@ def start_logging(file_name): msg = "buskill version " +str(BUSKILL_VERSION) print( msg ); logging.info( msg ) - # instantiate the buskill object - global bk - bk = packages.buskill.BusKill() - ############# # LAUNCH UI # ############# diff --git a/src/packages/buskill/__init__.py b/src/packages/buskill/__init__.py index a39aaafe4..830472b21 100644 --- a/src/packages/buskill/__init__.py +++ b/src/packages/buskill/__init__.py @@ -224,7 +224,13 @@ def __init__(self): self.SIMULATE_HOTPLUG_REMOVAL = False self.EXECUTED_AS_SCRIPT = None - self.LOG_FILE_PATH = logger.root.handlers[0].baseFilename + + # BusKill class was always initialised after finding the log file path Therefore in the new setup we are initialising it before the log file path is found + # This will prevent it from out of index error + self.LOG_FILE_PATH = logger.root.handlers[0].baseFilename if logger.root.handlers else None + # Default value as False, because if no one updates the config.ini file then reverting back to old file path + self.PERSISTENT_LOG = False + self.EXE_PATH = None self.EXE_DIR = None self.EXE_FILE = None @@ -429,6 +435,8 @@ def __init__(self): msg = "DEBUG: CONF_FILE:|" +str(self.CONF_FILE)+ "|\n" print( msg ); logger.debug( msg ) + # running the config option only after the implimentation of the config file + self.getConfigOption() # handle conditions where this version was already upgraded by a newer # version or if this is a version that upgraded an older version @@ -441,6 +449,20 @@ def __init__(self): # * https://bugs.python.org/issue34034 # * https://docs.python.org/3/library/pickle.html#object.__reduce__ # * https://docs.python.org/3/library/pickle.html#object.__getstate__ + + + # Old config file reading option only exist after the toggling between the arm and disarm functionality + # Created a new function to get config option for persistent log file + def getConfigOption(self): + self.config = configparser.ConfigParser() + self.config.read( self.CONF_FILE ) + + if self.config.has_option('buskill', 'persistent_log'): + self.PERSISTENT_LOG = self.config.getboolean( + 'buskill', + 'persistent_log' + ) + def __getstate__(self): state = self.__dict__.copy()