Skip to content

Commit

Permalink
added handling issues with log directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dvida committed Nov 8, 2023
1 parent 5b3306c commit 246acef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion RMS/Astrometry/ApplyRecalibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def applyRecalibrate(ftpdetectinfo_path, config, generate_plot=True):
config = cr.loadConfigFromDirectory(cml_args.config, dir_path)

# Initialize the logger
initLogging(config, 'recalibrate_')
initLogging(config, 'recalibrate_', safedir=dir_path)

# Get the logger handle
log = logging.getLogger("logger")
Expand Down
13 changes: 11 additions & 2 deletions RMS/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
from RMS.Misc import mkdirP


def initLogging(config, log_file_prefix=""):
def initLogging(config, log_file_prefix="", safedir=None):
""" Initializes the logger.
Arguments:
config: [Config] Config object.
Keyword arguments:
log_file_prefix: [str] String which will be prefixed to the log file. Empty string by default.
safedir: [str] Path to the directory where the log files will always be able to be written to. It will
be used if the default log directory is not writable. None by default.
"""

# Path to the directory with log files
Expand All @@ -40,6 +44,11 @@ def initLogging(config, log_file_prefix=""):
mkdirP(config.data_dir)
mkdirP(log_path)

# If the log directory doesn't exist or is not writable, use the safe directory
if safedir is not None:
if not os.path.exists(log_path) or not os.access(log_path, os.W_OK):
log_path = safedir

# Generate a file name for the log file
log_file_name = log_file_prefix + "log_" + str(config.stationID) + "_" + datetime.datetime.utcnow().strftime('%Y%m%d_%H%M%S.%f') + ".log"

Expand Down

0 comments on commit 246acef

Please sign in to comment.