Changeset ef9dff6


Ignore:
Timestamp:
Jul 14, 2014, 8:37:03 PM (11 years ago)
Author:
Frederik Heber <heber@…>
Children:
0ab195
Parents:
ca940b
git-author:
Frederik Heber <heber@…> (06/19/14 16:26:39)
git-committer:
Frederik Heber <heber@…> (07/14/14 20:37:03)
Message:

logger and errorLogger have new setOutputStream().

  • this allows to redirect the logging streams in a program internally, e.g. in a GUI setting.
  • added unit test on this function.
  • TESTFIX: LogUnitTest used loggerStub which made the tests useless.
Location:
src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/CodePatterns/errorlogger.hpp

    rca940b ref9dff6  
    2626public :
    2727  static ostream *nix;
     28  static ostream *out;
     29  static ostream *defaultout;
    2830  static int verbosity;
    2931
     
    3133  static void setVerbosity(int verbosityLevel);
    3234  static int getVerbosity();
     35  static void setOutputStream(ostream *_newout);
    3336
    3437protected:
  • src/CodePatterns/logger.hpp

    rca940b ref9dff6  
    2626public :
    2727  static ostream *nix;
     28  static ostream *out;
     29  static ostream *defaultout;
    2830  static int verbosity;
    2931
     
    3133  static void setVerbosity(int verbosityLevel);
    3234  static int getVerbosity();
     35  static void setOutputStream(ostream *_newout);
    3336
    3437protected:
  • src/Helpers/errorlogger.cpp

    rca940b ref9dff6  
    2828int errorLogger::verbosity = 2;
    2929ostream* errorLogger::nix = NULL;
     30ostream* errorLogger::defaultout = NULL;
     31ostream* errorLogger::out = NULL;
    3032
    3133/**
     
    3739{
    3840  nix = new ofstream("/dev/null");
     41  defaultout = &cerr;
     42  out = defaultout;
    3943};
    4044
     
    4549{
    4650  delete nix;
     51  out = NULL;   // we are not responsible if out got changed
     52  defaultout = NULL; // do not delete cerr
    4753}
    4854
     
    6773  return verbosity;
    6874}
     75
     76/** Sets a new output stream.
     77 *
     78 * \param _newout new output stream, if NULL we set to defaultout
     79 */
     80void errorLogger::setOutputStream(ostream *_newout)
     81{
     82  if(_newout != NULL)
     83    out = _newout;
     84  else
     85    out = defaultout;
     86}
     87
    6988
    7089/**
     
    95114        break;
    96115    }
    97     v.print(cerr);
    98     return cerr;
     116    v.print(*errorLogger::out);
     117    return *errorLogger::out;
    99118  } else
    100119    return *l.nix;
     
    117136        break;
    118137    }
    119     v.print(cerr);
    120     return cerr;
     138    v.print(*errorLogger::out);
     139    return *errorLogger::out;
    121140  } else
    122141    return *l->nix;
  • src/Helpers/logger.cpp

    rca940b ref9dff6  
    2929int logger::verbosity = 2;
    3030ostream* logger::nix = NULL;
     31ostream* logger::defaultout = NULL;
     32ostream* logger::out = NULL;
    3133
    3234/**
     
    3840{
    3941  nix = new ofstream("/dev/null");
     42  defaultout = &cout;
     43  out = defaultout;
    4044};
    4145
     
    4650{
    4751  delete nix;
     52  out = NULL;   // we are not responsible if out got changed
     53  defaultout = NULL; // do not delete cout
    4854}
    4955
     
    6975}
    7076
     77/** Sets a new output stream.
     78 *
     79 * \param _newout new output stream, if NULL we set to defaultout
     80 */
     81void logger::setOutputStream(ostream *_newout)
     82{
     83  if(_newout != NULL)
     84    out = _newout;
     85  else
     86    out = defaultout;
     87}
     88
    7189/**
    7290 * Operator for the Binary(arg) call.
     
    83101  l.nix->clear();
    84102  if (v.DoOutput(verbosityLevel)) {
    85     v.print(cout);
    86     return cout;
     103    v.print(*logger::out);
     104    return *logger::out;
    87105  } else
    88106    return *l.nix;
     
    93111  l->nix->clear();
    94112  if (v.DoOutput(verbosityLevel)) {
    95     v.print(cout);
    96     return cout;
     113    v.print(*logger::out);
     114    return *logger::out;
    97115  } else
    98116    return *l->nix;
  • src/Helpers/unittests/LogUnitTest.cpp

    rca940b ref9dff6  
    3939void LogTest::setUp()
    4040{
    41 };
     41}
    4242
    4343void LogTest::tearDown()
     
    4545  logger::purgeInstance();
    4646  errorLogger::purgeInstance();
    47 };
     47}
    4848
    4949/**
     
    7070  CPPUNIT_ASSERT(!DoLog(4));
    7171  CPPUNIT_ASSERT(!DoeLog(4));
    72 };
     72}
     73
     74/**
     75 * UnitTest for log()
     76 */
     77void LogTest::newOutputTest()
     78{
     79  // check whether redirecting output works
     80  std::stringstream teststream;
     81  {
     82    Log().setOutputStream(&teststream);
     83    logger::getInstance().setVerbosity(2);
     84    Log() << Verbose(0) << std::string("test");
     85//    DoLog(0) && (Log() << Verbose(0) << "test" << endl);
     86    CPPUNIT_ASSERT_EQUAL( std::string("test"), teststream.str() );
     87    Log().setOutputStream(NULL);  // go to default, as stringstream is destroyed
     88  }
     89  // redirect to NULL changes to cout
     90  teststream.str("");
     91  {
     92    DoLog(0) && (Log() << Verbose(0) << "test" << endl);
     93    CPPUNIT_ASSERT_EQUAL( std::string(""), teststream.str() );
     94  }
     95}
  • src/Helpers/unittests/LogUnitTest.hpp

    rca940b ref9dff6  
    2020    CPPUNIT_TEST_SUITE( LogTest) ;
    2121    CPPUNIT_TEST ( logTest );
     22    CPPUNIT_TEST ( newOutputTest );
    2223    CPPUNIT_TEST_SUITE_END();
    2324
     
    2526    void setUp();
    2627    void tearDown();
     28    void newOutputTest();
    2729
    2830    void logTest();
  • src/Helpers/unittests/Makefile.am

    rca940b ref9dff6  
    7777LogUnitTest_SOURCES = UnitTestMain.cpp \
    7878        ../Helpers/unittests/LogUnitTest.cpp \
    79         ../Helpers/unittests/LogUnitTest.hpp \
    80         ../Helpers/unittests/stubs/errorloggerStub.cpp \
    81         ../Helpers/unittests/stubs/loggerStub.cpp
     79        ../Helpers/unittests/LogUnitTest.hpp
    8280nodist_LogUnitTest_SOURCES = \
    8381        $(top_srcdir)/src/CodePatterns/Assert.hpp \
  • src/Helpers/unittests/stubs/loggerStub.cpp

    rca940b ref9dff6  
    2626int logger::verbosity = 2;
    2727ostream* logger::nix = NULL;
     28ostream* logger::defaultout = NULL;
     29ostream* logger::out = NULL;
    2830
    2931/**
     
    3537{
    3638  nix = new stringstream;
     39  defaultout = new stringstream;
     40  out = defaultout;
    3741};
    3842
     
    4347{
    4448  delete nix;
     49  delete defaultout;
     50  out = NULL;
    4551}
    4652
     
    6571}
    6672
     73/** Sets a new output stream.
     74 *
     75 * \param _newout new output stream, if NULL we set to defaultout
     76 */
     77void logger::setOutputStream(ostream *_newout)
     78{
     79  if(_newout != NULL)
     80    out = _newout;
     81  else
     82    out = defaultout;
     83}
     84
    6785/**
    6886 * Operator for the Binary(arg) call.
Note: See TracChangeset for help on using the changeset viewer.