From e76de0540284118845c93c4351c82c6c8d5a090a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 22 Nov 2016 15:43:19 +0100 Subject: [build] move logging to util --- include/mbgl/util/logging.hpp | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 include/mbgl/util/logging.hpp (limited to 'include/mbgl/util/logging.hpp') diff --git a/include/mbgl/util/logging.hpp b/include/mbgl/util/logging.hpp new file mode 100644 index 0000000000..d072673e76 --- /dev/null +++ b/include/mbgl/util/logging.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include + +#include + +#include +#include + +namespace mbgl { + +class Log { +public: + class Observer : private util::noncopyable { + public: + virtual ~Observer() = default; + + // When an observer is set, this function will be called for every log + // message. Returning true will consume the message. + virtual bool onRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) = 0; + }; + + class NullObserver : public Observer { + bool onRecord(EventSeverity, Event, int64_t, const std::string&) override { + return true; + } + }; + + static void setObserver(std::unique_ptr Observer); + static std::unique_ptr removeObserver(); + +private: + template + constexpr static bool includes(const T e, const T (&l)[N], const size_t i = 0) { + return i < N && (l[i] == e || includes(e, l, i + 1)); + } + +public: + template + static void Debug(Event event, Args&& ...args) { + Record(EventSeverity::Debug, event, ::std::forward(args)...); + } + + template + static void Info(Event event, Args&& ...args) { + Record(EventSeverity::Info, event, ::std::forward(args)...); + } + + template + static void Warning(Event event, Args&& ...args) { + Record(EventSeverity::Warning, event, ::std::forward(args)...); + } + + template + static void Error(Event event, Args&& ...args) { + Record(EventSeverity::Error, event, ::std::forward(args)...); + } + + template + static void Record(EventSeverity severity, Event event, Args&& ...args) { + if (!includes(severity, disabledEventSeverities) && + !includes(event, disabledEvents) && + !includes({ severity, event }, disabledEventPermutations)) { + record(severity, event, ::std::forward(args)...); + } + } + +private: + static void record(EventSeverity severity, Event event, const std::string &msg); + static void record(EventSeverity severity, Event event, const char* format, ...); + static void record(EventSeverity severity, Event event, int64_t code); + static void record(EventSeverity severity, Event event, int64_t code, const std::string &msg); + + // This method is the data sink that must be implemented by each platform we + // support. It should ideally output the error message in a human readable + // format to the developer. + static void platformRecord(EventSeverity severity, const std::string &msg); +}; + +} // namespace mbgl -- cgit v1.2.1