예제 #1
0
파일: logger.py 프로젝트: yqyunjie/chipsec
 def __init__( self ):
     """The Constructor."""
     pass
     self.mytime = localtime()
     self.logfile = None
     self.debug = pyLogging.DEBUG
     self.info = pyLogging.INFO
     self.rootLogger = pyLogging.getLogger(__name__)
     self.rootLogger.setLevel(self.debug)
     self.ALWAYS_FLUSH = False
     self.verbose = pyLogging.addLevelName(15,"verbose")
     self.logstream = pyLogging.StreamHandler(sys.stdout)
     self.logstream.setFormatter(ColorLogger()) #applys colorization to output
     self.rootLogger.addHandler(self.logstream) #adds streamhandler to root logger
     self.Results = ChipsecResults()
예제 #2
0
 def __init__( self ):
     """The Constructor."""
     self.mytime = localtime()
     self.logfile = None
     self.debug = pyLogging.DEBUG
     self.info = pyLogging.INFO
     self.rootLogger = pyLogging.getLogger(__name__)
     self.rootLogger.setLevel(self.debug)
     self.ALWAYS_FLUSH = False
     pyLogging.addLevelName(15, "verbose")
     self.verbose = 15
     self.logstream = pyLogging.StreamHandler(sys.stdout)
     # Respect https://no-color.org/ convention, and disable colorization
     # when the output is not a terminal (eg. redirection to a file)
     if sys.stdout.isatty() and os.getenv('NO_COLOR') is None:
         self.logstream.setFormatter(ColorLogger())
     self.rootLogger.addHandler(self.logstream) #adds streamhandler to root logger
     self.Results = ChipsecResults()
