OpenLB 1.8.1
Loading...
Searching...
No Matches
xmlReaderOutput.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022 Simon Großmann
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef IO_XML_READER_OUTPUT_H_
25#define IO_XML_READER_OUTPUT_H_
26
27#include <string>
28#include <vector>
29#include <iostream>
30
31#include "io/ostreamManager.h"
32
33namespace olb {
34
36
38public:
39 // printing the output of the xmlReader regarding readOrWarn
40 template<typename ParameterType>
41 void parameterReading(std::vector<std::string> parameters, ParameterType& var,
42 bool defaultAvailable, bool exitIfMissing, bool showWarning) const;
43 void loadFile(bool loadOK, std::string fName) const;
44 void readValue(bool warningsOn, std::string name, std::string fName) const;
45 void printWarning(std::string name, std::string typeName, std::string value,
46 bool verboseOn, bool exitIfMissing) const;
47 template<typename XMLreaderType>
48 void print(int indent, XMLreaderType& xmlReader) const;
49
50
51 XMLreaderOutput() : clout(std::cerr, "xmlReaderOutput"){}
52 XMLreaderOutput(std::ostream& stream) : clout(stream, "xmlReaderOutput"){}
53 XMLreaderOutput(OutputChannel outputChannel);
54
55protected:
57 bool _inDebugMode = false;
58};
59
60XMLreaderOutput::XMLreaderOutput(OutputChannel outputChannel) : clout(std::cerr, "xmlReaderOutput"){
61 if (outputChannel == OutputChannel::TERMINAL){
62 clout = OstreamManager(std::cout, "xmlReaderOutput");
63 }
64 else if (outputChannel == OutputChannel::ERRCHANNEL){
65 clout = OstreamManager(std::cerr, "xmlReaderOutput");
66 } else{
67 clout = OstreamManager(std::cerr, "xmlReaderOutput");
68 }
69#ifdef OLB_DEBUG
70 _inDebugMode = true;
71#endif
72}
73
74template<typename ParameterType>
75void XMLreaderOutput::parameterReading(std::vector<std::string> parameters,
76 ParameterType& var,
77 bool defaultAvailable,
78 bool exitIfMissing,
79 bool showWarning) const
80{
81 if (showWarning || _inDebugMode) {
82 clout << "Warning: Cannot read parameter from XML File: ";
83 std::for_each(parameters.begin(), parameters.end(), [this](const std::string name_parameter)
84 { clout << "<" << name_parameter << ">"; });
85 clout << std::endl;
86
87 if ( exitIfMissing ) {
88 clout << "Error: This program cannot continue without ";
89 std::for_each(parameters.begin(), parameters.end(), [this](const std::string name_parameter)
90 { clout << "<" << name_parameter << ">"; });
91 clout << ". Optimization aborted." << std::endl;
92 exit(1);
93 }
94 if (defaultAvailable) {
95 clout << "\t Setting default value: " << parameters.back() << " = "<< var << std::endl;
96 }
97 else {
98 clout << "\t Setting arbitrarily: " << parameters.back() << " = " << var << std::endl;
99 }
100 }
101}
102
103
104void XMLreaderOutput::loadFile(bool loadOK, std::string fName) const
105{
106 if (!loadOK) {
107 clout << std::string("Problem processing input XML file ") << fName << std::endl;
108 }
109}
110
111void XMLreaderOutput::readValue(bool warningsOn, std::string name, std::string fName) const
112{
113 if ( warningsOn ) {
114 clout << "Warning: cannot read value from node \"" << name << "\"" << ", \"" << fName <<"\"" << std::endl;
115 }
116}
117
119void XMLreaderOutput::printWarning(std::string name, std::string typeName,
120 std::string value, bool verboseOn, bool exitIfMissing) const
121{
122
123 if ( verboseOn ) {
124 clout << "Warning: Cannot read " << typeName << " value from XML element " << name << "." << std::endl;
125 if ( ! value.empty() ) {
126 clout << " Setting default value = " << value << std::endl;
127 }
128 }
129 if ( exitIfMissing ) {
130 clout << "Error: This program cannot continue without \"" << name << "\". Optimization aborted." << std::endl;
131 exit(1);
132 }
133}
134
136template<typename XMLreaderType>
137void XMLreaderOutput::print(int indent, XMLreaderType& xmlReader) const{
138 std::string indentStr(indent, ' ');
139 clout << indentStr << "[" << xmlReader.getName() << "]" << std::endl;
140 if (!xmlReader.getText().empty()) {
141 clout << indentStr << " " << xmlReader.getText() << std::endl;
142 }
143 for (unsigned int iNode=0; iNode<xmlReader._children.size(); ++iNode) {
144 xmlReader._children[iNode]->_output.print(indent+2,*xmlReader._children[iNode]);
145 }
146}
147
148} // namespace olb
149
150#endif
class for marking output with some text
void loadFile(bool loadOK, std::string fName) const
void parameterReading(std::vector< std::string > parameters, ParameterType &var, bool defaultAvailable, bool exitIfMissing, bool showWarning) const
void printWarning(std::string name, std::string typeName, std::string value, bool verboseOn, bool exitIfMissing) const
print warning if verbose mode is on and exit, if exItMissing is true
void print(int indent, XMLreaderType &xmlReader) const
printing the whole structure of the XMLreader
void readValue(bool warningsOn, std::string name, std::string fName) const
XMLreaderOutput(std::ostream &stream)
Top level namespace for all of OpenLB.