Skip to content
Permalink
Browse files

[processing] Fix use of "Save log to file" algorithm in models

run from the qgis_process standalone tool

(cherry picked from commit 6daeebc)
  • Loading branch information
nyalldawson committed Mar 19, 2021
1 parent fbc2540 commit be8401d23f82efdf7a0ec66ff7de0f98aba43213
@@ -60,9 +60,10 @@ void ConsoleFeedback::setProgressText( const QString &text )
{
if ( !mUseJson )
std::cout << text.toLocal8Bit().constData() << '\n';
QgsProcessingFeedback::setProgressText( text );
}

void ConsoleFeedback::reportError( const QString &error, bool )
void ConsoleFeedback::reportError( const QString &error, bool fatalError )
{
if ( !mUseJson )
std::cerr << "ERROR:\t" << error.toLocal8Bit().constData() << '\n';
@@ -72,6 +73,7 @@ void ConsoleFeedback::reportError( const QString &error, bool )
mJsonLog.insert( QStringLiteral( "errors" ), QStringList() );
mJsonLog[ QStringLiteral( "errors" )] = mJsonLog.value( QStringLiteral( "errors" ) ).toStringList() << error;
}
QgsProcessingFeedback::reportError( error, fatalError );
}

void ConsoleFeedback::pushWarning( const QString &warning )
@@ -84,6 +86,7 @@ void ConsoleFeedback::pushWarning( const QString &warning )
mJsonLog.insert( QStringLiteral( "warning" ), QStringList() );
mJsonLog[ QStringLiteral( "warning" )] = mJsonLog.value( QStringLiteral( "warning" ) ).toStringList() << warning;
}
QgsProcessingFeedback::pushWarning( warning );
}

void ConsoleFeedback::pushInfo( const QString &info )
@@ -96,6 +99,7 @@ void ConsoleFeedback::pushInfo( const QString &info )
mJsonLog.insert( QStringLiteral( "info" ), QStringList() );
mJsonLog[ QStringLiteral( "info" )] = mJsonLog.value( QStringLiteral( "info" ) ).toStringList() << info;
}
QgsProcessingFeedback::pushInfo( info );
}

void ConsoleFeedback::pushCommandInfo( const QString &info )
@@ -108,6 +112,7 @@ void ConsoleFeedback::pushCommandInfo( const QString &info )
mJsonLog.insert( QStringLiteral( "info" ), QStringList() );
mJsonLog[ QStringLiteral( "info" )] = mJsonLog.value( QStringLiteral( "info" ) ).toStringList() << info;
}
QgsProcessingFeedback::pushCommandInfo( info );
}

void ConsoleFeedback::pushDebugInfo( const QString &info )
@@ -120,6 +125,7 @@ void ConsoleFeedback::pushDebugInfo( const QString &info )
mJsonLog.insert( QStringLiteral( "info" ), QStringList() );
mJsonLog[ QStringLiteral( "info" )] = mJsonLog.value( QStringLiteral( "info" ) ).toStringList() << info;
}
QgsProcessingFeedback::pushDebugInfo( info );
}

void ConsoleFeedback::pushConsoleInfo( const QString &info )
@@ -132,6 +138,7 @@ void ConsoleFeedback::pushConsoleInfo( const QString &info )
mJsonLog.insert( QStringLiteral( "info" ), QStringList() );
mJsonLog[ QStringLiteral( "info" )] = mJsonLog.value( QStringLiteral( "info" ) ).toStringList() << info;
}
QgsProcessingFeedback::pushConsoleInfo( info );
}

QVariantMap ConsoleFeedback::jsonLog() const
@@ -266,6 +266,20 @@ def testModelRunJson(self):
self.assertEqual(res['algorithm_details']['id'], 'Test model')
self.assertTrue(os.path.exists(output_file))

def testModelRunWithLog(self):
output_file = self.TMP_DIR + '/model_log.log'
rc, output, err = self.run_process(['run', TEST_DATA_DIR + '/test_logging_model.model3', '--', 'logfile={}'.format(output_file)])
self.assertIn('Test logged message', err)
self.assertEqual(rc, 0)
self.assertIn('0...10...20...30...40...50...60...70...80...90', output.lower())
self.assertIn('results', output.lower())
self.assertTrue(os.path.exists(output_file))

with open(output_file, 'rt') as f:
lines = '\n'.join(f.readlines())

self.assertIn('Test logged message', lines)