예제 #3
0
파일: logger.py 프로젝트: tmaone/chipsec
class Logger:
    """Class for logging to console, text file, XML."""
    def __init__(self):
        """The Constructor."""
        pass
        self.mytime = localtime()
        self.logfile = None
        self.debug = pyLogging.DEBUG
        self.info = pyLogging.INFO
        self.rootLogger = pyLogging.getLogger(__name__)
        self.rootLogger.setLevel(self.debug)
        self.ALWAYS_FLUSH = False
        pyLogging.addLevelName(15, "verbose")
        self.verbose = 15
        self.logstream = pyLogging.StreamHandler(sys.stdout)
        self.logstream.setFormatter(
            ColorLogger())  #applys colorization to output
        self.rootLogger.addHandler(
            self.logstream)  #adds streamhandler to root logger
        self.Results = ChipsecResults()

    def set_log_file(self, name=None):
        """Sets the log file for the output."""
        # Close current log file if it's opened
        self.disable()
        self.LOG_FILE_NAME = name
        # specifying name=None effectively disables logging to file

        if self.LOG_FILE_NAME:
            # Open new log file and keep it opened
            try:
                self.logfile = pyLogging.FileHandler(
                    filename=self.LOG_FILE_NAME,
                    mode='w')  #creates FileHandler for log file
                self.rootLogger.addHandler(
                    self.logfile)  #adds filehandler to root logger

                self.LOG_TO_FILE = True
            except Exception:
                print("WARNING: Could not open log file '{}'".format(
                    self.LOG_FILE_NAME))
            self.rootLogger.removeHandler(self.logstream)
        else:
            try:
                self.rootLogger.addHandler(self.logstream)
            except:
                pass

    def close(self):
        """Closes the log file."""
        if self.logfile:
            try:
                self.rootLogger.removeHandler(self.logfile)
                self.rootLogger.removeHandler(self.logstream)
                self.logfile.close()
                self.logstream.flush()
            except Exception:
                print("WARNING: Could not close log file")
            finally:
                self.logfile = None

    def disable(self):
        """Disables the logging to file and closes the file if any."""
        self.LOG_TO_FILE = False
        self.LOG_FILE_NAME = None
        self.close()

    ######################################################################
    # Logging functions
    ######################################################################

    def flush(self):
        sys.stdout.flush()
        if self.LOG_TO_FILE and self.logfile is not None:
            # flush should work with new python logging
            try:
                self.rootLogger.removeHandler(self.logfile)
                self.logfile.flush()
                self.rootLogger.addHandler(self.logfile)
            except Exception:
                self.disable()

    def set_always_flush(self, val):
        self.ALWAYS_FLUSH = val

    def log(self, text, level=pyLogging.INFO):
        """Sends plain text to logging."""
        if self.Results.get_current() is not None:
            self.Results.get_current().add_output(text)
        if self.LOG_TO_FILE: self._save_to_log_file(text)
        else:
            if self.rootLogger:
                self.rootLogger.log(level, text)
                if self.ALWAYS_FLUSH: sys.stdout.flush()
            else:
                print(text)

    def error(self, text):
        """Logs an Error message"""
        text = "ERROR: " + text
        self.log(text, pyLogging.ERROR)

    def warn(self, text):
        """Logs an Warning message"""
        text = "WARNING: " + text
        self.log(text, pyLogging.WARNING)

    def verbose_log(self, text):
        """Logs an Verbose message"""
        if self.VERBOSE:
            self.log(text, self.verbose)

    def log_passed_check(self, text):
        """Logs a Test as PASSED"""
        self.log_passed(text)

    def log_failed_check(self, text):
        """Logs a Test as FAILED"""
        self.log_failed(text)

    def log_error_check(self, text):
        """Logs a Test as ERROR"""
        self.error(text)

    def log_skipped_check(self, text):
        """Logs a Test as Not Implemented"""
        self.log_skipped(text)

    def log_warn_check(self, text):
        """Logs a Warning test, a warning test is considered equal to a PASSED test"""
        self.log_warning(text)

    def log_information_check(self, text):
        """Logs a Information test, an information test"""
        self.log_information(text)

    def log_not_applicable_check(self, text):
        """Logs a Test as Not Applicable"""
        self.log_not_applicable(text)

    def log_passed(self, text):
        """Logs a passed message."""
        text = "[+] PASSED: " + text
        self.log(text, pyLogging.DEBUG)

    def log_failed(self, text):
        """Logs a failed message."""
        text = "[-] FAILED: " + text
        self.log(text, pyLogging.ERROR)

    def log_warning(self, text):
        """Logs a Warning message"""
        text = "[!] WARNING: " + text
        self.log(text, pyLogging.WARNING)

    def log_skipped(self, text):
        """Logs a NOT IMPLEMENTED message."""
        text = "[*] NOT IMPLEMENTED: " + text
        self.log(text, pyLogging.WARNING)

    def log_not_applicable(self, text):
        """Logs a NOT APPLICABLE message."""
        text = "[*] NOT APPLICABLE: " + text
        self.log(text, pyLogging.WARNING)

    def log_heading(self, text):
        """Logs a heading message."""
        self.log(text, pyLogging.CRITICAL)

    def log_important(self, text):
        """Logs a important message."""
        text = "[!] " + text
        self.log(text, pyLogging.ERROR)

    def log_result(self, text):
        """Logs a result message."""
        text = "[+] " + text
        self.log(text, pyLogging.DEBUG)

    def log_bad(self, text):
        """Logs a bad message, so it calls attention in the information displayed."""
        text = "[-] " + text
        self.log(text, pyLogging.ERROR)

    def log_good(self, text):
        """Logs a message, if colors available, displays in green."""
        text = "[+] " + text
        self.log(text, pyLogging.DEBUG)

    def log_unknown(self, text):
        """Logs a message with a question mark."""
        text = "[?] " + text
        self.log(text, pyLogging.INFO)

    def log_information(self, text):
        """Logs a message with information message"""
        text = "[#] INFORMATION: " + text
        self.log(text, pyLogging.DEBUG)

    def start_test(self, test_name):
        """Logs the start point of a Test"""
        text = "[x][ =======================================================================\n"
        text = text + "[x][ Module: " + test_name + "\n"
        text = text + "[x][ ======================================================================="
        self.log(text, pyLogging.CRITICAL)

    def start_module(self, module_name):
        """Displays a banner for the module name provided."""
        text = "\n[*] running module: {}".format(module_name)
        self.log(text, pyLogging.INFO)
        if self.Results.get_current() is not None:
            self.Results.get_current().add_desc(module_name)
            self.Results.get_current().set_time()

    def end_module(self, module_name):
        if self.Results.get_current() is not None:
            self.Results.get_current().set_time()
        #text = "\n[-] *** Done *** %s" % module_name
        #self._log(text, None, None)

    def _write_log(self, text, filename):
        self.rootLogger.log(self.info, text)  #writes text to defined log file
        if self.ALWAYS_FLUSH:
            # not sure why flush doesn't work as excpected
            # self.logfile.flush()
            # close and re-open log file
            try:
                self.logfile.close()
                self.logfile = open(self.LOG_FILE_NAME, 'a+')
            except Exception:
                self.disable()

    def _save_to_log_file(self, text):
        if (self.LOG_TO_FILE):
            self._write_log(text, self.LOG_FILE_NAME)

    VERBOSE = False
    UTIL_TRACE = False
    HAL = False
    DEBUG = False

    LOG_TO_STATUS_FILE = False
    LOG_STATUS_FILE_NAME = ""
    LOG_TO_FILE = False
    LOG_FILE_NAME = ""
