#include "Teuchos_VerboseObject.hpp"
#include "Teuchos_StandardCatchMacros.hpp"
#include "Teuchos_GlobalMPISession.hpp"
#include "Teuchos_CommandLineProcessor.hpp"
#include "Teuchos_dyn_cast.hpp"
#include "Teuchos_Version.hpp"
void someDumbFunction( std::ostream &out, const std::string &indentSpacer )
{
out << "\nEntering someDumbFunction(...)\n";
if(1) {
out << std::endl << indentSpacer << "I am \"dumb\" code that knows nothing of FancyOStream and does indenting manually! ...\n";
}
out << "\nLeaving someDumbFunction(...)\n";
}
void someLessDumbFunction( std::ostream &out_arg )
{
using Teuchos::OSTab;
Teuchos::RefCountPtr<Teuchos::FancyOStream>
out = Teuchos::getFancyOStream(Teuchos::rcp(&out_arg,false));
OSTab tab(out,1,"LDUMBALGO");
*out << "\nEntering someLessDumbFunction(...)\n";
if(1) {
OSTab tab(out);
*out << std::endl << "I am less \"dumb\" code that knows about FancyOStream but my interface does not support it directly! ...\n";
}
*out << "\nLeaving someLessDumbFunction(...)\n";
}
class AlgorithmA : public Teuchos::VerboseObject<AlgorithmA> {
public:
AlgorithmA()
{
this->setLinePrefix("ALGO_A");
}
void doAlgorithm()
{
using Teuchos::OSTab;
Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
Teuchos::RefCountPtr<Teuchos::FancyOStream> out = this->getOStream();
OSTab tab = this->getOSTab();
if(out.get() && verbLevel!=Teuchos::VERB_NONE)
*out << "\nEntering AlgorithmA::doAlgorithm()\n";
if(1) {
TEUCHOS_OSTAB;
if(out.get() && verbLevel!=Teuchos::VERB_NONE)
*out << "\nI am \"smart\" code that knows about FancyOStream and OSTab ...\n";
if(1) {
OSTab tab = this->getOSTab(OSTab::DISABLE_TABBING);
if(out.get() && verbLevel!=Teuchos::VERB_NONE)
*out << "\n***\n*** Warning, I am doing something very dangerous so watch out!!!\n***\n";
}
if(out.get() && verbLevel!=Teuchos::VERB_NONE)
*out << "\nHere I am doing some more stuff and printing with indenting turned back on!\n";
if(1) {
OSTab tab = this->getOSTab(1,"DUMBALGO");
someDumbFunction(*out,out->getTabIndentStr());
}
someLessDumbFunction(*out);
}
if(out.get() && verbLevel!=Teuchos::VERB_NONE)
*out << "\nLeaving AlgorithmA::doAlgorithm()\n";
}
};
void doAlgorithmStuff()
{
AlgorithmA algoA;
algoA.doAlgorithm();
*algoA.getOStream() << std::endl;
}
class TestVerboseObjectBaseInitialization {
public:
TestVerboseObjectBaseInitialization()
{
Teuchos::EVerbosityLevel verbLevel = Teuchos::VerboseObject<AlgorithmA>::getDefaultVerbLevel();
TEST_FOR_EXCEPT_PRINT(verbLevel!=Teuchos::VERB_DEFAULT,&std::cerr);
*Teuchos::VerboseObjectBase::getDefaultOStream()
<< "\n***\n*** Printing to default OStream before main() even starts!\n***\n\n"
<< std::flush;
}
};
static TestVerboseObjectBaseInitialization testVerboseObjectBaseInitialization;
int main(int argc, char* argv[])
{
using Teuchos::RefCountPtr;
using Teuchos::rcp;
using Teuchos::FancyOStream;
using Teuchos::VerboseObjectBase;
using Teuchos::OSTab;
using Teuchos::dyn_cast;
using Teuchos::CommandLineProcessor;
bool success = true;
Teuchos::GlobalMPISession mpiSession(&argc,&argv);
const int numProcs = Teuchos::GlobalMPISession::getNProc();
try {
CommandLineProcessor clp;
clp.throwExceptions(false);
clp.addOutputSetupOptions(true);
CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
RefCountPtr<FancyOStream>
out = VerboseObjectBase::getDefaultOStream();
*out << std::endl << Teuchos::Teuchos_Version() << std::endl << std::endl;
*out << "\n***\n*** Testing VerboseObject base class use\n***\n";
*out << "\n*** Algorithm output with default formatting\n\n";
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with no front matter\n\n";
out->setShowAllFrontMatter(false);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with processor ranks\n\n";
out->setShowAllFrontMatter(false).setShowProcRank(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with line prefix names\n\n";
out->setShowAllFrontMatter(false).setShowLinePrefix(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with tab counts\n\n";
out->setShowAllFrontMatter(false).setShowTabCount(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with line prefix names and tab counts\n\n";
out->setShowAllFrontMatter(false).setShowLinePrefix(true).setShowTabCount(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with processor ranks and line prefix names\n\n";
out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with processor ranks and tab counts\n\n";
out->setShowAllFrontMatter(false).setShowProcRank(true).setShowTabCount(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts\n\n";
out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
doAlgorithmStuff();
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts but no output for AlgorithmA\n\n";
Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_NONE);
out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
doAlgorithmStuff();
Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_DEFAULT);
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
*out << "\n***\n*** Do some more simple tests to make sure things work correctly\n***\n\n";
out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1).setShowTabCount(true);
out->setProcRankAndSize(mpiSession.getRank(),mpiSession.getNProc());
*out << "\n***\n*** Testing basic FancyOStream and OSTab classes\n***\n\n";
*out << "\nThis is very good output\nand I like it a lot!\n";
*out << "";
*out << "\n";
*out << "This should";
*out << " all be";
*out << " printed on";
*out << " the same";
*out << " line two lines below the above output!\n";
RefCountPtr<FancyOStream>
out2 = rcp(new FancyOStream(rcp(new std::ostringstream)," "));
if(1) {
OSTab tab(out);
*out << "This should be indented one tab!\n";
if(1) {
OSTab tab(out);
*out << "This should be indented two tabs!\n";
*out2 << "This should be indented zero tabs from out2!\n";
if(1) {
OSTab tab(out2);
*out << "This should be indented two tabs!\n";
*out2 << "This should be indented one tab from out2!\n";
}
}
*out << "This should be indented one tab!\n";
}
*out << "This should be indented zero tabs!\n";
*out << std::endl;
*out << "\n***\n*** Now outputting the latent output that was sent to out2\n***\n\n"
<< dyn_cast<std::ostringstream>(*out2->getOStream()).str();
if(success)
*out << "\nEnd Result: TEST PASSED" << std::endl;
}
TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
return ( success ? 0 : 1 );
}