Release 18.08
[platform/upstream/armnn.git] / src / armnnUtils / Logging.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include "Logging.hpp"
6
7 #include <string>
8 #include <iostream>
9
10 #if defined(_MSC_VER)
11 #include <Windows.h>
12 #endif
13
14 #if defined(__ANDROID__)
15 #include <android/log.h>
16 #endif
17
18 #include <boost/make_shared.hpp>
19 #include <boost/log/core.hpp>
20 #include <boost/log/sinks.hpp>
21 #include <boost/log/sinks/debug_output_backend.hpp>
22 #include <boost/log/sinks/basic_sink_backend.hpp>
23 #include <boost/log/sinks/text_ostream_backend.hpp>
24 #include <boost/log/utility/setup/console.hpp>
25
26 namespace armnnUtils
27 {
28
29 struct DebugOutputSink : boost::log::sinks::basic_formatted_sink_backend<char, boost::log::sinks::concurrent_feeding>
30 {
31     void consume(boost::log::record_view const& rec, std::string const& formatted_message)
32     {
33 #if defined(_MSC_VER)
34         OutputDebugString(formatted_message.c_str());
35         OutputDebugString("\n");
36 #endif
37 #if defined(__ANDROID__)
38         __android_log_write(ANDROID_LOG_DEBUG, "armnn", formatted_message.c_str());
39 #endif
40     }
41 };
42
43 void ConfigureLogging(boost::log::core* core, bool printToStandardOutput, bool printToDebugOutput,
44     armnn::LogSeverity severity)
45 {
46     // Even if we remove all the sinks, Boost will fallback to the 'default sink' and still print stuff to
47     // stdout, so we have to explicitly disable logging in this case.
48     core->set_logging_enabled(printToStandardOutput || printToDebugOutput);
49
50     // Sets up severity filter.
51     boost::log::trivial::severity_level boostSeverity;
52     switch (severity)
53     {
54     case armnn::LogSeverity::Trace:
55         boostSeverity = boost::log::trivial::trace;
56         break;
57     case armnn::LogSeverity::Debug:
58         boostSeverity = boost::log::trivial::debug;
59         break;
60     case armnn::LogSeverity::Info:
61         boostSeverity = boost::log::trivial::info;
62         break;
63     case armnn::LogSeverity::Warning:
64         boostSeverity = boost::log::trivial::warning;
65         break;
66     case armnn::LogSeverity::Error:
67         boostSeverity = boost::log::trivial::error;
68         break;
69     case armnn::LogSeverity::Fatal:
70         boostSeverity = boost::log::trivial::fatal;
71         break;
72     default:
73         BOOST_ASSERT_MSG(false, "Invalid severity");
74     }
75     core->set_filter(boost::log::trivial::severity >= boostSeverity);
76
77     core->remove_all_sinks();
78     if (printToStandardOutput)
79     {
80         typedef boost::log::sinks::basic_text_ostream_backend<char> backend_t;
81         boost::shared_ptr<backend_t>                                backend = boost::make_shared<backend_t>();
82
83         boost::shared_ptr<std::basic_ostream<char>> stream(&std::cout, boost::null_deleter());
84         backend->add_stream(stream);
85
86         typedef boost::log::sinks::synchronous_sink<backend_t> sink_t;
87         boost::shared_ptr<sink_t>                              standardOutputSink = boost::make_shared<sink_t>(backend);
88
89         core->add_sink(standardOutputSink);
90     }
91     if (printToDebugOutput)
92     {
93         typedef boost::log::sinks::synchronous_sink<DebugOutputSink> sink_t;
94         boost::shared_ptr<sink_t>                                    debugOutputSink(new sink_t());
95         core->add_sink(debugOutputSink);
96     }
97 }
98
99 }