Skip to content

Commit 1394c28

Browse files
committed
[processing] Move some log handling to c++ class
1 parent 5169e0d commit 1394c28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+357
-299
lines changed

doc/api_break.dox

+1
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,7 @@ object of type QgsProcessingFeedback, and will need to adapt their use of progre
22292229
- SilentProgress was removed. Use the base QgsProcessingFeedback class instead.
22302230
- algList was removed. Use QgsApplication.processingRegistry() instead.
22312231
- Processing.algs was removed. QgsApplication.processingRegistry().algorithms() instead.
2232+
- ProcessingLog should not be used when reporting log messages from algorithms. Use QgsProcessingUtils.logMessage() instead.
22322233

22332234
Triangulation {#qgis_api_break_3_0_Triangulation}
22342235
-------------

python/core/processing/qgsprocessingutils.sip

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ class QgsProcessingUtils
124124
:rtype: list of QVariant
125125
%End
126126

127+
static void logMessage( QgsMessageLog::MessageLevel level, const QString &message );
128+
%Docstring
129+
Logs a processing ``message`` of a specified ``level`` to the QGIS message log.
130+
%End
131+
127132
};
128133

129134

python/plugins/processing/algs/gdal/GdalUtils.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
from qgis.core import (QgsApplication,
3838
QgsVectorFileWriter,
3939
QgsProcessingFeedback,
40+
QgsProcessingUtils,
41+
QgsMessageLog,
4042
QgsSettings)
4143
from processing.core.ProcessingConfig import ProcessingConfig
42-
from processing.core.ProcessingLog import ProcessingLog
4344
from processing.tools.system import isWindows, isMac
4445

4546
try:
@@ -79,7 +80,7 @@ def runGdal(commands, feedback=None):
7980
os.putenv('PATH', envval)
8081

8182
fused_command = ' '.join([str(c) for c in commands])
82-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, fused_command)
83+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, fused_command)
8384
feedback.pushInfo('GDAL command:')
8485
feedback.pushCommandInfo(fused_command)
8586
feedback.pushInfo('GDAL command output:')
@@ -107,7 +108,7 @@ def runGdal(commands, feedback=None):
107108
else:
108109
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:])))
109110

110-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
111+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, loglines)
111112
GdalUtils.consoleOutput = loglines
112113

113114
@staticmethod

python/plugins/processing/algs/grass7/Grass7Algorithm.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
from qgis.PyQt.QtCore import QCoreApplication, QUrl
3535

3636
from qgis.core import (QgsRasterLayer,
37-
QgsApplication)
37+
QgsApplication,
38+
QgsProcessingUtils,
39+
QgsMessageLog)
3840
from qgis.utils import iface
3941

4042
from processing.core.GeoAlgorithm import GeoAlgorithm
4143
from processing.core.ProcessingConfig import ProcessingConfig
42-
from processing.core.ProcessingLog import ProcessingLog
4344
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
4445

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

196195
self.addParameter(ParameterExtent(
@@ -300,7 +299,7 @@ def processAlgorithm(self, context, feedback):
300299
feedback.pushCommandInfo(line)
301300
loglines.append(line)
302301
if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_COMMANDS):
303-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
302+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, loglines)
304303

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

python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
import os
3030
from qgis.PyQt.QtCore import QCoreApplication
3131
from qgis.core import (QgsApplication,
32-
QgsProcessingProvider)
32+
QgsProcessingProvider,
33+
QgsMessageLog,
34+
QgsProcessingUtils)
3335
from processing.core.ProcessingConfig import ProcessingConfig, Setting
34-
from processing.core.ProcessingLog import ProcessingLog
3536
from .Grass7Utils import Grass7Utils
3637
from .Grass7Algorithm import Grass7Algorithm
3738
from processing.tools.system import isWindows, isMac
@@ -97,12 +98,12 @@ def createAlgsList(self):
9798
if alg.name().strip() != '':
9899
algs.append(alg)
99100
else:
100-
ProcessingLog.addToLog(
101-
ProcessingLog.LOG_ERROR,
101+
QgsProcessingUtils.logMessage(
102+
QgsMessageLog.CRITICAL,
102103
self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile))
103104
except Exception as e:
104-
ProcessingLog.addToLog(
105-
ProcessingLog.LOG_ERROR,
105+
QgsProcessingUtils.logMessage(
106+
QgsMessageLog.CRITICAL,
106107
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)))
107108
algs.append(nviz7())
108109
return algs

python/plugins/processing/algs/grass7/Grass7Utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
import shutil
3232
import subprocess
3333
import os
34-
from qgis.core import QgsApplication
34+
35+
from qgis.core import (QgsApplication,
36+
QgsProcessingUtils,
37+
QgsMessageLog)
3538
from qgis.PyQt.QtCore import QCoreApplication
3639
from processing.core.ProcessingConfig import ProcessingConfig
37-
from processing.core.ProcessingLog import ProcessingLog
3840
from processing.tools.system import userFolder, isWindows, isMac, tempFolder, mkdir
3941
from processing.tests.TestData import points
4042

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

335337
if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_CONSOLE):
336-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
338+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO, loglines)
337339

338340
# GRASS session is used to hold the layers already exported or
339341
# produced in GRASS between multiple calls to GRASS algorithms.

python/plugins/processing/algs/qgis/Centroids.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929

3030
from qgis.PyQt.QtGui import QIcon
3131

32-
from qgis.core import QgsWkbTypes, QgsProcessingUtils
32+
from qgis.core import (QgsWkbTypes,
33+
QgsProcessingUtils,
34+
QgsMessageLog)
3335

3436
from processing.core.GeoAlgorithm import GeoAlgorithm
35-
from processing.core.ProcessingLog import ProcessingLog
3637
from processing.core.parameters import ParameterVector
3738
from processing.core.outputs import OutputVector
3839
from processing.tools import dataobjects, vector
@@ -80,8 +81,8 @@ def processAlgorithm(self, context, feedback):
8081
if input_feature.geometry():
8182
output_geometry = input_feature.geometry().centroid()
8283
if not output_geometry:
83-
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
84-
'Error calculating centroid for feature {}'.format(input_feature.id()))
84+
QgsProcessingUtils.logMessage(QgsMessageLog.WARNING,
85+
'Error calculating centroid for feature {}'.format(input_feature.id()))
8586
output_feature.setGeometry(output_geometry)
8687

8788
writer.addFeature(output_feature)

python/plugins/processing/algs/qgis/Clip.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929

3030
from qgis.PyQt.QtGui import QIcon
3131

32-
from qgis.core import QgsFeature, QgsGeometry, QgsFeatureRequest, QgsWkbTypes, QgsProcessingUtils
32+
from qgis.core import (QgsFeature,
33+
QgsGeometry,
34+
QgsFeatureRequest,
35+
QgsWkbTypes,
36+
QgsMessageLog,
37+
QgsProcessingUtils)
3338

3439
from processing.core.GeoAlgorithm import GeoAlgorithm
35-
from processing.core.ProcessingLog import ProcessingLog
3640
from processing.core.parameters import ParameterVector
3741
from processing.core.outputs import OutputVector
3842
from processing.tools import dataobjects, vector
@@ -137,10 +141,10 @@ def processAlgorithm(self, context, feedback):
137141
out_feat.setAttributes(in_feat.attributes())
138142
writer.addFeature(out_feat)
139143
except:
140-
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
141-
self.tr('Feature geometry error: One or more '
142-
'output features ignored due to '
143-
'invalid geometry.'))
144+
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL,
145+
self.tr('Feature geometry error: One or more '
146+
'output features ignored due to '
147+
'invalid geometry.'))
144148
continue
145149

146150
if single_clip_feature:

python/plugins/processing/algs/qgis/Difference.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929

3030
from qgis.PyQt.QtGui import QIcon
3131

32-
from qgis.core import QgsFeatureRequest, QgsFeature, QgsGeometry, QgsWkbTypes, QgsProcessingUtils
33-
from processing.core.ProcessingLog import ProcessingLog
32+
from qgis.core import (QgsFeatureRequest,
33+
QgsFeature,
34+
QgsGeometry,
35+
QgsWkbTypes,
36+
QgsMessageLog,
37+
QgsProcessingUtils)
3438
from processing.core.GeoAlgorithm import GeoAlgorithm
3539
from processing.core.parameters import ParameterVector
3640
from processing.core.outputs import OutputVector
@@ -98,8 +102,8 @@ def processAlgorithm(self, context, feedback):
98102
outFeat.setAttributes(attrs)
99103
writer.addFeature(outFeat)
100104
except:
101-
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
102-
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
105+
QgsProcessingUtils.logMessage(QgsMessageLog.WARNING,
106+
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
103107
continue
104108

105109
feedback.setProgress(int(current * total))

python/plugins/processing/algs/qgis/Dissolve.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030

3131
from qgis.PyQt.QtGui import QIcon
3232

33-
from qgis.core import QgsFeature, QgsGeometry, QgsProcessingUtils
33+
from qgis.core import (QgsFeature,
34+
QgsGeometry,
35+
QgsMessageLog,
36+
QgsProcessingUtils)
3437

35-
from processing.core.ProcessingLog import ProcessingLog
3638
from processing.core.GeoAlgorithm import GeoAlgorithm
3739
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3840
from processing.core.parameters import ParameterVector
@@ -106,12 +108,12 @@ def processAlgorithm(self, context, feedback):
106108
errors = tmpInGeom.validateGeometry()
107109
if len(errors) != 0:
108110
for error in errors:
109-
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
110-
self.tr('ValidateGeometry()'
111-
'error: One or more '
112-
'input features have '
113-
'invalid geometry: ') +
114-
error.what())
111+
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL,
112+
self.tr('ValidateGeometry()'
113+
'error: One or more '
114+
'input features have '
115+
'invalid geometry: ') +
116+
error.what())
115117
continue
116118