예제 #4
0
파일: logger.py 프로젝트: chipsec/chipsec
class Logger:
    
    """Class for logging to console, text file, XML."""

    def __init__( self ):
        """The Constructor."""
        pass
        self.mytime = localtime()
        self.logfile = None
        self.debug = pyLogging.DEBUG
        self.info = pyLogging.INFO
        self.rootLogger = pyLogging.getLogger(__name__)
        self.rootLogger.setLevel(self.debug)
        self.ALWAYS_FLUSH = False
        self.verbose = pyLogging.addLevelName(15,"verbose")
        self.logstream = pyLogging.StreamHandler(sys.stdout)
        self.logstream.setFormatter(ColorLogger()) #applys colorization to output
        self.rootLogger.addHandler(self.logstream) #adds streamhandler to root logger
        self.Results = ChipsecResults()

    def set_log_file( self, name=None ):
        """Sets the log file for the output."""
        # Close current log file if it's opened
        self.disable()
        self.LOG_FILE_NAME = name
        # specifying name=None effectively disables logging to file

        if self.LOG_FILE_NAME:
            # Open new log file and keep it opened
            try:
                self.logfile = pyLogging.FileHandler(filename = self.LOG_FILE_NAME,mode='w') #creates FileHandler for log file
                self.rootLogger.addHandler(self.logfile) #adds filehandler to root logger
                
                self.LOG_TO_FILE = True
            except None:
                print("WARNING: Could not open log file '{}'".format(self.LOG_FILE_NAME))
            self.rootLogger.removeHandler(self.logstream)
        else:
            try:
                self.rootLogger.addHandler(self.logstream)
            except:
                pass

    def close( self ):
        """Closes the log file."""
        if self.logfile:
            try:
                self.rootLogger.removeHandler(self.logfile)
                self.rootLogger.removeHandler(self.logstream)
                self.logfile.close()
                self.logstream.flush()             
            except None:
                print ("WARNING: Could not close log file")
            finally:
                self.logfile = None
        
    def disable( self ):
        """Disables the logging to file and closes the file if any."""
        self.LOG_TO_FILE = False
        self.LOG_FILE_NAME = None
        self.close()

    ######################################################################
    # Logging functions
    ######################################################################

    def flush(self):
        sys.stdout.flush()
        if self.LOG_TO_FILE and self.logfile is not None:
            # flush should work with new python logging
            try:
                self.rootLogger.removeHandler(self.logfile)
                self.logfile.flush()
            except None:
                self.disable()

    def set_always_flush( self, val ):
        self.ALWAYS_FLUSH = val

    def log( self, text):
        """Sends plain text to logging."""
        if self.Results.get_current() is not None:
            self.Results.get_current().add_output(text)
        if self.LOG_TO_FILE: self._save_to_log_file( text )
        else:
            if self.rootLogger:
                self.rootLogger.info(text)
                if self.ALWAYS_FLUSH: sys.stdout.flush()
            else:
                print(text)
    
    def error( self, text ):
        """Logs an Error message"""
        text = "ERROR: " + text
        self.rootLogger.error(text)

    def warn( self, text ):
        """Logs an Warning message"""
        text = "WARNING: " + text
        self.rootLogger.warning(text)
    
    def verbose_log( self, text):
        """Logs an Verbose message"""
        if self.VERBOSE:
            self.rootLogger.log(self.verbose, text )

    def log_passed_check( self, text ):
        """Logs a Test as PASSED"""
        self.log_passed(text)

    def log_failed_check( self, text ):
        """Logs a Test as FAILED"""
        self.log_failed(text)

    def log_error_check( self, text ):
        """Logs a Test as ERROR"""
        self.error(text)

    def log_skipped_check( self, text ):
        """Logs a Test as Not Implemented"""
        self.log_skipped(text)

    def log_warn_check( self, text ):
        """Logs a Warning test, a warning test is considered equal to a PASSED test"""
        self.log_warning(text)

    def log_information_check( self, text ):
        """Logs a Information test, an information test"""
        self.log_information(text)

    def log_not_applicable_check( self, text):
        """Logs a Test as Not Applicable"""
        self.log_not_applicable(text)

    def log_passed( self, text ):
        """Logs a passed message."""
        text = "[+] PASSED: " + text
        self.rootLogger.debug(text)

    def log_failed( self, text ):
        """Logs a failed message."""
        text = "[-] FAILED: " + text
        self.rootLogger.error(text)

    def log_warning( self, text ):
        """Logs a Warning message"""
        text = "[!] WARNING: " + text
        self.rootLogger.warning(text)

    def log_skipped( self, text ):
        """Logs a NOT IMPLEMENTED message."""
        text = "[*] NOT IMPLEMENTED: " + text
        self.rootLogger.warning(text)

    def log_not_applicable(self, text):
        """Logs a NOT APPLICABLE message."""
        text = "[*] NOT APPLICABLE: " + text
        self.rootLogger.warning(text)

    def log_heading( self, text ):
        """Logs a heading message."""
        self.rootLogger.critical(text)

    def log_important( self, text ):
        """Logs a important message."""
        text = "[!] " + text
        self.rootLogger.error(text)

    def log_result( self, text ):
        """Logs a result message."""
        text = "[+] " + text
        self.rootLogger.debug(text)

    def log_bad( self, text ):
        """Logs a bad message, so it calls attention in the information displayed."""
        text = "[-] " + text
        self.rootLogger.error(text)

    def log_good( self, text ):
        """Logs a message, if colors available, displays in green."""
        text = "[+] " + text
        self.rootLogger.debug(text)

    def log_unknown( self, text ):
        """Logs a message with a question mark."""
        text = "[?] " + text
        self.rootLogger.info(text)

    def log_information( self, text):
        """Logs a message with information message"""
        text = "[#] INFORMATION: " + text
        self.rootLogger.debug(text)

    def start_test( self, test_name ):
        """Logs the start point of a Test"""
        text =        "[x][ =======================================================================\n"
        text = text + "[x][ Module: " + test_name + "\n"
        text = text + "[x][ ======================================================================="
        self.rootLogger.critical(text)

    def start_module( self, module_name ):
        """Displays a banner for the module name provided."""
        text = "\n[*] running module: {}".format(module_name)
        self.rootLogger.info(text)
        if self.Results.get_current() is not None:
            self.Results.get_current().add_desc(module_name)
            self.Results.get_current().set_time()


    def end_module( self, module_name ):
        if self.Results.get_current() is not None:
            self.Results.get_current().set_time()
        #text = "\n[-] *** Done *** %s" % module_name
        #self._log(text, None, None)

    def _write_log( self, text, filename ):
        self.rootLogger.log(self.info,text) #writes text to defined log file
        if self.ALWAYS_FLUSH:
            # not sure why flush doesn't work as excpected
            # self.logfile.flush()
            # close and re-open log file
            try:
                self.logfile.close()
                self.logfile = open( self.LOG_FILE_NAME, 'a+' )
            except None:
                self.disable()

    def _save_to_log_file(self, text):
        if(self.LOG_TO_FILE):
            self._write_log(text, self.LOG_FILE_NAME)

    VERBOSE    = False
    UTIL_TRACE = False
    HAL        = False
    DEBUG      = False
    

    LOG_TO_STATUS_FILE   = False
    LOG_STATUS_FILE_NAME = ""
    LOG_TO_FILE          = False
    LOG_FILE_NAME        = ""