Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9537


Ignore:
Timestamp:
Mar 4, 2013, 11:23:57 PM (12 years ago)
Author:
landauf
Message:

ConsoleWriter now accepts and writes to any std::ostream, not just std::cout. Makes it easier to test.

Location:
code/branches/testing
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/testing/src/libraries/util/output/ConsoleWriter.cc

    r9536 r9537  
    3434#include "ConsoleWriter.h"
    3535
    36 #include <iostream>
    3736
    3837#include "OutputManager.h"
     
    4847        After creation, the instance is enabled.
    4948    */
    50     ConsoleWriter::ConsoleWriter() : BaseWriter("Console")
     49    ConsoleWriter::ConsoleWriter(std::ostream& outputStream) : BaseWriter("Console"), outputStream_(outputStream)
    5150    {
    5251#ifdef ORXONOX_RELEASE
     
    7069    void ConsoleWriter::printLine(const std::string& line, OutputLevel)
    7170    {
    72         std::cout << line << std::endl;
     71        this->outputStream_ << line << std::endl;
    7372    }
    7473
  • code/branches/testing/src/libraries/util/output/ConsoleWriter.h

    r9536 r9537  
    3737
    3838#include "util/UtilPrereqs.h"
     39
     40#include <ostream>
     41
    3942#include "BaseWriter.h"
    4043
     
    5053    {
    5154        public:
    52             ConsoleWriter();
     55            ConsoleWriter(std::ostream& outputStream);
    5356            ConsoleWriter(const ConsoleWriter&);
    5457            virtual ~ConsoleWriter();
     
    5760            void disable();
    5861
     62            inline const std::ostream& getOutputStream() const
     63                { return this->outputStream_; }
     64
    5965        protected:
    6066            virtual void printLine(const std::string& line, OutputLevel level);
    6167
    6268        private:
    63             bool bEnabled_; ///< If false, the instance will not write output to the console.
     69            std::ostream& outputStream_; ///< The ostream to which the console writer writes its output
     70            bool bEnabled_;              ///< If false, the instance will not write output to the console.
    6471    };
    6572}
  • code/branches/testing/src/libraries/util/output/OutputManager.cc

    r9536 r9537  
    3333
    3434#include "OutputManager.h"
     35
     36#include <iostream>
    3537
    3638#include "MemoryWriter.h"
     
    111113    ConsoleWriter& OutputManager::getConsoleWriter()
    112114    {
    113         static ConsoleWriter instance;
     115        static ConsoleWriter instance(std::cout);
    114116        return instance;
    115117    }
  • code/branches/testing/test/util/output/ConsoleWriterTest.cc

    r9535 r9537  
    88    TEST(ConsoleWriterTest, Disable)
    99    {
     10        std::ostream stream(NULL);
    1011        EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size());
    11         ConsoleWriter writer;
     12        ConsoleWriter writer(stream);
    1213        EXPECT_EQ(1U, OutputManager::getInstance().getListeners().size());
    1314        writer.disable();
     
    1718    TEST(ConsoleWriterTest, Enable)
    1819    {
    19         ConsoleWriter writer;
     20        std::ostream stream(NULL);
     21        ConsoleWriter writer(stream);
    2022        writer.disable();
    2123        EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size());
     
    2325        EXPECT_EQ(1U, OutputManager::getInstance().getListeners().size());
    2426    }
     27
     28    TEST(ConsoleWriterTest, WritesNoOutputToOutputStream)
     29    {
     30        std::stringbuf buffer;
     31        std::ostream stream(&buffer);
     32        ConsoleWriter writer(stream);
     33        writer.setLevelMax(level::verbose);
     34
     35        EXPECT_EQ(0, buffer.in_avail());
     36        EXPECT_EQ("", buffer.str());
     37    }
     38
     39    TEST(ConsoleWriterTest, WritesOutputToOutputStream)
     40    {
     41        std::stringbuf buffer;
     42        std::ostream stream(&buffer);
     43        ConsoleWriter writer(stream);
     44        writer.setLevelMax(level::verbose);
     45
     46        std::vector<std::string> lines;
     47        lines.push_back("test");
     48
     49        EXPECT_TRUE(writer.acceptsOutput(level::debug_output, context::undefined()));
     50
     51        writer.unfilteredOutput(level::debug_output, context::undefined(), lines);
     52
     53        EXPECT_LT(0, buffer.in_avail());
     54        EXPECT_NE("", buffer.str());
     55        EXPECT_NE(std::string::npos, buffer.str().find("test"));
     56    }
     57
     58    TEST(ConsoleWriterTest, DefaultConsoleWriterUsesCout)
     59    {
     60        EXPECT_EQ(std::cout, OutputManager::getInstance().getConsoleWriter().getOutputStream());
     61    }
    2562}
Note: See TracChangeset for help on using the changeset viewer.