Teuchos Package Browser (Single Doxygen Collection) Version of the Day
FancyOutputting_test.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_VerboseObject.hpp"
00032 #include "Teuchos_StandardCatchMacros.hpp"
00033 #include "Teuchos_GlobalMPISession.hpp"
00034 #include "Teuchos_CommandLineProcessor.hpp"
00035 #include "Teuchos_StandardParameterEntryValidators.hpp"
00036 #include "Teuchos_dyn_cast.hpp"
00037 #include "Teuchos_Version.hpp"
00038 
00039 #include "AlgorithmA.hpp"
00040 
00041 
00042 //
00043 // Here is a simple driver function that I call over and over to show
00044 // different features of FancyOStream
00045 //
00046 
00047 void doAlgorithmStuff( Teuchos::ParameterList *algoParams = 0 )
00048 {
00049 
00050   // Here I just create the algorithm object that derives from VerboseObject.
00051   // By default, this object will print to *Verbose::getDefaultOStream()
00052   AlgorithmA algoA;
00053   if (algoParams)
00054     algoA.setParameterList(Teuchos::rcp(algoParams,false));
00055   // Note that here I could change the stream just this object prints to
00056   // by calling algoA.setOStream(...).
00057   
00058   // Now I call the algorithm which will print to its default output stream
00059   algoA.doAlgorithm();
00060   
00061   *algoA.getOStream() << std::endl;
00062 
00063   TEUCHOS_ASSERT(algoA.getParameterList().getRawPtr() == algoParams);
00064 
00065 }
00066 
00067 //
00068 // Test that static initailziation of VerboseObjectBase and VerboseObject works!
00069 //
00070 
00071 class TestVerboseObjectBaseInitialization {
00072 public:
00073   TestVerboseObjectBaseInitialization()
00074     {
00075       // Get the verbosity level for AlgorithmA
00076       Teuchos::EVerbosityLevel verbLevel = Teuchos::VerboseObject<AlgorithmA>::getDefaultVerbLevel();
00077       TEST_FOR_EXCEPT_PRINT(verbLevel!=Teuchos::VERB_DEFAULT,&std::cerr);
00078       // Print to the default default OStream to make sure that the initialization
00079       // trick worked!
00080       *Teuchos::VerboseObjectBase::getDefaultOStream()
00081         << "\n***\n*** Printing to default OStream before main() even starts!\n***\n\n"
00082         << std::flush;
00083     }
00084 };
00085 
00086 static TestVerboseObjectBaseInitialization testVerboseObjectBaseInitialization;
00087 
00088 //
00089 // Main driver program
00090 //
00091 
00092 int main(int argc, char* argv[])
00093 {
00094 
00095   using Teuchos::RCP;
00096   using Teuchos::rcp;
00097   using Teuchos::FancyOStream;
00098   using Teuchos::VerboseObjectBase;
00099   using Teuchos::OSTab;
00100   using Teuchos::dyn_cast;
00101   using Teuchos::CommandLineProcessor;
00102 
00103   bool success = true;
00104 
00105   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00106   const int numProcs = Teuchos::GlobalMPISession::getNProc();
00107 
00108   try {
00109 
00110     // Get some commandline options
00111     CommandLineProcessor  clp;
00112     clp.throwExceptions(false);
00113     clp.addOutputSetupOptions(true);
00114     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00115     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00116 
00117     // Here I am just grabbing the default output stream
00118     RCP<FancyOStream>
00119       out = VerboseObjectBase::getDefaultOStream();
00120     // Note that the VerboseObject manages FancyOStream objects and not just
00121     // std::ostream objects.  This is important to the design and very
00122     // resonable I think.
00123 
00124     *out << std::endl << Teuchos::Teuchos_Version() << std::endl << std::endl;
00125 
00126     //
00127     // Now I call doAlgorithmStuff() a bunch of times with different setups to
00128     // show the different kinds of line prefix options
00129     //
00130   
00131     *out << "\n***\n*** Testing VerboseObject base class use\n***\n";
00132   
00133     *out << "\n*** Algorithm output with default formatting\n\n";
00134     doAlgorithmStuff();
00135   
00136     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00137     *out << "\n*** Algorithm output with no front matter\n\n";
00138     out->setShowAllFrontMatter(false);
00139     doAlgorithmStuff();
00140   
00141     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00142     *out << "\n*** Algorithm output with processor ranks\n\n";
00143     out->setShowAllFrontMatter(false).setShowProcRank(true);
00144     doAlgorithmStuff();
00145   
00146     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00147     *out << "\n*** Algorithm output with line prefix names\n\n";
00148     out->setShowAllFrontMatter(false).setShowLinePrefix(true);
00149     doAlgorithmStuff();
00150   
00151     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00152     *out << "\n*** Algorithm output with tab counts\n\n";
00153     out->setShowAllFrontMatter(false).setShowTabCount(true);
00154     doAlgorithmStuff();
00155   
00156     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00157     *out << "\n*** Algorithm output with line prefix names and tab counts\n\n";
00158     out->setShowAllFrontMatter(false).setShowLinePrefix(true).setShowTabCount(true);
00159     doAlgorithmStuff();
00160   
00161     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00162     *out << "\n*** Algorithm output with processor ranks and line prefix names\n\n";
00163     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true);
00164     doAlgorithmStuff();
00165   
00166     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00167     *out << "\n*** Algorithm output with processor ranks and tab counts\n\n";
00168     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowTabCount(true);
00169     doAlgorithmStuff();
00170   
00171     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00172     *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts\n\n";
00173     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
00174     doAlgorithmStuff();
00175   
00176     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00177     *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts but no output for AlgorithmA\n\n";
00178     Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_NONE);
00179     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
00180     doAlgorithmStuff();
00181     Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_DEFAULT);
00182 
00183     *out << "\n*** Running the algorithm by setting parameters in the parameter list ...\n";
00184 
00185     Teuchos::ParameterList algoParams("AlgorithmA");
00186 
00187     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00188     *out << "\n*** Set AlgorithmA verbosity level to extreme through a parameter list\n\n";
00189     algoParams.sublist("VerboseObject").set("Verbosity Level","extreme");
00190     algoParams.set("Algo Type","Harry");
00191     algoParams.set("Algo Tol",0.3);
00192     doAlgorithmStuff(&algoParams);
00193 
00194     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00195     *out << "\n*** Set AlgorithmA verbosity level to medium and the output file \"AlgorithmA.out\" through a parameter list\n\n";
00196     algoParams.sublist("VerboseObject").set("Verbosity Level","medium");
00197     algoParams.sublist("VerboseObject").set("Output File","AlgorithmA.out");
00198     algoParams.set("Algo Type","John");
00199     algoParams.set("Algo Tol",10);
00200     doAlgorithmStuff(&algoParams);
00201 
00202     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00203     *out << "\n*** Set AlgorithmA verbosity level to low and the output back to default through a parameter list\n\n";
00204     algoParams.sublist("VerboseObject").set("Verbosity Level","low");
00205     algoParams.sublist("VerboseObject").set("Output File","none");
00206     algoParams.set("Algo Tol","20");
00207     doAlgorithmStuff(&algoParams);
00208 
00209     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00210     *out << "\n***\n*** Do some more simple tests to make sure things work correctly\n***\n\n";
00211 
00212     //
00213     // Now I do some other simple tests just to see that FancyOStream is working
00214     // correctly
00215     //
00216 
00217     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1).setShowTabCount(true);
00218     out->setProcRankAndSize(mpiSession.getRank(),mpiSession.getNProc());
00219     
00220     *out << "\n***\n*** Testing basic FancyOStream and OSTab classes\n***\n\n";
00221     
00222     *out << "\nThis is very good output\nand I like it a lot!\n";
00223     *out << "";
00224     *out << "\n";
00225     *out << "This should";
00226     *out << " all be";
00227     *out << " printed on";
00228     *out << " the same";
00229     *out << " line two lines below the above output!\n";
00230     RCP<FancyOStream>
00231       out2 = rcp(new FancyOStream(rcp(new std::ostringstream),"  "));
00232     {
00233       OSTab tab1(out);
00234       *out << "This should be indented one tab!\n";
00235       {
00236         OSTab tab2(out);
00237         *out << "This should be indented two tabs!\n";
00238         *out2 << "This should be indented zero tabs from out2!\n";
00239         {
00240           OSTab tab3(out2);
00241           *out << "This should be indented two tabs!\n";
00242           *out2 << "This should be indented one tab from out2!\n";
00243         }
00244       }
00245       *out << "This should be indented one tab!\n";
00246     }
00247     *out << "This should be indented zero tabs!\n";
00248     
00249     *out << std::endl; // This required overflow() to be overridden!
00250 
00251     *out << "\n***\n*** Now outputting the latent output that was sent to out2\n***\n\n"
00252          << dyn_cast<std::ostringstream>(*out2->getOStream()).str();
00253 
00254     if(success)
00255       *out << "\nEnd Result: TEST PASSED" << std::endl;
00256     
00257   }
00258   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00259     
00260   return ( success ? 0 : 1 );
00261   
00262 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines