Skip to content

Commit

Permalink
[processing] Move some log handling to c++ class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 26, 2017
1 parent 5169e0d commit 1394c28
Show file tree
Hide file tree
Showing 50 changed files with 357 additions and 299 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -2229,6 +2229,7 @@ object of type QgsProcessingFeedback, and will need to adapt their use of progre
- SilentProgress was removed. Use the base QgsProcessingFeedback class instead.
- algList was removed. Use QgsApplication.processingRegistry() instead.
- Processing.algs was removed. QgsApplication.processingRegistry().algorithms() instead.
- ProcessingLog should not be used when reporting log messages from algorithms. Use QgsProcessingUtils.logMessage() instead.

Triangulation {#qgis_api_break_3_0_Triangulation}
-------------
Expand Down
5 changes: 5 additions & 0 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -124,6 +124,11 @@ class QgsProcessingUtils
:rtype: list of QVariant
%End

static void logMessage( QgsMessageLog::MessageLevel level, const QString &message );
%Docstring
Logs a processing ``message`` of a specified ``level`` to the QGIS message log.
%End

};


Expand Down
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -37,9 +37,10 @@
from qgis.core import (QgsApplication,
QgsVectorFileWriter,
QgsProcessingFeedback,
QgsProcessingUtils,
QgsMessageLog,
QgsSettings)
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
from processing.tools.system import isWindows, isMac

try:
Expand Down Expand Up @@ -79,7 +80,7 @@ def runGdal(commands, feedback=None):
os.putenv('PATH', envval)

fused_command = ' '.join([str(c) for c in commands])
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, fused_command)
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, fused_command)
feedback.pushInfo('GDAL command:')
feedback.pushCommandInfo(fused_command)
feedback.pushInfo('GDAL command output:')
Expand Down Expand Up @@ -107,7 +108,7 @@ def runGdal(commands, feedback=None):
else:
raise IOError(e.message + u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}'.format(len(loglines), u'\n'.join(loglines[-10:])))

ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, loglines)
GdalUtils.consoleOutput = loglines

@staticmethod
Expand Down
11 changes: 5 additions & 6 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -34,12 +34,13 @@
from qgis.PyQt.QtCore import QCoreApplication, QUrl

from qgis.core import (QgsRasterLayer,
QgsApplication)
QgsApplication,
QgsProcessingUtils,
QgsMessageLog)
from qgis.utils import iface

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

from processing.core.parameters import (getParameterFromString,
Expand Down Expand Up @@ -188,9 +189,7 @@ def defineCharacteristicsFromFile(self):
"txt"))
line = lines.readline().strip('\n').strip()
except Exception as e:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(self.descriptionFile, line))
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL, self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(self.descriptionFile, line))
raise e

self.addParameter(ParameterExtent(
Expand Down Expand Up @@ -300,7 +299,7 @@ def processAlgorithm(self, context, feedback):
feedback.pushCommandInfo(line)
loglines.append(line)
if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_COMMANDS):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, loglines)

Grass7Utils.executeGrass7(self.commands, feedback, self.outputCommands)

Expand Down
13 changes: 7 additions & 6 deletions python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py
Expand Up @@ -29,9 +29,10 @@
import os
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsApplication,
QgsProcessingProvider)
QgsProcessingProvider,
QgsMessageLog,
QgsProcessingUtils)
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.core.ProcessingLog import ProcessingLog
from .Grass7Utils import Grass7Utils
from .Grass7Algorithm import Grass7Algorithm
from processing.tools.system import isWindows, isMac
Expand Down Expand Up @@ -97,12 +98,12 @@ def createAlgsList(self):
if alg.name().strip() != '':
algs.append(alg)
else:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
QgsProcessingUtils.logMessage(
QgsMessageLog.CRITICAL,
self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile))
except Exception as e:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
QgsProcessingUtils.logMessage(
QgsMessageLog.CRITICAL,
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)))
algs.append(nviz7())
return algs
Expand Down
8 changes: 5 additions & 3 deletions python/plugins/processing/algs/grass7/Grass7Utils.py
Expand Up @@ -31,10 +31,12 @@
import shutil
import subprocess
import os
from qgis.core import QgsApplication

