QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 #include <QTextStream>
20 
22 
23 QString QgsSaveLogToFileAlgorithm::name() const
24 {
25  return QStringLiteral( "savelog" );
26 }
27 
28 QgsProcessingAlgorithm::Flags QgsSaveLogToFileAlgorithm::flags() const
29 {
30  return QgsProcessingAlgorithm::flags() | FlagHideFromToolbox | FlagSkipGenericModelLogging;
31 }
32 
33 QString QgsSaveLogToFileAlgorithm::displayName() const
34 {
35  return QObject::tr( "Save log to file" );
36 }
37 
38 QStringList QgsSaveLogToFileAlgorithm::tags() const
39 {
40  return QObject::tr( "record,messages,logged" ).split( ',' );
41 }
42 
43 QString QgsSaveLogToFileAlgorithm::group() const
44 {
45  return QObject::tr( "Modeler tools" );
46 }
47 
48 QString QgsSaveLogToFileAlgorithm::groupId() const
49 {
50  return QStringLiteral( "modelertools" );
51 }
52 
53 QString QgsSaveLogToFileAlgorithm::shortHelpString() const
54 {
55  return QObject::tr( "This algorithm saves the model's execution log to a file.\n"
56  "Optionally, the log can be saved in a HTML formatted version." );
57 }
58 
59 QString QgsSaveLogToFileAlgorithm::shortDescription() const
60 {
61  return QObject::tr( "Saves the model's log contents to a file." );
62 }
63 
64 QgsSaveLogToFileAlgorithm *QgsSaveLogToFileAlgorithm::createInstance() const
65 {
66  return new QgsSaveLogToFileAlgorithm();
67 }
68 
69 void QgsSaveLogToFileAlgorithm::initAlgorithm( const QVariantMap & )
70 {
71  addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Log file" ), QObject::tr( "Text files (*.txt);;HTML files (*.html *.HTML)" ) ) );
72  addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_HTML" ), QObject::tr( "Use HTML formatting" ), false ) );
73 }
74 
75 QVariantMap QgsSaveLogToFileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76 {
77  const QString file = parameterAsFile( parameters, QStringLiteral( "OUTPUT" ), context );
78  const bool useHtml = parameterAsBool( parameters, QStringLiteral( "USE_HTML" ), context );
79  if ( !file.isEmpty() )
80  {
81  QFile exportFile( file );
82  if ( !exportFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
83  {
84  throw QgsProcessingException( QObject::tr( "Could not save log to file %1" ).arg( file ) );
85  }
86  QTextStream fout( &exportFile );
87 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
88  fout.setCodec( "UTF-8" );
89 #endif
90  fout << ( useHtml ? feedback->htmlLog() : feedback->textLog() );
91  }
92  QVariantMap res;
93  res.insert( QStringLiteral( "OUTPUT" ), file );
94  return res;
95 }
96 
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
virtual QString textLog() const
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
virtual QString htmlLog() const
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
A boolean parameter for processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...