OpenLB 1.7
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#include <string>
24#include <vector>
25#include <iostream>
26
28
29namespace olb {
30
31
33public:
34 // printing the output of the xmlReader regarding readOrWarn
35 template<typename ParameterType>
36 void parameterReading(std::vector<std::string> parameters, ParameterType& var,
37 bool defaultAvailable, bool exitIfMissing, bool showWarning) const;
38 void loadFile(bool loadOK, std::string fName) const;
39 void readValue(bool warningsOn, std::string name, std::string fName) const;
40 void printWarning(std::string name, std::string typeName, std::string value,
41 bool verboseOn, bool exitIfMissing) const;
42 template<typename XMLreaderType>
43 void print(int indent, XMLreaderType& xmlReader) const;
44
45
46 XMLreaderOutput() : clout(std::cerr, "xmlReaderOutput"){}
47 XMLreaderOutput(std::ostream& stream) : clout(stream, "xmlReaderOutput"){}
48 XMLreaderOutput(OutputChannel outputChannel);
49
50protected:
52 bool _inDebugMode = false;
53};
54
55XMLreaderOutput::XMLreaderOutput(OutputChannel outputChannel) : clout(std::cerr, "xmlReaderOutput"){
56 if (outputChannel == OutputChannel::TERMINAL){
57 clout = OstreamManager(std::cout, "xmlReaderOutput");
58 }
59 else if (outputChannel == OutputChannel::ERRCHANNEL){
60 clout = OstreamManager(std::cerr, "xmlReaderOutput");
61 } else{
62 clout = OstreamManager(std::cerr, "xmlReaderOutput");
63 }
64#ifdef OLB_DEBUG
65 _inDebugMode = true;
66#endif
67}
68
69template<typename ParameterType>
70void XMLreaderOutput::parameterReading(std::vector<std::string> parameters,
71 ParameterType& var,
72 bool defaultAvailable,
73 bool exitIfMissing,
74 bool showWarning) const
75{
76 if (showWarning || _inDebugMode) {
77 clout << "Warning: Cannot read parameter from XML File: ";
78 std::for_each(parameters.begin(), parameters.end(), [this](const std::string name_parameter)
79 { clout << "<" << name_parameter << ">"; });
80 clout << std::endl;
81
82 if ( exitIfMissing ) {
83 clout << "Error: This program cannot continue without ";
84 std::for_each(parameters.begin(), parameters.end(), [this](const std::string name_parameter)
85 { clout << "<" << name_parameter << ">"; });
86 clout << ". Optimization aborted." << std::endl;
87 exit(1);
88 }
89 if (defaultAvailable) {
90 clout << "\t Setting default value: " << parameters.back() << " = "<< var << std::endl;
91 }
92 else {
93 clout << "\t Setting arbitrarily: " << parameters.back() << " = " << var << std::endl;
94 }
95 }
96}
97
98
99void XMLreaderOutput::loadFile(bool loadOK, std::string fName) const
100{
101 if (!loadOK) {
102 clout << std::string("Problem processing input XML file ") << fName << std::endl;
103 }
104}
105
106void XMLreaderOutput::readValue(bool warningsOn, std::string name, std::string fName) const
107{
108 if ( warningsOn ) {
109 clout << "Warning: cannot read value from node \"" << name << "\"" << ", \"" << fName <<"\"" << std::endl;
110 }
111}
112
114void XMLreaderOutput::printWarning(std::string name, std::string typeName,
115 std::string value, bool verboseOn, bool exitIfMissing) const
116{
117
118 if ( verboseOn ) {
119 clout << "Warning: Cannot read " << typeName << " value from XML element " << name << "." << std::endl;
120 if ( ! value.empty() ) {
121 clout << " Setting default value = " << value << std::endl;
122 }
123 }
124 if ( exitIfMissing ) {
125 clout << "Error: This program cannot continue without \"" << name << "\". Optimization aborted." << std::endl;
126 exit(1);
127 }
128}
129
131template<typename XMLreaderType>
132void XMLreaderOutput::print(int indent, XMLreaderType& xmlReader) const{
133 std::string indentStr(indent, ' ');
134 clout << indentStr << "[" << xmlReader.getName() << "]" << std::endl;
135 if (!xmlReader.getText().empty()) {
136 clout << indentStr << " " << xmlReader.getText() << std::endl;
137 }
138 for (unsigned int iNode=0; iNode<xmlReader._children.size(); ++iNode) {
139 xmlReader._children[iNode]->_output.print(indent+2,*xmlReader._children[iNode]);
140 }
141}
142
143
144
145
146} // namespace olb
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.
OutputChannel