From 7477d0c9dfcf4d7e354e05f79efe4b3bc63a2533 Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Fri, 7 Nov 2014 23:46:28 +1100 Subject: [PATCH] Finish android log backend --- android/cpp/NativeMapView.cpp | 7 +-- include/mbgl/platform/android/log_android.hpp | 24 +++++++++ platform/android/log_android.cpp | 53 ++++++++++++++----- 3 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 include/mbgl/platform/android/log_android.hpp diff --git a/android/cpp/NativeMapView.cpp b/android/cpp/NativeMapView.cpp index 23badd2c6..a7bf2aafa 100644 --- a/android/cpp/NativeMapView.cpp +++ b/android/cpp/NativeMapView.cpp @@ -1,10 +1,9 @@ -//#include - #include #include #include +#include #include "log.h" @@ -50,9 +49,7 @@ NativeMapView::NativeMapView(JNIEnv* env, jobject obj_, return; } - // TODO replace all printfs in map code with android logging - //freopen("/sdcard/stdout.txt", "w", stdout); // NOTE: can't use till NDK fix the stdout macro bug - //freopen("/sdcard/stderr.txt", "w", stderr); + mbgl::Log::Set(); view = new MBGLView(this); map = new mbgl::Map(*view); diff --git a/include/mbgl/platform/android/log_android.hpp b/include/mbgl/platform/android/log_android.hpp new file mode 100644 index 000000000..12ed3bd34 --- /dev/null +++ b/include/mbgl/platform/android/log_android.hpp @@ -0,0 +1,24 @@ +#ifndef MBGL_PLATFORM_ANDROID_LOG_ANDROID +#define MBGL_PLATFORM_ANDROID_LOG_ANDROID + +#include + +namespace mbgl { + +class AndroidLogBackend : public LogBackend { +private: + int severity_to_priority(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/platform/android/log_android.cpp b/platform/android/log_android.cpp index 0daf3f326..8a7e9df51 100644 --- a/platform/android/log_android.cpp +++ b/platform/android/log_android.cpp @@ -1,32 +1,59 @@ -#include +#include #include #include +#define __STDC_FORMAT_MACROS // NDK bug workaround: https://code.google.com/p/android/issues/detail?id=72349 +#include + +#include namespace mbgl { -// TODO use android logging +int AndroidLogBackend::severity_to_priority(EventSeverity severity) { + switch(severity) { + case EventSeverity::Debug: + return ANDROID_LOG_DEBUG; -void StderrLogBackend::record(EventSeverity severity, Event event, const std::string &msg) { - std::cerr << "[" << severity << "] " << event << ": " << msg << std::endl; + case EventSeverity::Info: + return ANDROID_LOG_INFO; + + case EventSeverity::Warning: + return ANDROID_LOG_WARN; + + case EventSeverity::Error: + return ANDROID_LOG_ERROR; + + default: + return ANDROID_LOG_VERBOSE; + } } -void StderrLogBackend::record(EventSeverity severity, Event event, const char* format, ...) { - std::cerr << "[" << severity << "] " << event << ": "; +void AndroidLogBackend::record(EventSeverity severity, Event event, const std::string &msg) { + __android_log_print(severity_to_priority(severity), EventClass(event).c_str(), "%s", msg.c_str()); +} + +void AndroidLogBackend::record(EventSeverity severity, Event event, const char* format, ...) { va_list args; va_start(args, format); - vfprintf(stderr, format, args); + + const int len = vsnprintf(nullptr, 0, format, args) + 1; + char* buf = new char[len]; + vsnprintf(buf, len, format, args); + va_end(args); - std::cerr << std::endl; + + __android_log_print(severity_to_priority(severity), EventClass(event).c_str(), "%s", buf); + + delete buf; + buf = nullptr; } -void StderrLogBackend::record(EventSeverity severity, Event event, int64_t code) { - std::cerr << "[" << severity << "] " << event << ": (" << code << ")" << std::endl; +void AndroidLogBackend::record(EventSeverity severity, Event event, int64_t code) { + __android_log_print(severity_to_priority(severity), EventClass(event).c_str(), "(%" PRId64 ")", code); } -void StderrLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) { - std::cerr << "[" << severity << "] " << event << ": (" << code << ") " << msg << std::endl; - +void AndroidLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) { + __android_log_print(severity_to_priority(severity), EventClass(event).c_str(), "(%" PRId64 ") %s", code, msg.c_str()); } }