From a7bc66368525e69f220f4fcdcf5074f7b17c4f12 Mon Sep 17 00:00:00 2001 From: Raihan93-coder Date: Mon, 25 May 2026 22:32:34 +0530 Subject: [PATCH 1/6] Fix user-specific log file initialization --- src/main.py | 6 +++++- src/packages/buskill/__init__.py | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index 6d0cf3dab..3f8868919 100755 --- a/src/main.py +++ b/src/main.py @@ -54,7 +54,11 @@ # 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' ) + + # Removing the universal log file logic that generates the file in /tmp dir + # Changing the location of the log file to the user specific cache thus preventing the file permission error + buskill = packages.buskill.BusKill() + log_file_path = os.path.join( buskill.CACHE_DIR, 'buskill.log' ) logging.basicConfig( filename = log_file_path, diff --git a/src/packages/buskill/__init__.py b/src/packages/buskill/__init__.py index 4306890de..5b8d75a78 100644 --- a/src/packages/buskill/__init__.py +++ b/src/packages/buskill/__init__.py @@ -224,7 +224,9 @@ def __init__(self): self.SIMULATE_HOTPLUG_REMOVAL = False self.EXECUTED_AS_SCRIPT = None - self.LOG_FILE_PATH = logger.root.handlers[0].baseFilename + # Adding a fallback logic as there was an issue of out of bound indexing when the program didn't yet have a user specific writtable cache + self.LOG_FILE_PATH = logger.root.handlers[0].baseFilename if logger.root.handler else None + self.EXE_PATH = None self.EXE_DIR = None self.EXE_FILE = None @@ -411,6 +413,17 @@ def __init__(self): # create a data dir in some safe place where we have write access self.setupDataDir() + # Safety feature: initilaing the writable log file only after the above function is executed + # ie; after finding some safe place where there will to write access + # In this case it is user cache + if self.LOG_FILE_PATH is None: + self.LOG_FILE_PATH = os.path.join( self.CACHE_DIR , 'buskill.log' ) + logging.basicConfig( + filename=self.LOG_FILE_PATH, + level=logging.DEBUG, + format='%(ascitime)s %(levelname)s %(message)s' + ) + # path to buskill's config file self.CONF_FILE = os.path.join( self.DATA_DIR, "config.ini" ) From 33dd42f569b5650a67dd0c9a9d5482595bd0d465 Mon Sep 17 00:00:00 2001 From: Raihan93-coder Date: Tue, 26 May 2026 07:11:23 +0530 Subject: [PATCH 2/6] Handle logfile permission conflicts with timestamp fallback --- src/main.py | 36 +++++++++++++++++++++----------- src/packages/buskill/__init__.py | 16 +------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/main.py b/src/main.py index 3f8868919..fd48b94f1 100755 --- a/src/main.py +++ b/src/main.py @@ -55,18 +55,30 @@ # 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 - # Removing the universal log file logic that generates the file in /tmp dir - # Changing the location of the log file to the user specific cache thus preventing the file permission error - buskill = packages.buskill.BusKill() - log_file_path = os.path.join( buskill.CACHE_DIR, '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 - ) + # + log_file_path = os.path.join( tempfile.gettempdir() , 'buskill.log' ) + + try: + 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 + ) + except PermissionError: + from datetime import datetime + timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') + fallback_log = os.path.join( tempfile.gettempdir() , f'buskill.{timestamp}.log') + + logging.basicConfig( + filename = fallback_log, + filemode='a', + format = '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', + datefmt = '%H:%M:%S', + level = logging.DEBUG + ) + 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 5b8d75a78..a39aaafe4 100644 --- a/src/packages/buskill/__init__.py +++ b/src/packages/buskill/__init__.py @@ -224,9 +224,7 @@ def __init__(self): self.SIMULATE_HOTPLUG_REMOVAL = False self.EXECUTED_AS_SCRIPT = None - # Adding a fallback logic as there was an issue of out of bound indexing when the program didn't yet have a user specific writtable cache - self.LOG_FILE_PATH = logger.root.handlers[0].baseFilename if logger.root.handler else None - + self.LOG_FILE_PATH = logger.root.handlers[0].baseFilename self.EXE_PATH = None self.EXE_DIR = None self.EXE_FILE = None @@ -412,18 +410,6 @@ def __init__(self): # create a data dir in some safe place where we have write access self.setupDataDir() - - # Safety feature: initilaing the writable log file only after the above function is executed - # ie; after finding some safe place where there will to write access - # In this case it is user cache - if self.LOG_FILE_PATH is None: - self.LOG_FILE_PATH = os.path.join( self.CACHE_DIR , 'buskill.log' ) - logging.basicConfig( - filename=self.LOG_FILE_PATH, - level=logging.DEBUG, - format='%(ascitime)s %(levelname)s %(message)s' - ) - # path to buskill's config file self.CONF_FILE = os.path.join( self.DATA_DIR, "config.ini" ) From 64b154152d4d57309a3e88add3b3f8c31bdf0ab2 Mon Sep 17 00:00:00 2001 From: Raihan93-coder Date: Tue, 26 May 2026 07:22:23 +0530 Subject: [PATCH 3/6] Added comments to the new timestamp based log fallback logic --- src/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index fd48b94f1..07268de76 100755 --- a/src/main.py +++ b/src/main.py @@ -55,7 +55,8 @@ # 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 - # + # adding a fallback log to avoid PermissionError + # This creates a new file that appends the timestamp to the log file log_file_path = os.path.join( tempfile.gettempdir() , 'buskill.log' ) try: From 397bbd8f9533bf157c283faf26c31467380d388a Mon Sep 17 00:00:00 2001 From: Raihan93-coder Date: Wed, 27 May 2026 06:13:33 +0530 Subject: [PATCH 4/6] Move the datetime import below the imports banner --- src/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 07268de76..491389b7a 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 ################################################################################ @@ -68,7 +69,6 @@ level = logging.DEBUG ) except PermissionError: - from datetime import datetime timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') fallback_log = os.path.join( tempfile.gettempdir() , f'buskill.{timestamp}.log') From 21a3410bc8eb4b0ada122ff69f4dc7e0db03140c Mon Sep 17 00:00:00 2001 From: Raihan93-coder Date: Wed, 27 May 2026 06:59:09 +0530 Subject: [PATCH 5/6] Created a function for logging --- src/main.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main.py b/src/main.py index 491389b7a..6436cb030 100755 --- a/src/main.py +++ b/src/main.py @@ -43,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 # ################################################################################ @@ -61,24 +74,12 @@ log_file_path = os.path.join( tempfile.gettempdir() , 'buskill.log' ) try: - 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 - ) + start_logging(log_file_path) except PermissionError: timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') - fallback_log = os.path.join( tempfile.gettempdir() , f'buskill.{timestamp}.log') - - logging.basicConfig( - filename = fallback_log, - filemode='a', - format = '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', - datefmt = '%H:%M:%S', - level = logging.DEBUG - ) + log_file_path = os.path.join( tempfile.gettempdir() , f'buskill.{timestamp}.log') + + start_logging(log_file_path) msg = "===============================================================================" print( msg ); logging.info( msg ) From 5cf3978617c84c955d70e413279dfe248cbd1e02 Mon Sep 17 00:00:00 2001 From: Raihan93-coder Date: Wed, 27 May 2026 21:52:04 +0530 Subject: [PATCH 6/6] Adjusted the comments --- src/main.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index 6436cb030..c8dfa6858 100755 --- a/src/main.py +++ b/src/main.py @@ -69,13 +69,12 @@ 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 - # adding a fallback log to avoid PermissionError - # This creates a new file that appends the timestamp to the log file - log_file_path = os.path.join( tempfile.gettempdir() , 'buskill.log' ) - 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')