Skip to content

Commit

Permalink
Merge pull request #4400 from nyalldawson/processing_log
Browse files Browse the repository at this point in the history
[processing] Move some log handling to c++ class
  • Loading branch information
nyalldawson committed Apr 26, 2017
2 parents 6b4ddb3 + e9f13d1 commit 7c9b00c
Show file tree
Hide file tree
Showing 49 changed files with 324 additions and 310 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 QgsMessageLog.logMessage() instead.

Triangulation {#qgis_api_break_3_0_Triangulation}
-------------
Expand Down
9 changes: 5 additions & 4 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)
QgsMessageLog.logMessage(fused_command, 'Processing', QgsMessageLog.INFO)
feedback.pushInfo('GDAL command:')
feedback.pushCommandInfo(fused_command)
feedback.pushInfo('GDAL command output:')
Expand Down Expand Up @@ -107,8 +108,8 @@ 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)
GdalUtils.consoleOutput = loglines
QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', QgsMessageLog.INFO)
GdalUtils.consoleOutput = loglines

@staticmethod
def getConsoleOutput():
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))
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(self.descriptionFile, line), self.tr('Processing'), QgsMessageLog.CRITICAL)
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)
QgsMessageLog.logMessage(loglines, self.tr('Processing'), QgsMessageLog.INFO)

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

Expand Down
14 changes: 6 additions & 8 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,13 +98,10 @@ def createAlgsList(self):
if alg.name().strip() != '':
algs.append(alg)
else:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile))
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile), self.tr('Processing'), QgsMessageLog.CRITICAL)
except Exception as e:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)))
QgsMessageLog.logMessage(
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)), self.tr('Processing'), QgsMessageLog.CRITICAL)
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)
QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', QgsMessageLog.INFO)

# GRASS session is used to hold the layers already exported or
# produced in GRASS between multiple calls to GRASS algorithms.
Expand Down
8 changes: 4 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,7 @@ 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()))
QgsMessageLog.logMessage('Error calculating centroid for feature {}'.format(input_feature.id()), self.tr('Processing'), QgsMessageLog.WARNING)
output_feature.setGeometry(output_geometry)

writer.addFeature(output_feature)
Expand Down
15 changes: 9 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,9 @@ 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.'))
QgsMessageLog.logMessage(self.tr('Feature geometry error: One or more '
'output features ignored due to '
'invalid geometry.'), self.tr('Processing'), QgsMessageLog.CRITICAL)
continue

if single_clip_feature:
Expand Down
11 changes: 7 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,7 @@ 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.'))
QgsMessageLog.logMessage(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'), self.tr('Processing'), QgsMessageLog.WARNING)
continue

feedback.setProgress(int(current * total))
Expand Down
28 changes: 14 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,11 @@ 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())
QgsMessageLog.logMessage(self.tr('ValidateGeometry()'
'error: One or more '
'input features have '
'invalid geometry: ') +
error.what(), self.tr('Processing'), QgsMessageLog.CRITICAL)
continue

geom_queue.append(tmpInGeom)
Expand Down Expand Up @@ -149,12 +150,11 @@ 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())
QgsMessageLog.logMessage(self.tr('ValidateGeometry() '
'error: One or more input'
'features have invalid '
'geometry: ') +
error.what(), self.tr('Processing'), QgsMessageLog.CRITICAL)

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)))
QgsMessageLog.logMessage(self.tr('{0}: (No selection in input layer "{1}")').format(self.displayName(), self.getParameterValue(self.INPUT)),
self.tr('Processing'), QgsMessageLog.WARNING)

featToEliminate = []
selFeatIds = inLayer.selectedFeatureIds()
Expand Down
5 changes: 2 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,7 @@ 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()))
QgsMessageLog.logMessage('makeValid failed for feature {}'.format(inputFeature.id()), self.tr('Processing'), QgsMessageLog.WARNING)

if outputGeometry.wkbType() == QgsWkbTypes.Unknown or QgsWkbTypes.flatType(outputGeometry.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
tmpGeometries = outputGeometry.asGeometryCollection()
Expand Down
14 changes: 5 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,7 @@ 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()))
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
newGeom = None
else:
newGeom = QgsGeometry.fromPolyline(points)
Expand All @@ -112,8 +111,7 @@ 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()))
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
newGeom = None
else:
newGeom = QgsGeometry.fromMultiPolyline(polyline)
Expand All @@ -125,8 +123,7 @@ 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()))
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
newGeom = None
else:
newGeom = QgsGeometry.fromPolygon(polygon)
Expand All @@ -143,8 +140,7 @@ 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()))
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
newGeom = None
else:
newGeom = QgsGeometry.fromMultiPolygon(multipolygon)
Expand Down

0 comments on commit 7c9b00c

Please sign in to comment.