How to test boost logger output in unit testing?











up vote
1
down vote

favorite












My application uses BOOST_LOG_TRIVIAL to log messages. I thought it's good to check in test (gtest) that correct messages are written there in given scenarios.



Is there a way to somehow access what was written there after code was called? Or does it first have to be mocked in some way?



I have googled a lot for this and haven't found any instructions, so either I am asking wrong questions or no one thinks it should be done.










share|improve this question




























    up vote
    1
    down vote

    favorite












    My application uses BOOST_LOG_TRIVIAL to log messages. I thought it's good to check in test (gtest) that correct messages are written there in given scenarios.



    Is there a way to somehow access what was written there after code was called? Or does it first have to be mocked in some way?



    I have googled a lot for this and haven't found any instructions, so either I am asking wrong questions or no one thinks it should be done.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My application uses BOOST_LOG_TRIVIAL to log messages. I thought it's good to check in test (gtest) that correct messages are written there in given scenarios.



      Is there a way to somehow access what was written there after code was called? Or does it first have to be mocked in some way?



      I have googled a lot for this and haven't found any instructions, so either I am asking wrong questions or no one thinks it should be done.










      share|improve this question















      My application uses BOOST_LOG_TRIVIAL to log messages. I thought it's good to check in test (gtest) that correct messages are written there in given scenarios.



      Is there a way to somehow access what was written there after code was called? Or does it first have to be mocked in some way?



      I have googled a lot for this and haven't found any instructions, so either I am asking wrong questions or no one thinks it should be done.







      c++ logging boost googletest boost-log






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked yesterday









      Konrad

      398




      398
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          In your googletest suite, you can use the boost::log facilities in
          each test case to redirect the BOOST_LOG_TRIVIAL messages to a file.
          After writing the BOOST_LOG_TRIVIAL message(s) you want you can then
          flush the file, open it, and check that it has the contents you expect.
          For example:



          gtester.cpp



          #include <gtest/gtest.h>
          #include <boost/shared_ptr.hpp>
          #include <boost/log/sinks/sync_frontend.hpp>
          #include <boost/log/sinks/text_file_backend.hpp>
          #include <boost/log/utility/setup/file.hpp>
          #include <boost/log/trivial.hpp>
          #include <string>
          #include <fstream>

          using sink_t = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

          struct boost_log_tester : ::testing::Test {
          void SetUp() {
          file_sink = boost::log::add_file_log("boost.log");
          }
          void TearDown() {
          boost::log::core::get()->remove_sink(file_sink);
          file_sink.reset();
          }
          protected:
          boost::shared_ptr<sink_t> file_sink;
          };

          TEST_F(boost_log_tester,info_msg)
          {
          std::string msg = "An informational severity message";
          BOOST_LOG_TRIVIAL(info) << msg;
          file_sink->flush();
          std::ifstream captured_cout("boost.log");
          ASSERT_TRUE(captured_cout.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cout_str;
          std::getline(captured_cout,cout_str);
          EXPECT_NE(cout_str.find(msg),std::string::npos);
          }

          TEST_F(boost_log_tester,error_msg)
          {
          std::string msg = "An error severity message";
          BOOST_LOG_TRIVIAL(error) << msg;
          file_sink->flush();
          std::ifstream captured_cerr("boost.log");
          ASSERT_TRUE(captured_cerr.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cerr_str;
          std::getline(captured_cerr,cerr_str);
          EXPECT_NE(cerr_str.find(msg),std::string::npos);
          }

          int main(int argc, char **argv) {
          ::testing::InitGoogleTest(&argc, argv);
          return RUN_ALL_TESTS();
          }


          Compile and link:



          $ g++ -Wall -Wextra -DBOOST_LOG_DYN_LINK -c gtester.cpp
          $ g++ -o gtester gtester.o -lboost_log -lboost_thread -lboost_system -lgtest -pthread


          And it runs like:



          $ ./gtester 
          [==========] Running 2 tests from 1 test case.
          [----------] Global test environment set-up.
          [----------] 2 tests from boost_log_tester
          [ RUN ] boost_log_tester.info_msg
          [ OK ] boost_log_tester.info_msg (0 ms)
          [ RUN ] boost_log_tester.error_msg
          [ OK ] boost_log_tester.error_msg (2 ms)
          [----------] 2 tests from boost_log_tester (2 ms total)

          [----------] Global test environment tear-down
          [==========] 2 tests from 1 test case ran. (2 ms total)
          [ PASSED ] 2 tests.





          share|improve this answer





















          • Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
            – Konrad
            18 hours ago


















          up vote
          1
          down vote













          Following Mike's answer, in which I think necessity of file creation is a downside, I did a bit of research and found this way of using a stringstream instead:



          class boost_logger_test : public testing::Test
          {
          typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> sink_t;
          boost::shared_ptr<sink_t> streamSink;
          std::stringstream ss;

          public:
          virtual void SetUp()
          {
          streamSink = boost::log::add_console_log(ss);
          }

          virtual void TearDown()
          {
          boost::log::core::get()->remove_sink(streamSink);
          streamSink.reset();
          }

          std::vector<std::string> getLogMessages()
          {
          std::vector<std::string> messages;
          std::string msg;
          while (std::getline(ss, msg, 'n'))
          {
          messages.push_back(msg);
          }
          return messages;
          }
          }


          Then you could easily use messages in tests, like:



          ASSERT_THAT(getLogMessages(), ElementsAre("Just some log message"));





          share|improve this answer





















          • Was going to follow up with much the same, but you're there already ;)
            – Mike Kinghan
            10 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372078%2fhow-to-test-boost-logger-output-in-unit-testing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          In your googletest suite, you can use the boost::log facilities in
          each test case to redirect the BOOST_LOG_TRIVIAL messages to a file.
          After writing the BOOST_LOG_TRIVIAL message(s) you want you can then
          flush the file, open it, and check that it has the contents you expect.
          For example:



          gtester.cpp



          #include <gtest/gtest.h>
          #include <boost/shared_ptr.hpp>
          #include <boost/log/sinks/sync_frontend.hpp>
          #include <boost/log/sinks/text_file_backend.hpp>
          #include <boost/log/utility/setup/file.hpp>
          #include <boost/log/trivial.hpp>
          #include <string>
          #include <fstream>

          using sink_t = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

          struct boost_log_tester : ::testing::Test {
          void SetUp() {
          file_sink = boost::log::add_file_log("boost.log");
          }
          void TearDown() {
          boost::log::core::get()->remove_sink(file_sink);
          file_sink.reset();
          }
          protected:
          boost::shared_ptr<sink_t> file_sink;
          };

          TEST_F(boost_log_tester,info_msg)
          {
          std::string msg = "An informational severity message";
          BOOST_LOG_TRIVIAL(info) << msg;
          file_sink->flush();
          std::ifstream captured_cout("boost.log");
          ASSERT_TRUE(captured_cout.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cout_str;
          std::getline(captured_cout,cout_str);
          EXPECT_NE(cout_str.find(msg),std::string::npos);
          }

          TEST_F(boost_log_tester,error_msg)
          {
          std::string msg = "An error severity message";
          BOOST_LOG_TRIVIAL(error) << msg;
          file_sink->flush();
          std::ifstream captured_cerr("boost.log");
          ASSERT_TRUE(captured_cerr.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cerr_str;
          std::getline(captured_cerr,cerr_str);
          EXPECT_NE(cerr_str.find(msg),std::string::npos);
          }

          int main(int argc, char **argv) {
          ::testing::InitGoogleTest(&argc, argv);
          return RUN_ALL_TESTS();
          }


          Compile and link:



          $ g++ -Wall -Wextra -DBOOST_LOG_DYN_LINK -c gtester.cpp
          $ g++ -o gtester gtester.o -lboost_log -lboost_thread -lboost_system -lgtest -pthread


          And it runs like:



          $ ./gtester 
          [==========] Running 2 tests from 1 test case.
          [----------] Global test environment set-up.
          [----------] 2 tests from boost_log_tester
          [ RUN ] boost_log_tester.info_msg
          [ OK ] boost_log_tester.info_msg (0 ms)
          [ RUN ] boost_log_tester.error_msg
          [ OK ] boost_log_tester.error_msg (2 ms)
          [----------] 2 tests from boost_log_tester (2 ms total)

          [----------] Global test environment tear-down
          [==========] 2 tests from 1 test case ran. (2 ms total)
          [ PASSED ] 2 tests.





          share|improve this answer





















          • Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
            – Konrad
            18 hours ago















          up vote
          2
          down vote



          accepted










          In your googletest suite, you can use the boost::log facilities in
          each test case to redirect the BOOST_LOG_TRIVIAL messages to a file.
          After writing the BOOST_LOG_TRIVIAL message(s) you want you can then
          flush the file, open it, and check that it has the contents you expect.
          For example:



          gtester.cpp



          #include <gtest/gtest.h>
          #include <boost/shared_ptr.hpp>
          #include <boost/log/sinks/sync_frontend.hpp>
          #include <boost/log/sinks/text_file_backend.hpp>
          #include <boost/log/utility/setup/file.hpp>
          #include <boost/log/trivial.hpp>
          #include <string>
          #include <fstream>

          using sink_t = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

          struct boost_log_tester : ::testing::Test {
          void SetUp() {
          file_sink = boost::log::add_file_log("boost.log");
          }
          void TearDown() {
          boost::log::core::get()->remove_sink(file_sink);
          file_sink.reset();
          }
          protected:
          boost::shared_ptr<sink_t> file_sink;
          };

          TEST_F(boost_log_tester,info_msg)
          {
          std::string msg = "An informational severity message";
          BOOST_LOG_TRIVIAL(info) << msg;
          file_sink->flush();
          std::ifstream captured_cout("boost.log");
          ASSERT_TRUE(captured_cout.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cout_str;
          std::getline(captured_cout,cout_str);
          EXPECT_NE(cout_str.find(msg),std::string::npos);
          }

          TEST_F(boost_log_tester,error_msg)
          {
          std::string msg = "An error severity message";
          BOOST_LOG_TRIVIAL(error) << msg;
          file_sink->flush();
          std::ifstream captured_cerr("boost.log");
          ASSERT_TRUE(captured_cerr.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cerr_str;
          std::getline(captured_cerr,cerr_str);
          EXPECT_NE(cerr_str.find(msg),std::string::npos);
          }

          int main(int argc, char **argv) {
          ::testing::InitGoogleTest(&argc, argv);
          return RUN_ALL_TESTS();
          }


          Compile and link:



          $ g++ -Wall -Wextra -DBOOST_LOG_DYN_LINK -c gtester.cpp
          $ g++ -o gtester gtester.o -lboost_log -lboost_thread -lboost_system -lgtest -pthread


          And it runs like:



          $ ./gtester 
          [==========] Running 2 tests from 1 test case.
          [----------] Global test environment set-up.
          [----------] 2 tests from boost_log_tester
          [ RUN ] boost_log_tester.info_msg
          [ OK ] boost_log_tester.info_msg (0 ms)
          [ RUN ] boost_log_tester.error_msg
          [ OK ] boost_log_tester.error_msg (2 ms)
          [----------] 2 tests from boost_log_tester (2 ms total)

          [----------] Global test environment tear-down
          [==========] 2 tests from 1 test case ran. (2 ms total)
          [ PASSED ] 2 tests.





          share|improve this answer





















          • Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
            – Konrad
            18 hours ago













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          In your googletest suite, you can use the boost::log facilities in
          each test case to redirect the BOOST_LOG_TRIVIAL messages to a file.
          After writing the BOOST_LOG_TRIVIAL message(s) you want you can then
          flush the file, open it, and check that it has the contents you expect.
          For example:



          gtester.cpp



          #include <gtest/gtest.h>
          #include <boost/shared_ptr.hpp>
          #include <boost/log/sinks/sync_frontend.hpp>
          #include <boost/log/sinks/text_file_backend.hpp>
          #include <boost/log/utility/setup/file.hpp>
          #include <boost/log/trivial.hpp>
          #include <string>
          #include <fstream>

          using sink_t = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

          struct boost_log_tester : ::testing::Test {
          void SetUp() {
          file_sink = boost::log::add_file_log("boost.log");
          }
          void TearDown() {
          boost::log::core::get()->remove_sink(file_sink);
          file_sink.reset();
          }
          protected:
          boost::shared_ptr<sink_t> file_sink;
          };

          TEST_F(boost_log_tester,info_msg)
          {
          std::string msg = "An informational severity message";
          BOOST_LOG_TRIVIAL(info) << msg;
          file_sink->flush();
          std::ifstream captured_cout("boost.log");
          ASSERT_TRUE(captured_cout.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cout_str;
          std::getline(captured_cout,cout_str);
          EXPECT_NE(cout_str.find(msg),std::string::npos);
          }

          TEST_F(boost_log_tester,error_msg)
          {
          std::string msg = "An error severity message";
          BOOST_LOG_TRIVIAL(error) << msg;
          file_sink->flush();
          std::ifstream captured_cerr("boost.log");
          ASSERT_TRUE(captured_cerr.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cerr_str;
          std::getline(captured_cerr,cerr_str);
          EXPECT_NE(cerr_str.find(msg),std::string::npos);
          }

          int main(int argc, char **argv) {
          ::testing::InitGoogleTest(&argc, argv);
          return RUN_ALL_TESTS();
          }


          Compile and link:



          $ g++ -Wall -Wextra -DBOOST_LOG_DYN_LINK -c gtester.cpp
          $ g++ -o gtester gtester.o -lboost_log -lboost_thread -lboost_system -lgtest -pthread


          And it runs like:



          $ ./gtester 
          [==========] Running 2 tests from 1 test case.
          [----------] Global test environment set-up.
          [----------] 2 tests from boost_log_tester
          [ RUN ] boost_log_tester.info_msg
          [ OK ] boost_log_tester.info_msg (0 ms)
          [ RUN ] boost_log_tester.error_msg
          [ OK ] boost_log_tester.error_msg (2 ms)
          [----------] 2 tests from boost_log_tester (2 ms total)

          [----------] Global test environment tear-down
          [==========] 2 tests from 1 test case ran. (2 ms total)
          [ PASSED ] 2 tests.





          share|improve this answer












          In your googletest suite, you can use the boost::log facilities in
          each test case to redirect the BOOST_LOG_TRIVIAL messages to a file.
          After writing the BOOST_LOG_TRIVIAL message(s) you want you can then
          flush the file, open it, and check that it has the contents you expect.
          For example:



          gtester.cpp



          #include <gtest/gtest.h>
          #include <boost/shared_ptr.hpp>
          #include <boost/log/sinks/sync_frontend.hpp>
          #include <boost/log/sinks/text_file_backend.hpp>
          #include <boost/log/utility/setup/file.hpp>
          #include <boost/log/trivial.hpp>
          #include <string>
          #include <fstream>

          using sink_t = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

          struct boost_log_tester : ::testing::Test {
          void SetUp() {
          file_sink = boost::log::add_file_log("boost.log");
          }
          void TearDown() {
          boost::log::core::get()->remove_sink(file_sink);
          file_sink.reset();
          }
          protected:
          boost::shared_ptr<sink_t> file_sink;
          };

          TEST_F(boost_log_tester,info_msg)
          {
          std::string msg = "An informational severity message";
          BOOST_LOG_TRIVIAL(info) << msg;
          file_sink->flush();
          std::ifstream captured_cout("boost.log");
          ASSERT_TRUE(captured_cout.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cout_str;
          std::getline(captured_cout,cout_str);
          EXPECT_NE(cout_str.find(msg),std::string::npos);
          }

          TEST_F(boost_log_tester,error_msg)
          {
          std::string msg = "An error severity message";
          BOOST_LOG_TRIVIAL(error) << msg;
          file_sink->flush();
          std::ifstream captured_cerr("boost.log");
          ASSERT_TRUE(captured_cerr.good()) << "Failure executing test: Could not open `boost.log` for reading";
          std::string cerr_str;
          std::getline(captured_cerr,cerr_str);
          EXPECT_NE(cerr_str.find(msg),std::string::npos);
          }

          int main(int argc, char **argv) {
          ::testing::InitGoogleTest(&argc, argv);
          return RUN_ALL_TESTS();
          }


          Compile and link:



          $ g++ -Wall -Wextra -DBOOST_LOG_DYN_LINK -c gtester.cpp
          $ g++ -o gtester gtester.o -lboost_log -lboost_thread -lboost_system -lgtest -pthread


          And it runs like:



          $ ./gtester 
          [==========] Running 2 tests from 1 test case.
          [----------] Global test environment set-up.
          [----------] 2 tests from boost_log_tester
          [ RUN ] boost_log_tester.info_msg
          [ OK ] boost_log_tester.info_msg (0 ms)
          [ RUN ] boost_log_tester.error_msg
          [ OK ] boost_log_tester.error_msg (2 ms)
          [----------] 2 tests from boost_log_tester (2 ms total)

          [----------] Global test environment tear-down
          [==========] 2 tests from 1 test case ran. (2 ms total)
          [ PASSED ] 2 tests.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Mike Kinghan

          29.2k762107




          29.2k762107












          • Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
            – Konrad
            18 hours ago


















          • Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
            – Konrad
            18 hours ago
















          Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
          – Konrad
          18 hours ago




          Thanks Mike. Works really well, only downside of this solution I see is that it requires a file creation.
          – Konrad
          18 hours ago












          up vote
          1
          down vote













          Following Mike's answer, in which I think necessity of file creation is a downside, I did a bit of research and found this way of using a stringstream instead:



          class boost_logger_test : public testing::Test
          {
          typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> sink_t;
          boost::shared_ptr<sink_t> streamSink;
          std::stringstream ss;

          public:
          virtual void SetUp()
          {
          streamSink = boost::log::add_console_log(ss);
          }

          virtual void TearDown()
          {
          boost::log::core::get()->remove_sink(streamSink);
          streamSink.reset();
          }

          std::vector<std::string> getLogMessages()
          {
          std::vector<std::string> messages;
          std::string msg;
          while (std::getline(ss, msg, 'n'))
          {
          messages.push_back(msg);
          }
          return messages;
          }
          }


          Then you could easily use messages in tests, like:



          ASSERT_THAT(getLogMessages(), ElementsAre("Just some log message"));





          share|improve this answer





















          • Was going to follow up with much the same, but you're there already ;)
            – Mike Kinghan
            10 hours ago















          up vote
          1
          down vote













          Following Mike's answer, in which I think necessity of file creation is a downside, I did a bit of research and found this way of using a stringstream instead:



          class boost_logger_test : public testing::Test
          {
          typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> sink_t;
          boost::shared_ptr<sink_t> streamSink;
          std::stringstream ss;

          public:
          virtual void SetUp()
          {
          streamSink = boost::log::add_console_log(ss);
          }

          virtual void TearDown()
          {
          boost::log::core::get()->remove_sink(streamSink);
          streamSink.reset();
          }

          std::vector<std::string> getLogMessages()
          {
          std::vector<std::string> messages;
          std::string msg;
          while (std::getline(ss, msg, 'n'))
          {
          messages.push_back(msg);
          }
          return messages;
          }
          }


          Then you could easily use messages in tests, like:



          ASSERT_THAT(getLogMessages(), ElementsAre("Just some log message"));





          share|improve this answer





















          • Was going to follow up with much the same, but you're there already ;)
            – Mike Kinghan
            10 hours ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          Following Mike's answer, in which I think necessity of file creation is a downside, I did a bit of research and found this way of using a stringstream instead:



          class boost_logger_test : public testing::Test
          {
          typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> sink_t;
          boost::shared_ptr<sink_t> streamSink;
          std::stringstream ss;

          public:
          virtual void SetUp()
          {
          streamSink = boost::log::add_console_log(ss);
          }

          virtual void TearDown()
          {
          boost::log::core::get()->remove_sink(streamSink);
          streamSink.reset();
          }

          std::vector<std::string> getLogMessages()
          {
          std::vector<std::string> messages;
          std::string msg;
          while (std::getline(ss, msg, 'n'))
          {
          messages.push_back(msg);
          }
          return messages;
          }
          }


          Then you could easily use messages in tests, like:



          ASSERT_THAT(getLogMessages(), ElementsAre("Just some log message"));





          share|improve this answer












          Following Mike's answer, in which I think necessity of file creation is a downside, I did a bit of research and found this way of using a stringstream instead:



          class boost_logger_test : public testing::Test
          {
          typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> sink_t;
          boost::shared_ptr<sink_t> streamSink;
          std::stringstream ss;

          public:
          virtual void SetUp()
          {
          streamSink = boost::log::add_console_log(ss);
          }

          virtual void TearDown()
          {
          boost::log::core::get()->remove_sink(streamSink);
          streamSink.reset();
          }

          std::vector<std::string> getLogMessages()
          {
          std::vector<std::string> messages;
          std::string msg;
          while (std::getline(ss, msg, 'n'))
          {
          messages.push_back(msg);
          }
          return messages;
          }
          }


          Then you could easily use messages in tests, like:



          ASSERT_THAT(getLogMessages(), ElementsAre("Just some log message"));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 15 hours ago









          Konrad

          398




          398












          • Was going to follow up with much the same, but you're there already ;)
            – Mike Kinghan
            10 hours ago


















          • Was going to follow up with much the same, but you're there already ;)
            – Mike Kinghan
            10 hours ago
















          Was going to follow up with much the same, but you're there already ;)
          – Mike Kinghan
          10 hours ago




          Was going to follow up with much the same, but you're there already ;)
          – Mike Kinghan
          10 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372078%2fhow-to-test-boost-logger-output-in-unit-testing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Saint-Aignan (Tarn-et-Garonne)

          Volksrepublik China