Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 #
#############
Expand Down
24 changes: 23 additions & 1 deletion src/packages/buskill/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
Loading