QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsalgorithmsavelog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmsavelog.cpp
3  ---------------------
4  begin : March 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsalgorithmsavelog.h"
19 
21 
22 QString QgsSaveLogToFileAlgorithm::name() const
23 {
24  return QStringLiteral( "savelog" );
25 }
26 
27 QgsProcessingAlgorithm::Flags QgsSaveLogToFileAlgorithm::flags() const
28 {
29  return QgsProcessingAlgorithm::flags() | FlagHideFromToolbox | FlagSkipGenericModelLogging;
30 }
31 
32 QString QgsSaveLogToFileAlgorithm::displayName() const
33 {
34  return QObject::tr( "Save log to file" );
35 }
36 
37 QStringList QgsSaveLogToFileAlgorithm::tags() const
38 {
39  return QObject::tr( "record,messages,logged" ).split( ',' );
40 }
41 
42 QString QgsSaveLogToFileAlgorithm::group() const
43 {
44  return QObject::tr( "Modeler tools" );
45 }
46 
47 QString QgsSaveLogToFileAlgorithm::groupId() const
48 {
49  return QStringLiteral( "modelertools" );
50 }
51 
52 QString QgsSaveLogToFileAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm saves the model's execution log to a file.\n"
55  "Optionally, the log can be saved in a HTML formatted version." );
56 }
57 
58 QString QgsSaveLogToFileAlgorithm::shortDescription() const
59 {
60  return QObject::tr( "Saves the model's log contents to a file." );
61 }
62 
63 QgsSaveLogToFileAlgorithm *QgsSaveLogToFileAlgorithm::createInstance() const
64 {
65  return new QgsSaveLogToFileAlgorithm();
66 }
67 
68 void QgsSaveLogToFileAlgorithm::initAlgorithm( const QVariantMap & )
69 {
70  addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Log file" ), QObject::tr( "Text files (*.txt);;HTML files (*.html *.HTML)" ) ) );
71  addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_HTML" ), QObject::tr( "Use HTML formatting" ), false ) );
72 }
73 
74 QVariantMap QgsSaveLogToFileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75 {
76  const QString file = parameterAsFile( parameters, QStringLiteral( "OUTPUT" ), context );
77  const bool useHtml = parameterAsBool( parameters, QStringLiteral( "USE_HTML" ), context );
78  if ( !file.isEmpty() )
79  {
80  QFile exportFile( file );
81  if ( !exportFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
82  {
83  throw QgsProcessingException( QObject::tr( "Could not save log to file %1" ).arg( file ) );
84  }
85  QTextStream fout( &exportFile );
86  fout << ( useHtml ? feedback->htmlLog() : feedback->textLog() );
87  }
88  QVariantMap res;
89  res.insert( QStringLiteral( "OUTPUT" ), file );
90  return res;
91 }
92 
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::textLog
virtual QString textLog() const
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
Definition: qgsprocessingfeedback.cpp:112
qgsalgorithmsavelog.h
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterFileDestination
Definition: qgsprocessingparameters.h:3005
QgsProcessingParameterBoolean
Definition: qgsprocessingparameters.h:1439
QgsProcessingFeedback::htmlLog
virtual QString htmlLog() const
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
Definition: qgsprocessingfeedback.cpp:107
QgsProcessingAlgorithm::flags
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Definition: qgsprocessingalgorithm.cpp:88
QgsProcessingException
Definition: qgsexception.h:82