From 51bc265db341f1d98b4fd46be80241771f854eb5 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 9 Mar 2015 16:15:18 +0200 Subject: [PATCH] Make the logging system static No initialization is needed anymore and we can use the logging functions safely at any point of the code (threading is not handled though, so you might get multiplexed messages if you log from two threads simultaneously). --- android/cpp/jni.cpp | 3 -- bin/render.cpp | 14 +-------- include/mbgl/platform/android/log_android.hpp | 24 -------------- include/mbgl/platform/darwin/log_nslog.hpp | 18 ----------- include/mbgl/platform/default/log_stderr.hpp | 18 ----------- include/mbgl/platform/log.hpp | 31 ++++++------------- linux/main.cpp | 4 +-- macosx/main.mm | 4 +-- platform/android/log_android.cpp | 10 ++++-- platform/darwin/log_nslog.mm | 6 ++-- platform/default/log_stderr.cpp | 4 +-- platform/ios/MGLMapView.mm | 6 +--- src/mbgl/platform/log.cpp | 12 ++++--- 13 files changed, 33 insertions(+), 121 deletions(-) delete mode 100644 include/mbgl/platform/android/log_android.hpp delete mode 100644 include/mbgl/platform/darwin/log_nslog.hpp delete mode 100644 include/mbgl/platform/default/log_stderr.hpp diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 1163c33b5..62e153e9b 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -827,8 +826,6 @@ jobject JNICALL nativeLatLngForPixel(JNIEnv *env, jobject obj, jlong nativeMapVi extern "C" { extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { - mbgl::Log::Set(); - mbgl::Log::Debug(mbgl::Event::JNI, "JNI_OnLoad"); JNIEnv *env = nullptr; diff --git a/bin/render.cpp b/bin/render.cpp index 71981b9d9..01f692909 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -5,15 +5,10 @@ #include #include +#include #include #include -#if __APPLE__ -#include -#else -#include -#endif - #pragma GCC diagnostic push #ifndef __clang__ #pragma GCC diagnostic ignored "-Wunused-local-typedefs" @@ -71,13 +66,6 @@ int main(int argc, char *argv[]) { using namespace mbgl; - -#if __APPLE__ - Log::Set(); -#else - Log::Set(); -#endif - mbgl::SQLiteCache cache(cache_file); mbgl::DefaultFileSource fileSource(&cache); diff --git a/include/mbgl/platform/android/log_android.hpp b/include/mbgl/platform/android/log_android.hpp deleted file mode 100644 index 94d90a9b3..000000000 --- a/include/mbgl/platform/android/log_android.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef MBGL_PLATFORM_ANDROID_LOG_ANDROID -#define MBGL_PLATFORM_ANDROID_LOG_ANDROID - -#include - -namespace mbgl { - -class AndroidLogBackend : public LogBackend { -private: - int severityToPriority(EventSeverity severity); - -public: - inline ~AndroidLogBackend() = default; - - void record(EventSeverity severity, Event event, const std::string &msg); - void record(EventSeverity severity, Event event, const char* format, ...); - void record(EventSeverity severity, Event event, int64_t code); - void record(EventSeverity severity, Event event, int64_t code, const std::string &msg); -}; - - -} - -#endif diff --git a/include/mbgl/platform/darwin/log_nslog.hpp b/include/mbgl/platform/darwin/log_nslog.hpp deleted file mode 100644 index 5b2ed0411..000000000 --- a/include/mbgl/platform/darwin/log_nslog.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MBGL_COMMON_NSLOG_LOG -#define MBGL_COMMON_NSLOG_LOG - -#include - -namespace mbgl { - -class NSLogBackend : public LogBackend { -public: - inline ~NSLogBackend() = default; - - void record(EventSeverity severity, Event event, int64_t code, const std::string &msg); -}; - - -} - -#endif diff --git a/include/mbgl/platform/default/log_stderr.hpp b/include/mbgl/platform/default/log_stderr.hpp deleted file mode 100644 index e07357593..000000000 --- a/include/mbgl/platform/default/log_stderr.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MBGL_COMMON_STDERR_LOG -#define MBGL_COMMON_STDERR_LOG - -#include - -namespace mbgl { - -class StderrLogBackend : public LogBackend { -public: - inline ~StderrLogBackend() = default; - - void record(EventSeverity severity, Event event, int64_t code, const std::string &msg) override; -}; - - -} - -#endif diff --git a/include/mbgl/platform/log.hpp b/include/mbgl/platform/log.hpp index e5431a07c..e6b8f17b6 100644 --- a/include/mbgl/platform/log.hpp +++ b/include/mbgl/platform/log.hpp @@ -10,17 +10,6 @@ namespace mbgl { -class LogBackend { -public: - virtual inline ~LogBackend() = default; - - void record(EventSeverity severity, Event event, const std::string &msg); - void record(EventSeverity severity, Event event, const char* format, ...); - void record(EventSeverity severity, Event event, int64_t code); - - virtual void record(EventSeverity severity, Event event, int64_t code, const std::string &msg) = 0; -}; - class Log { private: template @@ -54,20 +43,20 @@ public: if (!includes(severity, disabledEventSeverities) && !includes(event, disabledEvents) && !includes({ severity, event }, disabledEventPermutations)) { - if (Backend) { - Backend->record(severity, event, ::std::forward(args)...); - } + record(severity, event, ::std::forward(args)...); } } - template - static inline const T &Set(Args&& ...args) { - Backend = util::make_unique(::std::forward(args)...); - return *reinterpret_cast(Backend.get()); - } - private: - static std::unique_ptr Backend; + 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, Event event, int64_t code, const std::string &msg); }; } diff --git a/linux/main.cpp b/linux/main.cpp index 6688905ea..6a2be43d7 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -1,9 +1,9 @@ #include #include +#include #include #include #include -#include #include #include @@ -26,8 +26,6 @@ void quit_handler(int) { } int main(int argc, char *argv[]) { - mbgl::Log::Set(); - int fullscreen_flag = 0; std::string style; diff --git a/macosx/main.mm b/macosx/main.mm index f59fbda6a..087544f7c 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -1,6 +1,6 @@ +#include #include #include -#include #include #include #include @@ -101,8 +101,6 @@ const std::string &defaultCacheDatabase() { } int main() { - mbgl::Log::Set(); - GLFWView view; mbgl::SQLiteCache cache(defaultCacheDatabase()); diff --git a/platform/android/log_android.cpp b/platform/android/log_android.cpp index e1bec3e0c..9de8232ba 100644 --- a/platform/android/log_android.cpp +++ b/platform/android/log_android.cpp @@ -1,4 +1,4 @@ -#include +#include #define __STDC_FORMAT_MACROS // NDK bug workaround: https://code.google.com/p/android/issues/detail?id=72349 #include @@ -7,7 +7,9 @@ namespace mbgl { -int AndroidLogBackend::severityToPriority(EventSeverity severity) { +namespace { + +int severityToPriority(EventSeverity severity) { switch(severity) { case EventSeverity::Debug: return ANDROID_LOG_DEBUG; @@ -26,7 +28,9 @@ int AndroidLogBackend::severityToPriority(EventSeverity severity) { } } -void AndroidLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) { +} // namespace + +void Log::platformRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) { __android_log_print(severityToPriority(severity), EventClass(event).c_str(), "(%" PRId64 ") %s", code, msg.c_str()); } diff --git a/platform/darwin/log_nslog.mm b/platform/darwin/log_nslog.mm index edcf41ebb..5d53ed812 100644 --- a/platform/darwin/log_nslog.mm +++ b/platform/darwin/log_nslog.mm @@ -1,11 +1,11 @@ -#include +#include #import namespace mbgl { -void NSLogBackend::record(EventSeverity severity, Event event, int64_t code, - const std::string &msg) { +void Log::platformRecord(EventSeverity severity, Event event, int64_t code, + const std::string &msg) { NSString *message = [[NSString alloc] initWithBytes:msg.data() length:msg.size() encoding:NSUTF8StringEncoding]; NSLog(@"[%s] %s: (%lld) %@", EventSeverityClass(severity).c_str(), EventClass(event).c_str(), diff --git a/platform/default/log_stderr.cpp b/platform/default/log_stderr.cpp index 891bfd57f..3305176d9 100644 --- a/platform/default/log_stderr.cpp +++ b/platform/default/log_stderr.cpp @@ -1,10 +1,10 @@ -#include +#include #include namespace mbgl { -void StderrLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) { +void Log::platformRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) { std::cerr << "[" << severity << "] " << event << ": (" << code << ") " << msg << std::endl; } diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 1ab1afcbb..f60d3a05f 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -1,6 +1,6 @@ #import "MGLMapView.h" -#import +#import #import #import @@ -181,10 +181,6 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; - (BOOL)commonInit { - // set logging backend - // - mbgl::Log::Set(); - // create context // _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; diff --git a/src/mbgl/platform/log.cpp b/src/mbgl/platform/log.cpp index 71463dc2f..4bbff8da5 100644 --- a/src/mbgl/platform/log.cpp +++ b/src/mbgl/platform/log.cpp @@ -4,13 +4,11 @@ namespace mbgl { -std::unique_ptr Log::Backend; - -void LogBackend::record(EventSeverity severity, Event event, const std::string &msg) { +void Log::record(EventSeverity severity, Event event, const std::string &msg) { record(severity, event, -1, msg); } -void LogBackend::record(EventSeverity severity, Event event, const char* format, ...) { +void Log::record(EventSeverity severity, Event event, const char* format, ...) { va_list args; va_start(args, format); char msg[4096]; @@ -20,8 +18,12 @@ void LogBackend::record(EventSeverity severity, Event event, const char* format, record(severity, event, -1, msg); } -void LogBackend::record(EventSeverity severity, Event event, int64_t code) { +void Log::record(EventSeverity severity, Event event, int64_t code) { record(severity, event, code, std::string()); } +void Log::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) { + platformRecord(severity, event, code, msg); +} + }