diff options
author | Derek Lamberti <derek.lamberti@arm.com> | 2019-11-26 16:38:31 +0000 |
---|---|---|
committer | Derek Lamberti <derek.lamberti@arm.com> | 2019-12-05 15:35:51 +0000 |
commit | 08446976e3b6ce0e02f22b391b37aacaad181e1a (patch) | |
tree | b57106c6a3e28662adb2592ac3e850a8f19b6ec7 /include/armnn | |
parent | a3b31f010004ed397ec04325edf7020984847f21 (diff) | |
download | armnn-08446976e3b6ce0e02f22b391b37aacaad181e1a.tar.gz |
Replace boost logging with simple logger
!referencetests:214319
* Reduces arm nn binary size ~15%
* Also fixed test logging black hole issues
Change-Id: Iba27db304d9a8088fa46aeb0b52225d93bb56bc8
Signed-off-by: Derek Lamberti <derek.lamberti@arm.com>
Diffstat (limited to 'include/armnn')
-rw-r--r-- | include/armnn/ArmNN.hpp | 1 | ||||
-rw-r--r-- | include/armnn/Logging.hpp | 300 | ||||
-rw-r--r-- | include/armnn/Utils.hpp | 9 |
3 files changed, 310 insertions, 0 deletions
diff --git a/include/armnn/ArmNN.hpp b/include/armnn/ArmNN.hpp index b18f14c8b7..119520b7c9 100644 --- a/include/armnn/ArmNN.hpp +++ b/include/armnn/ArmNN.hpp @@ -9,6 +9,7 @@ #include "Exceptions.hpp" #include "INetwork.hpp" #include "IRuntime.hpp" +#include "Logging.hpp" #include "LstmParams.hpp" #include "Optional.hpp" #include "QuantizedLstmParams.hpp" diff --git a/include/armnn/Logging.hpp b/include/armnn/Logging.hpp new file mode 100644 index 0000000000..9bf086bf04 --- /dev/null +++ b/include/armnn/Logging.hpp @@ -0,0 +1,300 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <iostream> + +#include "Utils.hpp" + + +#if defined(_MSC_VER) +#include <Windows.h> +#endif + +#if defined(__ANDROID__) +#include <android/log.h> +#endif + +#include <boost/assert.hpp> + + +namespace armnn +{ + +inline std::string LevelToString(LogSeverity level) +{ + switch(level) + { + case LogSeverity::Trace: + return "Trace"; + case LogSeverity::Debug: + return "Debug"; + case LogSeverity::Info: + return "Info"; + case LogSeverity::Warning: + return "Warning"; + case LogSeverity::Error: + return "Error"; + case LogSeverity::Fatal: + return "Fatal"; + default: + return "Log"; + } +} + +class LogSink +{ +public: + virtual ~LogSink(){}; + + virtual void Consume(const std::string& s) = 0; +private: + +}; + +class StandardOutputColourSink : public LogSink +{ +public: + StandardOutputColourSink(LogSeverity level = LogSeverity::Info) + : m_Level(level) + { + } + + void Consume(const std::string& s) override + { + std::cout << GetColour(m_Level) << s << ResetColour() << std::endl; + } + +private: + std::string ResetColour() + { + return "\033[0m"; + } + + std::string GetColour(LogSeverity level) + { + switch(level) + { + case LogSeverity::Trace: + return "\033[35m"; + case LogSeverity::Debug: + return "\033[32m"; + case LogSeverity::Info: + return "\033[0m"; + case LogSeverity::Warning: + return "\033[33m"; + case LogSeverity::Error: + return "\033[31m"; + case LogSeverity::Fatal: + return "\033[41;30m"; + + default: + return "\033[0m"; + } + } + LogSeverity m_Level; +}; + +class StandardOutputSink : public LogSink +{ +public: + void Consume(const std::string& s) override + { + std::cout << s << std::endl; + } +}; + +class DebugOutputSink : public LogSink +{ +public: + void Consume(const std::string& s) override + { +#if defined(_MSC_VER) + OutputDebugString(s.c_str()); + OutputDebugString("\n"); +#endif +#if defined(__ANDROID__) + __android_log_write(ANDROID_LOG_DEBUG, "armnn", s.c_str()); +#endif + } +}; + +struct ScopedRecord +{ + ScopedRecord(const std::vector<std::shared_ptr<LogSink>>& sinks, LogSeverity level, bool enabled) + : m_LogSinks(sinks) + , m_Enabled(enabled) + { + if (enabled) + { + m_Os << LevelToString(level) << ": "; + } + } + + ~ScopedRecord() + { + if (m_Enabled) + { + for (auto sink : m_LogSinks) + { + if (sink) + { + sink->Consume(m_Os.str()); + } + } + } + } + + ScopedRecord(const ScopedRecord&) = delete; + ScopedRecord& operator=(const ScopedRecord&) = delete; + ScopedRecord(ScopedRecord&& other) = default; + ScopedRecord& operator=(ScopedRecord&&) = default; + + template<typename Streamable> + ScopedRecord& operator<<(const Streamable& s) + { + if (m_Enabled) + { + m_Os << s; + } + return (*this); + } + +private: + const std::vector<std::shared_ptr<LogSink>>& m_LogSinks; + std::ostringstream m_Os; + bool m_Enabled; +}; + +template<LogSeverity Level> +class SimpleLogger +{ +public: + SimpleLogger() + : m_Sinks{std::make_shared<StandardOutputSink>()} + , m_Enable(true) + { + } + + static SimpleLogger& Get() + { + static SimpleLogger<Level> logger; + return logger; + } + + void Enable(bool enable = true) + { + m_Enable = enable; + } + + ScopedRecord StartNewRecord() + { + ScopedRecord record(m_Sinks, Level, m_Enable); + return record; + } + + void RemoveAllSinks() + { + m_Sinks.clear(); + } + + void AddSink(std::shared_ptr<LogSink> sink) + { + m_Sinks.push_back(sink); + } +private: + std::vector<std::shared_ptr<LogSink>> m_Sinks; + bool m_Enable; +}; + +inline void SetLogFilter(LogSeverity level) +{ + SimpleLogger<LogSeverity::Trace>::Get().Enable(false); + SimpleLogger<LogSeverity::Debug>::Get().Enable(false); + SimpleLogger<LogSeverity::Info>::Get().Enable(false); + SimpleLogger<LogSeverity::Warning>::Get().Enable(false); + SimpleLogger<LogSeverity::Error>::Get().Enable(false); + SimpleLogger<LogSeverity::Fatal>::Get().Enable(false); + switch (level) + { + case LogSeverity::Trace: + SimpleLogger<LogSeverity::Trace>::Get().Enable(true); + ARMNN_FALLTHROUGH; + case LogSeverity::Debug: + SimpleLogger<LogSeverity::Debug>::Get().Enable(true); + ARMNN_FALLTHROUGH; + case LogSeverity::Info: + SimpleLogger<LogSeverity::Info>::Get().Enable(true); + ARMNN_FALLTHROUGH; + case LogSeverity::Warning: + SimpleLogger<LogSeverity::Warning>::Get().Enable(true); + ARMNN_FALLTHROUGH; + case LogSeverity::Error: + SimpleLogger<LogSeverity::Error>::Get().Enable(true); + ARMNN_FALLTHROUGH; + case LogSeverity::Fatal: + SimpleLogger<LogSeverity::Fatal>::Get().Enable(true); + break; + default: + BOOST_ASSERT(false); + } +} + +template<LogSeverity Level> +inline void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured) +{ + SimpleLogger<Level>::Get().RemoveAllSinks(); + + if (standardOut) + { + if (coloured) + { + SimpleLogger<Level>::Get().AddSink( + std::make_shared<StandardOutputColourSink>(Level)); + } else + { + SimpleLogger<Level>::Get().AddSink( + std::make_shared<StandardOutputSink>()); + } + } + + if (debugOut) + { + SimpleLogger<Level>::Get().AddSink( + std::make_shared<DebugOutputSink>()); + } +} + +inline void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured) +{ + SetLoggingSinks<LogSeverity::Trace>(standardOut, debugOut, coloured); + SetLoggingSinks<LogSeverity::Debug>(standardOut, debugOut, coloured); + SetLoggingSinks<LogSeverity::Info>(standardOut, debugOut, coloured); + SetLoggingSinks<LogSeverity::Warning>(standardOut, debugOut, coloured); + SetLoggingSinks<LogSeverity::Error>(standardOut, debugOut, coloured); + SetLoggingSinks<LogSeverity::Fatal>(standardOut, debugOut, coloured); +} + +enum class BoostLogSeverityMapping +{ + trace, + debug, + info, + warning, + error, + fatal +}; + +constexpr LogSeverity ConvertLogSeverity(BoostLogSeverityMapping severity) +{ + return static_cast<LogSeverity>(severity); +} + + +#define ARMNN_LOG(severity) \ + armnn::SimpleLogger<ConvertLogSeverity(armnn::BoostLogSeverityMapping::severity)>::Get().StartNewRecord() + +} //namespace armnn diff --git a/include/armnn/Utils.hpp b/include/armnn/Utils.hpp index 26a27f4100..3113d61f12 100644 --- a/include/armnn/Utils.hpp +++ b/include/armnn/Utils.hpp @@ -26,4 +26,13 @@ enum class LogSeverity /// severity: All log messages that are at this severity level or higher will be printed, others will be ignored. void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity); + +#if defined(__clang__) &&((__clang_major__>=3)||(__clang_major__==3 && __clang_minor__ >= 5)) +# define ARMNN_FALLTHROUGH [[clang::fallthrough]] +#elif defined(__GNUC__) && (__GNUC__ >= 7) +# define ARMNN_FALLTHROUGH __attribute__((fallthrough)) +#else +# define ARMNN_FALLTHROUGH ((void)0) +#endif + } // namespace armnn |