from qgis.core import (QgsApplication,
QgsProcessingUtils,
QgsMessageLog)
from qgis.PyQt.QtCore import QCoreApplication
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
from processing.tools.system import userFolder, isWindows, isMac, tempFolder, mkdir
from processing.tests.TestData import points

Expand Down Expand Up @@ -333,7 +335,7 @@ def executeGrass7(commands, feedback, outputCommands=None):
feedback.pushConsoleInfo(line)

if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_CONSOLE):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, loglines)

# GRASS session is used to hold the layers already exported or
# produced in GRASS between multiple calls to GRASS algorithms.
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/qgis/Centroids.py
Expand Up @@ -29,10 +29,11 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsWkbTypes, QgsProcessingUtils
from qgis.core import (QgsWkbTypes,
QgsProcessingUtils,
QgsMessageLog)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.ProcessingLog import ProcessingLog
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
Expand Down Expand Up @@ -80,8 +81,8 @@ def processAlgorithm(self, context, feedback):
if input_feature.geometry():
output_geometry = input_feature.geometry().centroid()
if not output_geometry:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
'Error calculating centroid for feature {}'.format(input_feature.id()))
QgsProcessingUtils.logMessage(QgsMessageLog.WARNING,
'Error calculating centroid for feature {}'.format(input_feature.id()))
output_feature.setGeometry(output_geometry)

writer.addFeature(output_feature)
Expand Down
16 changes: 10 additions & 6 deletions python/plugins/processing/algs/qgis/Clip.py
Expand Up @@ -29,10 +29,14 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeature, QgsGeometry, QgsFeatureRequest, QgsWkbTypes, QgsProcessingUtils
from qgis.core import (QgsFeature,
QgsGeometry,
QgsFeatureRequest,
QgsWkbTypes,
QgsMessageLog,
QgsProcessingUtils)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.ProcessingLog import ProcessingLog
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
Expand Down Expand Up @@ -137,10 +141,10 @@ def processAlgorithm(self, context, feedback):
out_feat.setAttributes(in_feat.attributes())
writer.addFeature(out_feat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('Feature geometry error: One or more '
'output features ignored due to '
'invalid geometry.'))
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL,
self.tr('Feature geometry error: One or more '
'output features ignored due to '
'invalid geometry.'))
continue

if single_clip_feature:
Expand Down
12 changes: 8 additions & 4 deletions python/plugins/processing/algs/qgis/Difference.py
Expand Up @@ -29,8 +29,12 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeatureRequest, QgsFeature, QgsGeometry, QgsWkbTypes, QgsProcessingUtils
from processing.core.ProcessingLog import ProcessingLog
from qgis.core import (QgsFeatureRequest,
QgsFeature,
QgsGeometry,
QgsWkbTypes,
QgsMessageLog,
QgsProcessingUtils)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
Expand Down Expand Up @@ -98,8 +102,8 @@ def processAlgorithm(self, context, feedback):
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
QgsProcessingUtils.logMessage(QgsMessageLog.WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

feedback.setProgress(int(current * total))
Expand Down
30 changes: 16 additions & 14 deletions python/plugins/processing/algs/qgis/Dissolve.py
Expand Up @@ -30,9 +30,11 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeature, QgsGeometry, QgsProcessingUtils
from qgis.core import (QgsFeature,
QgsGeometry,
QgsMessageLog,
QgsProcessingUtils)

from processing.core.ProcessingLog import ProcessingLog
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
Expand Down Expand Up @@ -106,12 +108,12 @@ def processAlgorithm(self, context, feedback):
errors = tmpInGeom.validateGeometry()
if len(errors) != 0:
for error in errors:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('ValidateGeometry()'
'error: One or more '
'input features have '
'invalid geometry: ') +
error.what())
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL,
self.tr('ValidateGeometry()'
'error: One or more '
'input features have '
'invalid geometry: ') +
error.what())
continue

