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).
This commit is contained in:
Thiago Marcos P. Santos
2015-03-09 16:15:18 +02:00
committed by Thiago Marcos P. Santos
parent 2948e7d121
commit 51bc265db3
13 changed files with 33 additions and 121 deletions

View File

@@ -15,7 +15,6 @@
#include <mbgl/android/jni.hpp>
#include <mbgl/android/native_map_view.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/platform/android/log_android.hpp>
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/storage/network_status.hpp>
@@ -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::AndroidLogBackend>();
mbgl::Log::Debug(mbgl::Event::JNI, "JNI_OnLoad");
JNIEnv *env = nullptr;

View File

@@ -5,15 +5,10 @@
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default/sqlite_cache.hpp>
#if __APPLE__
#include <mbgl/platform/darwin/log_nslog.hpp>
#else
#include <mbgl/platform/default/log_stderr.hpp>
#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<NSLogBackend>();
#else
Log::Set<StderrLogBackend>();
#endif
mbgl::SQLiteCache cache(cache_file);
mbgl::DefaultFileSource fileSource(&cache);

View File

@@ -1,24 +0,0 @@
#ifndef MBGL_PLATFORM_ANDROID_LOG_ANDROID
#define MBGL_PLATFORM_ANDROID_LOG_ANDROID
#include <mbgl/platform/log.hpp>
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

View File

@@ -1,18 +0,0 @@
#ifndef MBGL_COMMON_NSLOG_LOG
#define MBGL_COMMON_NSLOG_LOG
#include <mbgl/platform/log.hpp>
namespace mbgl {
class NSLogBackend : public LogBackend {
public:
inline ~NSLogBackend() = default;
void record(EventSeverity severity, Event event, int64_t code, const std::string &msg);
};
}
#endif

View File

@@ -1,18 +0,0 @@
#ifndef MBGL_COMMON_STDERR_LOG
#define MBGL_COMMON_STDERR_LOG
#include <mbgl/platform/log.hpp>
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

View File

@@ -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 <typename T, size_t N>
@@ -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>(args)...);
}
record(severity, event, ::std::forward<Args>(args)...);
}
}
template<typename T, typename ...Args>
static inline const T &Set(Args&& ...args) {
Backend = util::make_unique<T>(::std::forward<Args>(args)...);
return *reinterpret_cast<T *>(Backend.get());
}
private:
static std::unique_ptr<LogBackend> 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);
};
}

View File

@@ -1,9 +1,9 @@
#include <mbgl/mbgl.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/default/settings_json.hpp>
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/platform/default/log_stderr.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default/sqlite_cache.hpp>
@@ -26,8 +26,6 @@ void quit_handler(int) {
}
int main(int argc, char *argv[]) {
mbgl::Log::Set<mbgl::StderrLogBackend>();
int fullscreen_flag = 0;
std::string style;

View File

@@ -1,6 +1,6 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/darwin/settings_nsuserdefaults.hpp>
#include <mbgl/platform/darwin/log_nslog.hpp>
#include <mbgl/platform/darwin/Reachability.h>
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/storage/default_file_source.hpp>
@@ -101,8 +101,6 @@ const std::string &defaultCacheDatabase() {
}
int main() {
mbgl::Log::Set<mbgl::NSLogBackend>();
GLFWView view;
mbgl::SQLiteCache cache(defaultCacheDatabase());

View File

@@ -1,4 +1,4 @@
#include <mbgl/platform/android/log_android.hpp>
#include <mbgl/platform/log.hpp>
#define __STDC_FORMAT_MACROS // NDK bug workaround: https://code.google.com/p/android/issues/detail?id=72349
#include <cinttypes>
@@ -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());
}

View File

@@ -1,11 +1,11 @@
#include <mbgl/platform/darwin/log_nslog.hpp>
#include <mbgl/platform/log.hpp>
#import <Foundation/Foundation.h>
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(),

View File

@@ -1,10 +1,10 @@
#include <mbgl/platform/default/log_stderr.hpp>
#include <mbgl/platform/log.hpp>
#include <iostream>
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;
}

View File

@@ -1,6 +1,6 @@
#import "MGLMapView.h"
#import <mbgl/platform/darwin/log_nslog.hpp>
#import <mbgl/platform/log.hpp>
#import <mbgl/platform/gl.hpp>
#import <GLKit/GLKit.h>
@@ -181,10 +181,6 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
- (BOOL)commonInit
{
// set logging backend
//
mbgl::Log::Set<mbgl::NSLogBackend>();
// create context
//
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

View File

@@ -4,13 +4,11 @@
namespace mbgl {
std::unique_ptr<LogBackend> 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);
}
}