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/event.hpp | 58 ++++++++++++++++++++++++++++++ include/mbgl/util/logging.hpp | 80 ++++++++++++++++++++++++++++++++++++++++++ include/mbgl/util/platform.hpp | 32 +++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 include/mbgl/util/event.hpp create mode 100644 include/mbgl/util/logging.hpp create mode 100644 include/mbgl/util/platform.hpp (limited to 'include/mbgl/util') diff --git a/include/mbgl/util/event.hpp b/include/mbgl/util/event.hpp new file mode 100644 index 0000000000..7ad3d914e8 --- /dev/null +++ b/include/mbgl/util/event.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include + +namespace mbgl { + +enum class EventSeverity : uint8_t { + Debug, + Info, + Warning, + Error, +}; + +enum class Event : uint8_t { + General, + Setup, + Shader, + ParseStyle, + ParseTile, + Render, + Style, + Database, + HttpRequest, + Sprite, + Image, + OpenGL, + JNI, + Android, + Crash, + Glyph, +}; + +struct EventPermutation { + const EventSeverity severity; + const Event event; + + constexpr bool operator==(const EventPermutation &rhs) const { + return severity == rhs.severity && event == rhs.event; + } +}; + +constexpr EventSeverity disabledEventSeverities[] = { +#ifdef NDEBUG + EventSeverity(-1) // Avoid zero size array +#else + EventSeverity::Debug +#endif +}; + +constexpr Event disabledEvents[] = { + Event(-1) // Avoid zero size array +}; + +constexpr EventPermutation disabledEventPermutations[] = { + { EventSeverity::Debug, Event::Shader } +}; + +} // namespace mbgl 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 diff --git a/include/mbgl/util/platform.hpp b/include/mbgl/util/platform.hpp new file mode 100644 index 0000000000..cc8327c470 --- /dev/null +++ b/include/mbgl/util/platform.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +namespace mbgl { +namespace platform { + +class Request; + +// Uppercase a string, potentially using platform-specific routines. +std::string uppercase(const std::string &string); + +// Lowercase a string, potentially using platform-specific routines. +std::string lowercase(const std::string &string); + +// Gets the name of the current thread. +std::string getCurrentThreadName(); + +// Set the name of the current thread, truncated at 15. +void setCurrentThreadName(const std::string& name); + +// Makes the current thread low priority. +void makeThreadLowPriority(); + +// Shows an alpha image with the specified dimensions in a named window. +void showDebugImage(std::string name, const char *data, size_t width, size_t height); + +// Shows an alpha image with the specified dimensions in a named window. +void showColorDebugImage(std::string name, const char *data, size_t logical_width, size_t logical_height, size_t width, size_t height); +} // namespace platform +} // namespace mbgl -- cgit v1.2.1