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