blob: 5d9211e44fd69abd56cc419720874a469618d799 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#define BOOST_TEST_MODULE UnitTests
6#include <boost/test/unit_test.hpp>
7
8#include "UnitTests.hpp"
Derek Lamberti08446972019-11-26 16:38:31 +00009#include <armnn/Logging.hpp>
10
telsoa014fcda012018-03-09 14:13:49 +000011struct ConfigureLoggingFixture
12{
13 ConfigureLoggingFixture()
14 {
15 ConfigureLoggingTest();
16 }
17};
18
19BOOST_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 Flynnbbfe6032020-07-20 16:57:44 +010027#include <commons/include/WindowsWrapper.hpp>
telsoa014fcda012018-03-09 14:13:49 +000028
29using namespace boost::iostreams;
30using namespace std;
31
32struct 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
43class SetupDebugOutput
44{
45public:
46 SetupDebugOutput()
47 {
telsoa01c577f2c2018-08-31 09:22:23 +010048 // Sends the output to both cout (as standard) and the debug output.
telsoa014fcda012018-03-09 14:13:49 +000049 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 }
54private:
55 filtering_ostream m_OutputStream;
56 DebugOutputSink m_DebugOutputSink;
57};
58
59BOOST_GLOBAL_FIXTURE(SetupDebugOutput);
60
Derek Lamberti08446972019-11-26 16:38:31 +000061#endif // defined(_MSC_VER)
62
63
64BOOST_AUTO_TEST_SUITE(LoggerSuite)
65
66BOOST_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 Monahana8837bf2020-04-16 10:01:56 +0100104 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 Lamberti08446972019-11-26 16:38:31 +0000110}
111
112BOOST_AUTO_TEST_SUITE_END()