add nslog and stderr log backends

This commit is contained in:
Konstantin Käfer
2014-07-11 14:34:17 -07:00
parent f3c55eba9d
commit 7ffe4a7858
10 changed files with 122 additions and 19 deletions

20
common/nslog_log.hpp Normal file
View File

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

24
common/nslog_log.mm Normal file
View File

@@ -0,0 +1,24 @@
#include "nslog_log.hpp"
#import <Foundation/Foundation.h>
namespace llmr {
void NSLogBackend::record(EventSeverity severity, Event event, const std::string &msg) {
NSLog(@"[%s] %s: %@", stringifyEventSeverity(severity), stringifyEvent(event),
[[NSString alloc] initWithBytes:msg.data()
length:msg.size()
encoding:NSUTF8StringEncoding]);
}
void NSLogBackend::record(EventSeverity severity, Event event, int64_t code) {
NSLog(@"[%s] %s: (%lld)", stringifyEventSeverity(severity), stringifyEvent(event), code);
}
void NSLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) {
NSLog(@"[%s] %s: (%lld) %@", stringifyEventSeverity(severity), stringifyEvent(event), code,
[[NSString alloc] initWithBytes:msg.data()
length:msg.size()
encoding:NSUTF8StringEncoding]);
}
}

20
common/stderr_log.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "stderr_log.hpp"
#include <iostream>
namespace llmr {
void StderrLogBackend::record(EventSeverity severity, Event event, const std::string &msg) {
std::cerr << "[" << severity << "] " << event << ": " << msg << std::endl;
}
void StderrLogBackend::record(EventSeverity severity, Event event, int64_t code) {
std::cerr << "[" << severity << "] " << event << ": (" << code << ")" << std::endl;
}
void StderrLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) {
std::cerr << "[" << severity << "] " << event << ": (" << code << ") " << msg << std::endl;
}
}

20
common/stderr_log.hpp Normal file
View File

@@ -0,0 +1,20 @@
#ifndef LLMR_COMMON_STDERR_LOG
#define LLMR_COMMON_STDERR_LOG
#include <llmr/platform/log.hpp>
namespace llmr {
class StderrLogBackend : public LogBackend {
public:
inline ~StderrLogBackend() = default;
void record(EventSeverity severity, Event event, const std::string &msg);
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

@@ -15,9 +15,11 @@ enum class EventSeverity : uint8_t {
};
EventSeverity parseEventSeverity(const char *name);
const char *stringifyEventSeverity(EventSeverity eventSeverity);
::std::ostream& operator<<(::std::ostream& os, EventSeverity eventSeverity);
enum class Event : uint8_t {
Setup,
ParseStyle,
ParseTile,
Render,
@@ -25,6 +27,7 @@ enum class Event : uint8_t {
};
Event parseEvent(const char *name);
const char *stringifyEvent(Event event);
::std::ostream& operator<<(::std::ostream& os, Event event);
constexpr EventSeverity enabledEventSeverities[] = {

View File

@@ -8,6 +8,7 @@
#include "../common/settings_json.hpp"
#include "../common/glfw_view.hpp"
#include "../common/stderr_log.hpp"
GLFWView *view = nullptr;
@@ -22,6 +23,8 @@ void quit_handler(int) {
}
int main(int argc, char *argv[]) {
llmr::Log::Set<llmr::StderrLogBackend>();
int fullscreen_flag = 0;
const struct option long_options[] = {
@@ -63,7 +66,7 @@ int main(int argc, char *argv[]) {
// Set access token if present
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
if (token == nullptr) {
fprintf(stderr, "[WARNING] no access token set. mapbox.com tiles won't work.\n");
llmr::Log::Warning(llmr::Event::Setup, "no access token set. mapbox.com tiles won't work.");
} else {
map.setAccessToken(std::string(token));
}

View File

@@ -16,6 +16,8 @@
'../common/glfw_view.cpp',
'../common/foundation_request.h',
'../common/foundation_request.mm',
'../common/nslog_log.hpp',
'../common/nslog_log.mm',
],
'product_extension': 'app',
'mac_bundle': 1,

View File

@@ -1,9 +1,12 @@
#include "../common/settings_nsuserdefaults.hpp"
#include "../common/glfw_view.hpp"
#include "../common/nslog_log.hpp"
#import <Foundation/Foundation.h>
int main() {
llmr::Log::Set<llmr::NSLogBackend>();
GLFWView view;
llmr::Map map(view);
@@ -16,7 +19,7 @@ int main() {
// Set access token if present
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
if (token == nullptr) {
fprintf(stderr, "[WARNING] no access token set. mapbox.com tiles won't work.\n");
llmr::Log::Warning(llmr::Event::Setup, "no access token set. mapbox.com tiles won't work.");
} else {
map.setAccessToken(std::string(token));
}
@@ -28,8 +31,6 @@ int main() {
error:nil];
map.setStyleJSON((std::string)[json cStringUsingEncoding:[NSString defaultCStringEncoding]]);
// fprintf(stderr, "lon: %f, lat: %f, zoom: %f, angle: %f, debug: %d\n", settings.l)
int ret = view.run();
// Save settings

View File

@@ -13,18 +13,23 @@ EventSeverity parseEventSeverity(const char *name) {
return EventSeverity(-1);
}
::std::ostream& operator<<(::std::ostream& os, EventSeverity eventSeverity) {
const char *stringifyEventSeverity(EventSeverity eventSeverity) {
switch (eventSeverity) {
case EventSeverity::Debug: return os << "DEBUG";
case EventSeverity::Info: return os << "INFO";
case EventSeverity::Test: return os << "TEST";
case EventSeverity::Warning: return os << "WARNING";
case EventSeverity::Error: return os << "ERROR";
default: return os << "UNKNOWN";
case EventSeverity::Debug: return "DEBUG";
case EventSeverity::Info: return "INFO";
case EventSeverity::Test: return "TEST";
case EventSeverity::Warning: return "WARNING";
case EventSeverity::Error: return "ERROR";
default: return "UNKNOWN";
}
}
::std::ostream& operator<<(::std::ostream& os, EventSeverity eventSeverity) {
return os << stringifyEventSeverity(eventSeverity);
}
Event parseEvent(const char *name) {
if (strcmp(name, "Setup") == 0) return Event::Setup;
if (strcmp(name, "ParseStyle") == 0) return Event::ParseStyle;
if (strcmp(name, "ParseTile") == 0) return Event::ParseTile;
if (strcmp(name, "Render") == 0) return Event::Render;
@@ -32,15 +37,20 @@ Event parseEvent(const char *name) {
return Event(-1);
}
::std::ostream& operator<<(::std::ostream& os, Event event) {
switch (event) {
case Event::ParseStyle: return os << "ParseStyle";
case Event::ParseTile: return os << "ParseTile";
case Event::Render: return os << "Render";
case Event::HttpRequest: return os << "HttpRequest";
default: return os << "Unknown";
const char *stringifyEvent(Event event) {
switch (event) {
case Event::Setup: return "Setup";
case Event::ParseStyle: return "ParseStyle";
case Event::ParseTile: return "ParseTile";
case Event::Render: return "Render";
case Event::HttpRequest: return "HttpRequest";
default: return "Unknown";
}
}
::std::ostream& operator<<(::std::ostream& os, Event event) {
return os << stringifyEvent(event);
}
}