2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
14 #if defined(__ANDROID__)
15 #include <android/log.h>
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>
29 struct DebugOutputSink : boost::log::sinks::basic_formatted_sink_backend<char, boost::log::sinks::concurrent_feeding>
31 void consume(boost::log::record_view const& rec, std::string const& formatted_message)
34 OutputDebugString(formatted_message.c_str());
35 OutputDebugString("\n");
37 #if defined(__ANDROID__)
38 __android_log_write(ANDROID_LOG_DEBUG, "armnn", formatted_message.c_str());
43 void ConfigureLogging(boost::log::core* core, bool printToStandardOutput, bool printToDebugOutput,
44 armnn::LogSeverity severity)
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);
50 // Sets up severity filter.
51 boost::log::trivial::severity_level boostSeverity;
54 case armnn::LogSeverity::Trace:
55 boostSeverity = boost::log::trivial::trace;
57 case armnn::LogSeverity::Debug:
58 boostSeverity = boost::log::trivial::debug;
60 case armnn::LogSeverity::Info:
61 boostSeverity = boost::log::trivial::info;
63 case armnn::LogSeverity::Warning:
64 boostSeverity = boost::log::trivial::warning;
66 case armnn::LogSeverity::Error:
67 boostSeverity = boost::log::trivial::error;
69 case armnn::LogSeverity::Fatal:
70 boostSeverity = boost::log::trivial::fatal;
73 BOOST_ASSERT_MSG(false, "Invalid severity");
75 core->set_filter(boost::log::trivial::severity >= boostSeverity);
77 core->remove_all_sinks();
78 if (printToStandardOutput)
80 typedef boost::log::sinks::basic_text_ostream_backend<char> backend_t;
81 boost::shared_ptr<backend_t> backend = boost::make_shared<backend_t>();
83 boost::shared_ptr<std::basic_ostream<char>> stream(&std::cout, boost::null_deleter());
84 backend->add_stream(stream);
86 typedef boost::log::sinks::synchronous_sink<backend_t> sink_t;
87 boost::shared_ptr<sink_t> standardOutputSink = boost::make_shared<sink_t>(backend);
89 core->add_sink(standardOutputSink);
91 if (printToDebugOutput)
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);