From bbfe603e5ae42317a2b67d713d00882bea341c88 Mon Sep 17 00:00:00 2001 From: Jim Flynn Date: Mon, 20 Jul 2020 16:57:44 +0100 Subject: IVGCVSW-5166 Pull out the common and server side code into standalone libraries Change-Id: I180f84c493a9b2be4b93b25d312ebdd9e71b1735 Signed-off-by: Jim Flynn --- profiling/common/include/Logging.hpp | 182 +++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 profiling/common/include/Logging.hpp (limited to 'profiling/common/include/Logging.hpp') diff --git a/profiling/common/include/Logging.hpp b/profiling/common/include/Logging.hpp new file mode 100644 index 0000000000..a31c2aaa7b --- /dev/null +++ b/profiling/common/include/Logging.hpp @@ -0,0 +1,182 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include +#include +#include +#include + +namespace arm +{ + +namespace pipe +{ + +enum class LogSeverity +{ + Trace, + Debug, + Info, + Warning, + Error, + Fatal +}; + +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&) = 0; +private: + +}; + +class StandardOutputSink : public LogSink +{ +public: + void Consume(const std::string& s) override + { + std::cout << s << std::endl; + } +}; + +struct ScopedRecord +{ + ScopedRecord(const std::vector>& 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& operator=(ScopedRecord&&) = delete; + + ScopedRecord(ScopedRecord&& other) = default; + + template + ScopedRecord& operator<<(const Streamable& s) + { + if (m_Enabled) + { + m_Os << s; + } + return (*this); + } + +private: + const std::vector>& m_LogSinks; + std::ostringstream m_Os; + bool m_Enabled; +}; + +template +class SimpleLogger +{ +public: + SimpleLogger() + : m_Sinks{std::make_shared()} + , m_Enable(true) + { + } + + static SimpleLogger& Get() + { + static SimpleLogger 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 sink) + { + m_Sinks.push_back(sink); + } +private: + std::vector> m_Sinks; + bool m_Enable; +}; + +void SetLogFilter(LogSeverity level); + +void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured); + +enum class BoostLogSeverityMapping +{ + trace, + debug, + info, + warning, + error, + fatal +}; + +constexpr LogSeverity ConvertLogSeverity(BoostLogSeverityMapping severity) +{ + return static_cast(severity); +} + + +#define ARM_PIPE_LOG(severity) \ + arm::pipe::SimpleLogger::Get().StartNewRecord() + +} // namespace pipe +} // namespace arm -- cgit v1.2.1