Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FancyOutputting_test.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #include "Teuchos_dyn_cast.hpp"
16 #include "Teuchos_Version.hpp"
17 
18 #include "AlgorithmA.hpp"
19 
20 
21 //
22 // Here is a simple driver function that I call over and over to show
23 // different features of FancyOStream
24 //
25 
27 {
28 
29  // Here I just create the algorithm object that derives from VerboseObject.
30  // By default, this object will print to *Verbose::getDefaultOStream()
31  AlgorithmA algoA;
32  if (algoParams)
33  algoA.setParameterList(Teuchos::rcp(algoParams,false));
34  // Note that here I could change the stream just this object prints to
35  // by calling algoA.setOStream(...).
36 
37  // Now I call the algorithm which will print to its default output stream
38  algoA.doAlgorithm();
39 
40  *algoA.getOStream() << std::endl;
41 
42  TEUCHOS_ASSERT(algoA.getParameterList().getRawPtr() == algoParams);
43 
44 }
45 
46 //
47 // Test that static initialization of VerboseObjectBase and VerboseObject works!
48 //
49 
51 public:
53  {
54  // Get the verbosity level for AlgorithmA
57  // Print to the default default OStream to make sure that the initialization
58  // trick worked!
60  << "\n***\n*** Printing to default OStream before main() even starts!\n***\n\n"
61  << std::flush;
62  }
63 };
64 
66 
67 //
68 // Main driver program
69 //
70 
71 int main(int argc, char* argv[])
72 {
73 
74  using Teuchos::RCP;
75  using Teuchos::rcp;
78  using Teuchos::OSTab;
79  using Teuchos::dyn_cast;
81 
82  bool success = true;
83 
84  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
85  const int numProcs = Teuchos::GlobalMPISession::getNProc();
86 
87  try {
88 
89  // Get some commandline options
90  CommandLineProcessor clp;
91  clp.throwExceptions(false);
92  clp.addOutputSetupOptions(true);
93  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
94  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
95 
96  // Here I am just grabbing the default output stream
97  RCP<FancyOStream>
98  out = VerboseObjectBase::getDefaultOStream();
99  // Note that the VerboseObject manages FancyOStream objects and not just
100  // std::ostream objects. This is important to the design and very
101  // resonable I think.
102 
103  *out << std::endl << Teuchos::Teuchos_Version() << std::endl << std::endl;
104 
105  //
106  // Now I call doAlgorithmStuff() a bunch of times with different setups to
107  // show the different kinds of line prefix options
108  //
109 
110  *out << "\n***\n*** Testing VerboseObject base class use\n***\n";
111 
112  *out << "\n*** Algorithm output with default formatting\n\n";
114 
115  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
116  *out << "\n*** Algorithm output with no front matter\n\n";
117  out->setShowAllFrontMatter(false);
119 
120  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
121  *out << "\n*** Algorithm output with processor ranks\n\n";
122  out->setShowAllFrontMatter(false).setShowProcRank(true);
124 
125  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
126  *out << "\n*** Algorithm output with line prefix names\n\n";
127  out->setShowAllFrontMatter(false).setShowLinePrefix(true);
129 
130  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
131  *out << "\n*** Algorithm output with tab counts\n\n";
132  out->setShowAllFrontMatter(false).setShowTabCount(true);
134 
135  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
136  *out << "\n*** Algorithm output with line prefix names and tab counts\n\n";
137  out->setShowAllFrontMatter(false).setShowLinePrefix(true).setShowTabCount(true);
139 
140  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
141  *out << "\n*** Algorithm output with processor ranks and line prefix names\n\n";
142  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true);
144 
145  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
146  *out << "\n*** Algorithm output with processor ranks and tab counts\n\n";
147  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowTabCount(true);
149 
150  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
151  *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts\n\n";
152  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
154 
155  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
156  *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts but no output for AlgorithmA\n\n";
158  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
161 
162  *out << "\n*** Running the algorithm by setting parameters in the parameter list ...\n";
163 
164  Teuchos::ParameterList algoParams("AlgorithmA");
165 
166  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
167  *out << "\n*** Set AlgorithmA verbosity level to extreme through a parameter list\n\n";
168  algoParams.sublist("VerboseObject").set("Verbosity Level","extreme");
169  algoParams.set("Algo Type","Harry");
170  algoParams.set("Algo Tol",0.3);
171  doAlgorithmStuff(&algoParams);
172 
173  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
174  *out << "\n*** Set AlgorithmA verbosity level to medium and the output file \"AlgorithmA.out\" through a parameter list\n\n";
175  algoParams.sublist("VerboseObject").set("Verbosity Level","medium");
176  algoParams.sublist("VerboseObject").set("Output File","AlgorithmA.out");
177  algoParams.set("Algo Type","John");
178  algoParams.set("Algo Tol",10);
179  doAlgorithmStuff(&algoParams);
180 
181  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
182  *out << "\n*** Set AlgorithmA verbosity level to low and the output back to default through a parameter list\n\n";
183  algoParams.sublist("VerboseObject").set("Verbosity Level","low");
184  algoParams.sublist("VerboseObject").set("Output File","none");
185  algoParams.set("Algo Tol","20");
186  doAlgorithmStuff(&algoParams);
187 
188  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
189  *out << "\n***\n*** Do some more simple tests to make sure things work correctly\n***\n\n";
190 
191  //
192  // Now I do some other simple tests just to see that FancyOStream is working
193  // correctly
194  //
195 
196  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1).setShowTabCount(true);
197  out->setProcRankAndSize(mpiSession.getRank(),mpiSession.getNProc());
198 
199  *out << "\n***\n*** Testing basic FancyOStream and OSTab classes\n***\n\n";
200 
201  *out << "\nThis is very good output\nand I like it a lot!\n";
202  *out << "";
203  *out << "\n";
204  *out << "This should";
205  *out << " all be";
206  *out << " printed on";
207  *out << " the same";
208  *out << " line two lines below the above output!\n";
209  RCP<FancyOStream>
210  out2 = rcp(new FancyOStream(rcp(new std::ostringstream)," "));
211  {
212  OSTab tab1(out);
213  *out << "This should be indented one tab!\n";
214  {
215  OSTab tab2(out);
216  *out << "This should be indented two tabs!\n";
217  *out2 << "This should be indented zero tabs from out2!\n";
218  {
219  OSTab tab3(out2);
220  *out << "This should be indented two tabs!\n";
221  *out2 << "This should be indented one tab from out2!\n";
222  }
223  }
224  *out << "This should be indented one tab!\n";
225  }
226  *out << "This should be indented zero tabs!\n";
227 
228  *out << std::endl; // This required overflow() to be overridden!
229 
230  *out << "\n***\n*** Now outputting the latent output that was sent to out2\n***\n\n"
231  << dyn_cast<std::ostringstream>(*out2->getOStream()).str();
232 
233  if(success)
234  *out << "\nEnd Result: TEST PASSED" << std::endl;
235 
236  }
237  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
238 
239  return ( success ? 0 : 1 );
240 
241 }
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
EVerbosityLevel
Verbosity level.
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const &paramList)
Set parameters from a parameter list and return with default values.
Definition: AlgorithmA.cpp:82
static int getNProc()
The number of processes in MPI_COMM_WORLD.
static void setDefaultVerbLevel(const EVerbosityLevel defaultVerbLevel)
Set the default verbosity level.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
Initialize, finalize, and query the global MPI session.
basic_OSTab< char > OSTab
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
static EVerbosityLevel getDefaultVerbLevel()
Get the default verbosity level.
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast&lt;T&amp;&gt; by throwing a better documented er...
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
Get const version of the parameter list that was set using setParameterList().
Definition: AlgorithmA.cpp:130
Generate no output.
Non-templated base class for objects that can print their activities to a stream. ...
std::string Teuchos_Version()
A list of parameters of arbitrary type.
static TestVerboseObjectBaseInitialization testVerboseObjectBaseInitialization
int main(int argc, char *argv[])
basic_FancyOStream< char > FancyOStream
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
#define TEUCHOS_TEST_FOR_EXCEPT_PRINT(throw_exception_test, out_ptr)
This macro is the same as TEUCHOS_TEST_FOR_EXCEPT() except that the exception will be caught...
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
void doAlgorithmStuff(Teuchos::ParameterList *algoParams=0)
void doAlgorithm()
Definition: AlgorithmA.cpp:165
virtual RCP< FancyOStream > getOStream() const
Return the output stream to be used for out for *this object.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Generate output as defined by the object.