summaryrefslogtreecommitdiff
path: root/include/mbgl/util
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-11-22 15:43:19 +0100
committerKonstantin Käfer <mail@kkaefer.com>2016-11-22 20:58:51 +0100
commite76de0540284118845c93c4351c82c6c8d5a090a (patch)
treedc295f87b74c5a4766444f6d1e7020e6219017a8 /include/mbgl/util
parent117863f1114551407c481abc752f5fcfd139c878 (diff)
downloadqtlocation-mapboxgl-e76de0540284118845c93c4351c82c6c8d5a090a.tar.gz
[build] move logging to util
Diffstat (limited to 'include/mbgl/util')
-rw-r--r--include/mbgl/util/event.hpp58
-rw-r--r--include/mbgl/util/logging.hpp80
-rw-r--r--include/mbgl/util/platform.hpp32
3 files changed, 170 insertions, 0 deletions
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 <cstdint>
+
+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 <mbgl/util/event.hpp>
+
+#include <mbgl/util/noncopyable.hpp>
+
+#include <memory>
+#include <string>
+
+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> Observer);
+ static std::unique_ptr<Observer> removeObserver();
+
+private:
+ template <typename T, size_t N>
+ 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 <typename ...Args>
+ static void Debug(Event event, Args&& ...args) {
+ Record(EventSeverity::Debug, event, ::std::forward<Args>(args)...);
+ }
+
+ template <typename ...Args>
+ static void Info(Event event, Args&& ...args) {
+ Record(EventSeverity::Info, event, ::std::forward<Args>(args)...);
+ }
+
+ template <typename ...Args>
+ static void Warning(Event event, Args&& ...args) {
+ Record(EventSeverity::Warning, event, ::std::forward<Args>(args)...);
+ }
+
+ template <typename ...Args>
+ static void Error(Event event, Args&& ...args) {
+ Record(EventSeverity::Error, event, ::std::forward<Args>(args)...);
+ }
+
+ template <typename ...Args>
+ 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>(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 <memory>
+#include <string>
+
+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