117119
geom_queue.append(tmpInGeom)
@@ -149,12 +151,12 @@ def processAlgorithm(self, context, feedback):
149151
errors = tmpInGeom.validateGeometry()
150152
if len(errors) != 0:
151153
for error in errors:
152-
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
153-
self.tr('ValidateGeometry() '
154-
'error: One or more input'
155-
'features have invalid '
156-
'geometry: ') +
157-
error.what())
154+
QgsProcessingUtils.logMessage(QgsMessageLog.CRITICAL,
155+
self.tr('ValidateGeometry() '
156+
'error: One or more input'
157+
'features have invalid '
158+
'geometry: ') +
159+
error.what())
158160

159161
if index_attrs not in attribute_dict:
160162
# keep attributes of first feature

python/plugins/processing/algs/qgis/EliminateSelection.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030

3131
from qgis.PyQt.QtGui import QIcon
3232

33-
from qgis.core import QgsFeatureRequest, QgsFeature, QgsGeometry
33+
from qgis.core import (QgsFeatureRequest,
34+
QgsFeature,
35+
QgsGeometry,
36+
QgsMessageLog,
37+
QgsProcessingUtils)
3438

3539
from processing.core.GeoAlgorithm import GeoAlgorithm
3640
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
37-
from processing.core.ProcessingLog import ProcessingLog
3841
from processing.core.parameters import ParameterVector
3942
from processing.core.parameters import ParameterSelection
4043
from processing.core.outputs import OutputVector
@@ -83,8 +86,8 @@ def processAlgorithm(self, context, feedback):
8386
smallestArea = self.getParameterValue(self.MODE) == self.MODE_SMALLEST_AREA
8487

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

8992
featToEliminate = []
9093
selFeatIds = inLayer.selectedFeatureIds()

python/plugins/processing/algs/qgis/FixGeometry.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
from qgis.core import (QgsWkbTypes,
2929
QgsGeometry,
3030
QgsApplication,
31+
QgsMessageLog,
3132
QgsProcessingUtils)
3233

3334
from processing.core.GeoAlgorithm import GeoAlgorithm
3435
from processing.core.parameters import ParameterVector
3536
from processing.core.outputs import OutputVector
3637
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
37-
from processing.core.ProcessingLog import ProcessingLog
3838
from processing.tools import dataobjects, vector
3939

4040

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

9494
if outputGeometry.wkbType() == QgsWkbTypes.Unknown or QgsWkbTypes.flatType(outputGeometry.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
9595
tmpGeometries = outputGeometry.asGeometryCollection()

python/plugins/processing/algs/qgis/Gridify.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
QgsPoint,
3131
QgsWkbTypes,
3232
QgsApplication,
33+
QgsMessageLog,
3334
QgsProcessingUtils)
3435
from processing.core.GeoAlgorithm import GeoAlgorithm
3536
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36-
from processing.core.ProcessingLog import ProcessingLog
3737
from processing.core.parameters import ParameterVector
3838
from processing.core.parameters import ParameterNumber
3939
from processing.core.outputs import OutputVector
@@ -100,8 +100,8 @@ def processAlgorithm(self, context, feedback):
100100
elif geomType == QgsWkbTypes.LineString:
101101
points = self._gridify(geom.asPolyline(), hSpacing, vSpacing)
102102
if len(points) < 2:
103-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
104-
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
103+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
104+
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
105105
newGeom = None
106106
else:
107107
newGeom = QgsGeometry.fromPolyline(points)
@@ -112,8 +112,8 @@ def processAlgorithm(self, context, feedback):
112112
if len(points) > 1:
113113
polyline.append(points)
114114
if len(polyline) <= 0:
115-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
116-
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
115+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
116+
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
117117
newGeom = None
118118
else:
119119
newGeom = QgsGeometry.fromMultiPolyline(polyline)
@@ -125,8 +125,8 @@ def processAlgorithm(self, context, feedback):
125125
if len(points) > 1:
126126
polygon.append(points)
127127
if len(polygon) <= 0:
128-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
129-
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
128+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
129+
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
130130
newGeom = None
131131
else:
132132
newGeom = QgsGeometry.fromPolygon(polygon)
@@ -143,8 +143,8 @@ def processAlgorithm(self, context, feedback):
143143
multipolygon.append(newPolygon)
144144

145145
if len(multipolygon) <= 0:
146-
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
147-
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
146+
QgsProcessingUtils.logMessage(QgsMessageLog.INFO,
147+
self.tr('Failed to gridify feature with FID {0}').format(f.id()))
148148
newGeom = None
149149
else:
150150
newGeom = QgsGeometry.fromMultiPolygon(multipolygon)

0 commit comments

Comments
 (0)