From 3254cdc85c4a836cb57b2b56770e5a1ec26adc23 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Sun, 15 Mar 2015 03:20:53 +0200 Subject: [PATCH] Log the Environment ID and the Thread name To make it easier to debug, add the Environment ID and the Thread name to the log messages. For instance log messages from the TileParser will look like: [WARNING] {0}{TileWorker_15/18653/9486}[ParseTile]: some relevant warning message | | | | | | | +-> Component | | +--------------------> Thread name | +----------------------------------> Environment ID +------------------------------------------> Severity level Log messages that are not inside an Environment::Scope will work normally and will look like: [WARNING] [JNI]: some relevant warning message Fixes #882. --- include/mbgl/platform/log.hpp | 2 +- platform/android/log_android.cpp | 7 ++----- platform/darwin/log_nslog.mm | 6 ++---- platform/default/log_stderr.cpp | 4 ++-- src/mbgl/platform/log.cpp | 26 +++++++++++++++++++++++++- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/include/mbgl/platform/log.hpp b/include/mbgl/platform/log.hpp index de7f13171..5d287eb57 100644 --- a/include/mbgl/platform/log.hpp +++ b/include/mbgl/platform/log.hpp @@ -68,7 +68,7 @@ private: // 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); + static void platformRecord(EventSeverity severity, const std::string &msg); }; } diff --git a/platform/android/log_android.cpp b/platform/android/log_android.cpp index 9de8232ba..e5c8cfd81 100644 --- a/platform/android/log_android.cpp +++ b/platform/android/log_android.cpp @@ -1,8 +1,5 @@ #include -#define __STDC_FORMAT_MACROS // NDK bug workaround: https://code.google.com/p/android/issues/detail?id=72349 -#include - #include namespace mbgl { @@ -30,8 +27,8 @@ int severityToPriority(EventSeverity severity) { } // 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()); +void Log::platformRecord(EventSeverity severity, const std::string &msg) { + __android_log_print(severityToPriority(severity), "mbgl", "%s", msg.c_str()); } } diff --git a/platform/darwin/log_nslog.mm b/platform/darwin/log_nslog.mm index 5d53ed812..a2e31968a 100644 --- a/platform/darwin/log_nslog.mm +++ b/platform/darwin/log_nslog.mm @@ -4,12 +4,10 @@ namespace mbgl { -void Log::platformRecord(EventSeverity severity, Event event, int64_t code, - const std::string &msg) { +void Log::platformRecord(EventSeverity severity, 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(), - code, message); + NSLog(@"[%s] %@", EventSeverityClass(severity).c_str(), message); } } diff --git a/platform/default/log_stderr.cpp b/platform/default/log_stderr.cpp index 3305176d9..f7ef34184 100644 --- a/platform/default/log_stderr.cpp +++ b/platform/default/log_stderr.cpp @@ -4,8 +4,8 @@ namespace mbgl { -void Log::platformRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) { - std::cerr << "[" << severity << "] " << event << ": (" << code << ") " << msg << std::endl; +void Log::platformRecord(EventSeverity severity, const std::string &msg) { + std::cerr << "[" << severity << "] " << msg << std::endl; } } diff --git a/src/mbgl/platform/log.cpp b/src/mbgl/platform/log.cpp index e9712f727..d6cbc4fd1 100644 --- a/src/mbgl/platform/log.cpp +++ b/src/mbgl/platform/log.cpp @@ -1,6 +1,9 @@ #include +#include + #include +#include namespace mbgl { @@ -37,7 +40,28 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const std::s return; } - platformRecord(severity, event, code, msg); + std::stringstream logStream; + + if (Environment::inScope()) { + logStream << "{" << Environment::Get().getID() << "}"; + } + + const std::string threadName = Environment::threadName(); + if (!threadName.empty()) { + logStream << "{" << threadName << "}"; + } + + logStream << "[" << event << "]"; + + if (code >= 0) { + logStream << "(" << code << ")"; + } + + if (!msg.empty()) { + logStream << ": " << msg; + } + + platformRecord(severity, logStream.str()); } }