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 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
49 #include "Teuchos_dyn_cast.hpp"
50 #include "Teuchos_Version.hpp"
51 
52 #include "AlgorithmA.hpp"
53 
54 
55 //
56 // Here is a simple driver function that I call over and over to show
57 // different features of FancyOStream
58 //
59 
61 {
62 
63  // Here I just create the algorithm object that derives from VerboseObject.
64  // By default, this object will print to *Verbose::getDefaultOStream()
65  AlgorithmA algoA;
66  if (algoParams)
67  algoA.setParameterList(Teuchos::rcp(algoParams,false));
68  // Note that here I could change the stream just this object prints to
69  // by calling algoA.setOStream(...).
70 
71  // Now I call the algorithm which will print to its default output stream
72  algoA.doAlgorithm();
73 
74  *algoA.getOStream() << std::endl;
75 
76  TEUCHOS_ASSERT(algoA.getParameterList().getRawPtr() == algoParams);
77 
78 }
79 
80 //
81 // Test that static initialization of VerboseObjectBase and VerboseObject works!
82 //
83 
85 public:
87  {
88  // Get the verbosity level for AlgorithmA
91  // Print to the default default OStream to make sure that the initialization
92  // trick worked!
94  << "\n***\n*** Printing to default OStream before main() even starts!\n***\n\n"
95  << std::flush;
96  }
97 };
98 
100 
101 //
102 // Main driver program
103 //
104 
105 int main(int argc, char* argv[])
106 {
107 
108  using Teuchos::RCP;
109  using Teuchos::rcp;
110  using Teuchos::FancyOStream;
112  using Teuchos::OSTab;
113  using Teuchos::dyn_cast;
115 
116  bool success = true;
117 
118  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
119  const int numProcs = Teuchos::GlobalMPISession::getNProc();
120 
121  try {
122 
123  // Get some commandline options
124  CommandLineProcessor clp;
125  clp.throwExceptions(false);
126  clp.addOutputSetupOptions(true);
127  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
128  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
129 
130  // Here I am just grabbing the default output stream
131  RCP<FancyOStream>
132  out = VerboseObjectBase::getDefaultOStream();
133  // Note that the VerboseObject manages FancyOStream objects and not just
134  // std::ostream objects. This is important to the design and very
135  // resonable I think.
136 
137  *out << std::endl << Teuchos::Teuchos_Version() << std::endl << std::endl;
138 
139  //
140  // Now I call doAlgorithmStuff() a bunch of times with different setups to
141  // show the different kinds of line prefix options
142  //
143 
144  *out << "\n***\n*** Testing VerboseObject base class use\n***\n";
145 
146  *out << "\n*** Algorithm output with default formatting\n\n";
148 
149  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
150  *out << "\n*** Algorithm output with no front matter\n\n";
151  out->setShowAllFrontMatter(false);
153 
154  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
155  *out << "\n*** Algorithm output with processor ranks\n\n";
156  out->setShowAllFrontMatter(false).setShowProcRank(true);
158 
159  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
160  *out << "\n*** Algorithm output with line prefix names\n\n";
161  out->setShowAllFrontMatter(false).setShowLinePrefix(true);
163 
164  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
165  *out << "\n*** Algorithm output with tab counts\n\n";
166  out->setShowAllFrontMatter(false).setShowTabCount(true);
168 
169  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
170  *out << "\n*** Algorithm output with line prefix names and tab counts\n\n";
171  out->setShowAllFrontMatter(false).setShowLinePrefix(true).setShowTabCount(true);
173 
174  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
175  *out << "\n*** Algorithm output with processor ranks and line prefix names\n\n";
176  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true);
178 
179  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
180  *out << "\n*** Algorithm output with processor ranks and tab counts\n\n";
181  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowTabCount(true);
183 
184  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
185  *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts\n\n";
186  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
188 
189  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
190  *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts but no output for AlgorithmA\n\n";
192  out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
195 
196  *out << "\n*** Running the algorithm by setting parameters in the parameter list ...\n";
197 
198  Teuchos::ParameterList algoParams("AlgorithmA");
199 
200  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
201  *out << "\n*** Set AlgorithmA verbosity level to extreme through a parameter list\n\n";
202  algoParams.sublist("VerboseObject").set("Verbosity Level","extreme");
203  algoParams.set("Algo Type","Harry");
204  algoParams.set("Algo Tol",0.3);
205  doAlgorithmStuff(&algoParams);
206 
207  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
208  *out << "\n*** Set AlgorithmA verbosity level to medium and the output file \"AlgorithmA.out\" through a parameter list\n\n";
209  algoParams.sublist("VerboseObject").set("Verbosity Level","medium");
210  algoParams.sublist("VerboseObject").set("Output File","AlgorithmA.out");
211  algoParams.set("Algo Type","John");
212  algoParams.set("Algo Tol",10);
213  doAlgorithmStuff(&algoParams);
214 
215  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
216  *out << "\n*** Set AlgorithmA verbosity level to low and the output back to default through a parameter list\n\n";
217  algoParams.sublist("VerboseObject").set("Verbosity Level","low");
218  algoParams.sublist("VerboseObject").set("Output File","none");
219  algoParams.set("Algo Tol","20");
220  doAlgorithmStuff(&algoParams);
221 
222  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
223  *out << "\n***\n*** Do some more simple tests to make sure things work correctly\n***\n\n";
224 
225  //
226  // Now I do some other simple tests just to see that FancyOStream is working
227  // correctly
228  //
229 
230  out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1).setShowTabCount(true);
231  out->setProcRankAndSize(mpiSession.getRank(),mpiSession.getNProc());
232 
233  *out << "\n***\n*** Testing basic FancyOStream and OSTab classes\n***\n\n";
234 
235  *out << "\nThis is very good output\nand I like it a lot!\n";
236  *out << "";
237  *out << "\n";
238  *out << "This should";
239  *out << " all be";
240  *out << " printed on";
241  *out << " the same";
242  *out << " line two lines below the above output!\n";
243  RCP<FancyOStream>
244  out2 = rcp(new FancyOStream(rcp(new std::ostringstream)," "));
245  {
246  OSTab tab1(out);
247  *out << "This should be indented one tab!\n";
248  {
249  OSTab tab2(out);
250  *out << "This should be indented two tabs!\n";
251  *out2 << "This should be indented zero tabs from out2!\n";
252  {
253  OSTab tab3(out2);
254  *out << "This should be indented two tabs!\n";
255  *out2 << "This should be indented one tab from out2!\n";
256  }
257  }
258  *out << "This should be indented one tab!\n";
259  }
260  *out << "This should be indented zero tabs!\n";
261 
262  *out << std::endl; // This required overflow() to be overridden!
263 
264  *out << "\n***\n*** Now outputting the latent output that was sent to out2\n***\n\n"
265  << dyn_cast<std::ostringstream>(*out2->getOStream()).str();
266 
267  if(success)
268  *out << "\nEnd Result: TEST PASSED" << std::endl;
269 
270  }
271  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
272 
273  return ( success ? 0 : 1 );
274 
275 }
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:116
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 const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
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:164
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:199
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.