Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame^] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #define BOOST_TEST_MODULE UnitTests |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | |
| 8 | #include "UnitTests.hpp" |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 9 | #include <armnn/Logging.hpp> |
| 10 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 11 | struct ConfigureLoggingFixture |
| 12 | { |
| 13 | ConfigureLoggingFixture() |
| 14 | { |
| 15 | ConfigureLoggingTest(); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | BOOST_GLOBAL_FIXTURE(ConfigureLoggingFixture); |
| 20 | |
| 21 | // On Windows, duplicate the boost test logging output to the Visual Studio output window using OutputDebugString. |
| 22 | #if defined(_MSC_VER) |
| 23 | |
| 24 | #include <boost/iostreams/filtering_stream.hpp> |
| 25 | #include <boost/iostreams/tee.hpp> |
| 26 | #include <iostream> |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame^] | 27 | #include <commons/include/WindowsWrapper.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace boost::iostreams; |
| 30 | using namespace std; |
| 31 | |
| 32 | struct DebugOutputSink : boost::iostreams::sink |
| 33 | { |
| 34 | std::streamsize write(const char* s, std::streamsize n) |
| 35 | { |
| 36 | // The given string is not null-terminated, so we need to copy it. |
| 37 | std::string s2(s, boost::numeric_cast<size_t>(n)); |
| 38 | OutputDebugString(s2.c_str()); |
| 39 | return n; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | class SetupDebugOutput |
| 44 | { |
| 45 | public: |
| 46 | SetupDebugOutput() |
| 47 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | // Sends the output to both cout (as standard) and the debug output. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | m_OutputStream.push(tee(std::cout)); |
| 50 | m_OutputStream.push(m_DebugOutputSink); |
| 51 | |
| 52 | boost::unit_test::unit_test_log.set_stream(m_OutputStream); |
| 53 | } |
| 54 | private: |
| 55 | filtering_ostream m_OutputStream; |
| 56 | DebugOutputSink m_DebugOutputSink; |
| 57 | }; |
| 58 | |
| 59 | BOOST_GLOBAL_FIXTURE(SetupDebugOutput); |
| 60 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 61 | #endif // defined(_MSC_VER) |
| 62 | |
| 63 | |
| 64 | BOOST_AUTO_TEST_SUITE(LoggerSuite) |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(LoggerTest) |
| 67 | { |
| 68 | std::stringstream ss; |
| 69 | |
| 70 | { |
| 71 | struct StreamRedirector |
| 72 | { |
| 73 | public: |
| 74 | StreamRedirector(std::ostream& stream, std::streambuf* newStreamBuffer) |
| 75 | : m_Stream(stream) |
| 76 | , m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer)) |
| 77 | {} |
| 78 | ~StreamRedirector() { m_Stream.rdbuf(m_BackupBuffer); } |
| 79 | |
| 80 | private: |
| 81 | std::ostream& m_Stream; |
| 82 | std::streambuf* m_BackupBuffer; |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | StreamRedirector redirect(std::cout, ss.rdbuf()); |
| 87 | |
| 88 | using namespace armnn; |
| 89 | SetLogFilter(LogSeverity::Trace); |
| 90 | SetAllLoggingSinks(true, false, false); |
| 91 | |
| 92 | |
| 93 | ARMNN_LOG(trace) << "My trace message; " << -2; |
| 94 | ARMNN_LOG(debug) << "My debug message; " << -1; |
| 95 | ARMNN_LOG(info) << "My info message; " << 0; |
| 96 | ARMNN_LOG(warning) << "My warning message; " << 1; |
| 97 | ARMNN_LOG(error) << "My error message; " << 2; |
| 98 | ARMNN_LOG(fatal) << "My fatal message; " << 3; |
| 99 | |
| 100 | SetLogFilter(LogSeverity::Fatal); |
| 101 | |
| 102 | } |
| 103 | |
David Monahan | a8837bf | 2020-04-16 10:01:56 +0100 | [diff] [blame] | 104 | BOOST_CHECK(ss.str().find("Trace: My trace message; -2") != std::string::npos); |
| 105 | BOOST_CHECK(ss.str().find("Debug: My debug message; -1") != std::string::npos); |
| 106 | BOOST_CHECK(ss.str().find("Info: My info message; 0") != std::string::npos); |
| 107 | BOOST_CHECK(ss.str().find("Warning: My warning message; 1") != std::string::npos); |
| 108 | BOOST_CHECK(ss.str().find("Error: My error message; 2") != std::string::npos); |
| 109 | BOOST_CHECK(ss.str().find("Fatal: My fatal message; 3") != std::string::npos); |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_SUITE_END() |