# This is a top-level module file that contains functions to handle # error processing with the MPE workflow. # Logan Karsten # National Center for Atmospheric Research # Research Applications Laboratory # karsten@ucar.edu # 303-497-2693 import sys import os import logging def errOutScreen(statusMeta): """ Generic function to error out gracefully by simply printing the error message to the screen. This should only be called early in the program workflow before the DB has been initialized to storing error messages. """ print(statusMeta.errMsg) sys.exit(1) def initLog(statusMeta): """ Function to create a log handle for a specific log file. """ try: l = logging.getLogger('logMPE') except: statusMeta.errMsg = "ERROR: Unablet to create logging object." raise Exception() try: formatter = logging.Formatter('[%(asctime)s]: %(levelname)s - %(message)s','%m/%d %H:%M:%S') except: statusMeta.errMsg = "ERROR: Unable to establish formatting for logger." raise Exception() try: statusMeta.logHandle = logging.FileHandler(statusMeta.logFile,mode='a') except: statusMeta.errMsg = "ERROR: Unable to create log handle for: " + \ statusMeta.logFile raise Exception() try: statusMeta.logHandle.setFormatter(formatter) except: statusMeta.errMsg = "ERROR: Unable to set formatting for logger." raise Exception() #try: # streamHandler = logging.StreamHandler() #except: # statusMeta.errMsg = "ERROR: Unable to establish stream handler for logger." # raise Exception() #try: # streamHandler.setFormatter(formatter) #except: # statusMeta.errMsg = "ERROR: Unable to set formatting for stream handler." # raise Exception() try: l.addHandler(statusMeta.logHandle) except: statusMeta.errMsg = "ERROR: Unable to add log handler for: " + statusMeta.logFile raise Exception() #try: # l.addHandler(streamHandler) #except: # statusMeta.errMsg = "ERROR: Unable to add stream handler for: " + statusMeta.logFile # raise Exception() def errOut(statusMeta): """ Generic function to error out after an error message has been logged into the sqllite file. We will simply remove the LOCK file and exit with a non-zero exit status. """ # If LOCK file exists, remove it. if os.path.isfile(statusMeta.lockPath): os.remove(statusMeta.lockPath) try: l = logging.getLogger('logMPE') except: statusMeta.errMsg = "ERROR: Unable to obtain a logger object." raise Exception() try: l.setLevel(logging.ERROR) except: statusMeta.errMsg = "ERROR: Unable to set ERROR logger level." raise Exception() try: l.error(statusMeta.errMsg) except: statusMeta.errMsg = "ERROR: Unable to write error message to: " + \ statusMeta.logFile raise Exception() sys.exit(1) def logErr(statusMeta): """ Function to log an error message without quitting the program. """ try: l = logging.getLogger('logMPE') except: statusMeta.errMsg = "ERROR: Unable to obtain a logger object." raise Exception() try: l.setLevel(logging.CRITICAL) except: statusMeta.errMsg = "ERROR: Unable to set CRITICAL logger level." raise Exception() try: l.critical(statusMeta.errMsg) except: statusMeta.errMsg = "ERROR: Unable to write error message to: " + \ statusMeta.logFile raise Exception() def logWarning(statusMeta): """ Function to log a warning message without quitting the program. """ try: l = logging.getLogger('logMPE') except: statusMeta.errMsg = "ERROR: Unable to obtain a logger object." raise Exception() try: l.setLevel(logging.WARNING) except: statusMeta.errMsg = "ERROR: Unable to set WARNING logger level." raise Exception() try: l.warning(statusMeta.statusMsg) except: statusMeta.errMsg = "ERROR: Unable to write warning message to: " + \ statusMeta.logFile raise Exception() def logMsg(statusMeta): """ Function to log status messages to the log file for this given accumulation period. """ try: l = logging.getLogger('logMPE') except: statusMeta.errMsg = "ERROR: Unable to obtain a logger object." raise Exception() try: l.setLevel(logging.INFO) except: statusMeta.errMsg = "ERROR: Unable to set INFO logger level." raise Exception() try: l.info(statusMeta.statusMsg) except: statusMeta.errMsg = "ERROR: Unable to write status message to: " + \ statusMeta.logFile raise Exception() def closeLog(statusMeta): """ Function to close log file. """ try: l = logging.getLogger('logMPE') except: statusMeta.errMsg = "ERROR: Unable to obtain a logger object." raise Exception() try: l.removeHandler(statusMeta.logHandle) except: statusMeta.errMsg = "ERROR: Unable to remove logging file handle " + \ " for file: " + statusMeta.logFile raise Exception() try: statusMeta.logHandle.close() except: statusMeta.errMsg = "ERROR: Unable to close the logging file " + \ " handle for: " + statusMeta.logFile try: statusMeta.logHandle = None except: statusMeta.errMsg = "ERROR: Unable to reset handle variable to " + \ " None type after closing: " + statusMeta.logFile raise Exception()