コード例 #1
0
ファイル: test_qgslogger.py プロジェクト: AaronGaim/QGIS
 def testLogger(self):
     (myFileHandle, myFilename) = tempfile.mkstemp()
     try:
         myFile = os.fdopen(myFileHandle, "w")
         myFile.write("QGIS Logger Unit Test\n")
         myFile.close()
         os.environ['QGIS_DEBUG'] = '2'
         os.environ['QGIS_LOG_FILE'] = myFilename
         myLogger = QgsLogger()
         myLogger.debug('This is a debug')
         myLogger.warning('This is a warning')
         myLogger.critical('This is critical')
         #myLogger.fatal('Aaaargh...fatal');  #kills QGIS not testable
         myFile = open(myFilename, 'rt')
         myText = myFile.readlines()
         myFile.close()
         myExpectedText = ['QGIS Logger Unit Test\n',
                           'This is a debug\n',
                           'This is a warning\n',
                           'This is critical\n']
         myMessage = ('Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n' %
                            (myExpectedText, myText))
         self.assertEquals(myText, myExpectedText, myMessage)
     finally:
         pass
         os.remove(myFilename)
コード例 #2
0
 def testLogger(self):
     (myFileHandle, myFilename) = tempfile.mkstemp()
     try:
         myFile = os.fdopen(myFileHandle, "w")
         myFile.write("QGIS Logger Unit Test\n")
         myFile.close()
         os.environ['QGIS_DEBUG'] = '2'
         os.environ['QGIS_LOG_FILE'] = myFilename
         myLogger = QgsLogger()
         myLogger.debug('This is a debug')
         myLogger.warning('This is a warning')
         myLogger.critical('This is critical')
         #myLogger.fatal('Aaaargh...fatal');  #kills QGIS not testable
         myFile = open(myFilename, 'rt')
         myText = myFile.readlines()
         myFile.close()
         myExpectedText = [
             'QGIS Logger Unit Test\n', 'This is a debug\n',
             'This is a warning\n', 'This is critical\n'
         ]
         myMessage = ('Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n' %
                      (myExpectedText, myText))
         self.assertEquals(myText, myExpectedText, myMessage)
     finally:
         pass
         os.remove(myFilename)
コード例 #3
0
    def _log(self, message: str, level: LoggerLevel) -> None:
        """
        Print message to QGIS log, depending on release or debug mode.

        Print either to the QGIS error console, if in release mode,
        (https://qgis.org/pyqgis/master/core/QgsLogger.html#qgis.core.QgsLogger)
        or to the QGIS GUI message logger (Log Message Panel)
        (https://qgis.org/pyqgis/master/core/QgsMessageLog.html#qgis.core.QgsMessageLog).
        Always use the above thread-safe classess to print out messages.
        """
        message = str(message)
        # Get caller info
        callerframerecord = inspect.stack()[1]
        frame = callerframerecord[0]
        info = inspect.getframeinfo(frame)
        # prepare level message
        msg = 'file=%s, func=%s, line=%s: %s' % (
            info.filename,
            info.function,
            str(info.lineno),
            message)
        # Print to Log Message Panel
        # (https://qgis.org/pyqgis/master/core/QgsMessageLog.html#qgis.core.QgsMessageLog.logMessage)
        if self.debug_flag:
            if level == self.DEBUG:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Info)
            elif level == self.INFO:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Success)
            elif level == self.ERROR:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Critical)
            elif level == self.WARNING:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Warning)
            else:
                raise Exception('Invalid log level: %s' % level)
        # Print to QGIS Python console
        else:
            # Do not use QgsLogger.fatal(msg), as it will crash QGIS process
            if level == self.ERROR:
                QgsLogger.critical(msg)
            elif level == self.WARNING:
                QgsLogger.warning(msg)
            elif level in (self.DEBUG, self.INFO):
                QgsLogger.debug(
                    msg=message, file=info.filename,
                    function=info.function, line=info.lineno)
        # always print to QGIS log file
        # must configure the QGIS_LOG_FILE environment variable
        QgsLogger.logMessageToFile("Picterra plugin - " + msg)
コード例 #4
0
ファイル: test_qgslogger.py プロジェクト: liminlu0314/QGIS
 def testLogger(self):
     try:
         myFile = os.fdopen(myFileHandle, "w")
         myFile.write("QGIS Logger Unit Test\n")
         myFile.close()
         myLogger = QgsLogger()
         myLogger.debug("This is a debug")
         myLogger.warning("This is a warning")
         myLogger.critical("This is critical")
         # myLogger.fatal('Aaaargh...fatal');  #kills QGIS not testable
         myFile = open(myFilename, "rt")
         myText = myFile.readlines()
         myFile.close()
         myExpectedText = [
             "QGIS Logger Unit Test\n",
             "This is a debug\n",
             "This is a warning\n",
             "This is critical\n",
         ]
         myMessage = "Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n" % (myExpectedText, myText)
         self.assertEqual(myText, myExpectedText, myMessage)
     finally:
         pass
         os.remove(myFilename)