geom_queue.append(tmpInGeom)
Expand Down Expand Up @@ -149,12 +151,12 @@ def processAlgorithm(self, context, feedback):
errors = tmpInGeom.validateGeometry()
if len(errors) != 0:
for error in errors:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('ValidateGeometry() '
'error: One or more input'
'features have invalid '
'geometry: ') +
error.what())
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL,
self.tr('ValidateGeometry() '
'error: One or more input'
'features have invalid '
'geometry: ') +
error.what())

if index_attrs not in attribute_dict:
# keep attributes of first feature
Expand Down
11 changes: 7 additions & 4 deletions python/plugins/processing/algs/qgis/EliminateSelection.py
Expand Up @@ -30,11 +30,14 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeatureRequest, QgsFeature, QgsGeometry
from qgis.core import (QgsFeatureRequest,
QgsFeature,
QgsGeometry,
QgsMessageLog,
QgsProcessingUtils)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.ProcessingLog import ProcessingLog
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
Expand Down Expand Up @@ -83,8 +86,8 @@ def processAlgorithm(self, context, feedback):
smallestArea = self.getParameterValue(self.MODE) == self.MODE_SMALLEST_AREA

if inLayer.selectedFeatureCount() == 0:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('{0}: (No selection in input layer "{1}")').format(self.displayName(), self.getParameterValue(self.INPUT)))
QgsProcessingUtils.logMessage(QgsMessageLog.WARNING,
self.tr('{0}: (No selection in input layer "{1}")').format(self.displayName(), self.getParameterValue(self.INPUT)))

featToEliminate = []
selFeatIds = inLayer.selectedFeatureIds()
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/FixGeometry.py
Expand Up @@ -28,13 +28,13 @@
from qgis.core import (QgsWkbTypes,
QgsGeometry,
QgsApplication,
QgsMessageLog,
QgsProcessingUtils)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.ProcessingLog import ProcessingLog
from processing.tools import dataobjects, vector


Expand Down Expand Up @@ -88,8 +88,8 @@ def processAlgorithm(self, context, feedback):
if inputFeature.geometry():
outputGeometry = inputFeature.geometry().makeValid()
if not outputGeometry:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
'makeValid failed for feature {}'.format(inputFeature.id()))
QgsProcessingUtils.logMessage(QgsMessageLog.WARNING,
'makeValid failed for feature {}'.format(inputFeature.id()))

if outputGeometry.wkbType() == QgsWkbTypes.Unknown or QgsWkbTypes.flatType(outputGeometry.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
tmpGeometries = outputGeometry.asGeometryCollection()
Expand Down
18 changes: 9 additions & 9 deletions python/plugins/processing/algs/qgis/Gridify.py
Expand Up @@ -30,10 +30,10 @@
QgsPoint,
QgsWkbTypes,
QgsApplication,
QgsMessageLog,
QgsProcessingUtils)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.ProcessingLog import ProcessingLog
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
Expand Down Expand Up @@ -100,8 +100,8 @@ def processAlgorithm(self, context, feedback):
elif geomType == QgsWkbTypes.LineString:
points = self._gridify(geom.asPolyline(), hSpacing, vSpacing)
if len(points) < 2:
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromPolyline(points)
Expand All @@ -112,8 +112,8 @@ def processAlgorithm(self, context, feedback):
if len(points) > 1:
polyline.append(points)
if len(polyline) <= 0:
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromMultiPolyline(polyline)
Expand All @@ -125,8 +125,8 @@ def processAlgorithm(self, context, feedback):
if len(points) > 1:
polygon.append(points)
if len(polygon) <= 0:
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromPolygon(polygon)
Expand All @@ -143,8 +143,8 @@ def processAlgorithm(self, context, feedback):
multipolygon.append(newPolygon)

if len(multipolygon) <= 0:
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromMultiPolygon(multipolygon)
Expand Down

0 comments on commit 1394c28

Please sign in to comment.