if __name__ == '__main__':
# look for qgis bin path
@@ -0,0 +1,135 @@
<!DOCTYPE model>
<Option type="Map">
<Option name="children" type="Map">
<Option name="native:raisewarning_1" type="Map">
<Option name="active" value="true" type="bool"/>
<Option name="alg_config"/>
<Option name="alg_id" value="native:raisewarning" type="QString"/>
<Option name="color" value="" type="QString"/>
<Option name="comment" type="Map">
<Option name="color" value="" type="QString"/>
<Option name="component_description" value="" type="QString"/>
<Option name="component_height" value="60" type="double"/>
<Option name="component_pos_x" value="467.5" type="double"/>
<Option name="component_pos_y" value="48.5" type="double"/>
<Option name="component_width" value="100" type="double"/>
<Option name="outputs_collapsed" value="true" type="bool"/>
<Option name="parameters_collapsed" value="true" type="bool"/>
</Option>
<Option name="component_description" value="Raise warning" type="QString"/>
<Option name="component_height" value="30" type="double"/>
<Option name="component_pos_x" value="267.5" type="double"/>
<Option name="component_pos_y" value="93.5" type="double"/>
<Option name="component_width" value="200" type="double"/>
<Option name="dependencies"/>
<Option name="id" value="native:raisewarning_1" type="QString"/>
<Option name="outputs"/>
<Option name="outputs_collapsed" value="true" type="bool"/>
<Option name="parameters_collapsed" value="true" type="bool"/>
<Option name="params" type="Map">
<Option name="CONDITION" type="List">
<Option type="Map">
<Option name="source" value="2" type="int"/>
<Option name="static_value" value="" type="QString"/>
</Option>
</Option>
<Option name="MESSAGE" type="List">
<Option type="Map">
<Option name="source" value="2" type="int"/>
<Option name="static_value" value="Test logged message" type="QString"/>
</Option>
</Option>
</Option>
</Option>
<Option name="native:savelog_1" type="Map">
<Option name="active" value="true" type="bool"/>
<Option name="alg_config"/>
<Option name="alg_id" value="native:savelog" type="QString"/>
<Option name="color" value="" type="QString"/>
<Option name="comment" type="Map">
<Option name="color" value="" type="QString"/>
<Option name="component_description" value="" type="QString"/>
<Option name="component_height" value="60" type="double"/>
<Option name="component_pos_x" value="571.5" type="double"/>
<Option name="component_pos_y" value="139" type="double"/>
<Option name="component_width" value="100" type="double"/>
<Option name="outputs_collapsed" value="true" type="bool"/>
<Option name="parameters_collapsed" value="true" type="bool"/>
</Option>
<Option name="component_description" value="Save log to file" type="QString"/>
<Option name="component_height" value="30" type="double"/>
<Option name="component_pos_x" value="371.5" type="double"/>
<Option name="component_pos_y" value="184" type="double"/>
<Option name="component_width" value="200" type="double"/>
<Option name="dependencies" type="List">
<Option type="Map">
<Option name="child_id" value="native:raisewarning_1" type="QString"/>
<Option name="conditional_branch" value="" type="QString"/>
</Option>
</Option>
<Option name="id" value="native:savelog_1" type="QString"/>
<Option name="outputs"/>
<Option name="outputs_collapsed" value="true" type="bool"/>
<Option name="parameters_collapsed" value="true" type="bool"/>
<Option name="params" type="Map">
<Option name="OUTPUT" type="List">
<Option type="Map">
<Option name="parameter_name" value="logfile" type="QString"/>
<Option name="source" value="0" type="int"/>
</Option>
</Option>
<Option name="USE_HTML" type="List">
<Option type="Map">
<Option name="source" value="2" type="int"/>
<Option name="static_value" value="false" type="bool"/>
</Option>
</Option>
</Option>
</Option>
</Option>
<Option name="designerParameterValues" type="Map">
<Option name="logfile" value="/tmp/test.log" type="QString"/>
</Option>
<Option name="groupBoxes"/>
<Option name="help"/>
<Option name="modelVariables"/>
<Option name="model_group" value="" type="QString"/>
<Option name="model_name" value="Test logger" type="QString"/>
<Option name="parameterDefinitions" type="Map">
<Option name="logfile" type="Map">
<Option name="default" value="" type="QString"/>
<Option name="defaultGui" type="invalid"/>
<Option name="description" value="log_file" type="QString"/>
<Option name="flags" value="0" type="int"/>
<Option name="help" value="" type="QString"/>
<Option name="metadata"/>
<Option name="multiline" value="false" type="bool"/>
<Option name="name" value="logfile" type="QString"/>
<Option name="parameter_type" value="string" type="QString"/>
</Option>
</Option>
<Option name="parameterOrder"/>
<Option name="parameters" type="Map">
<Option name="logfile" type="Map">
<Option name="color" value="" type="QString"/>
<Option name="comment" type="Map">
<Option name="color" value="" type="QString"/>
<Option name="component_description" value="" type="QString"/>
<Option name="component_height" value="60" type="double"/>
<Option name="component_pos_x" value="408" type="double"/>
<Option name="component_pos_y" value="226" type="double"/>
<Option name="component_width" value="100" type="double"/>
<Option name="outputs_collapsed" value="true" type="bool"/>
<Option name="parameters_collapsed" value="true" type="bool"/>
</Option>
<Option name="component_description" value="logfile" type="QString"/>
<Option name="component_height" value="30" type="double"/>
<Option name="component_pos_x" value="146" type="double"/>
<Option name="component_pos_y" value="282" type="double"/>
<Option name="component_width" value="200" type="double"/>
<Option name="name" value="logfile" type="QString"/>
<Option name="outputs_collapsed" value="true" type="bool"/>
<Option name="parameters_collapsed" value="true" type="bool"/>
</Option>
</Option>
</Option>

0 comments on commit be8401d

Please sign in to comment.