From fbec72cd47a30788d1d5cf1df540c8f5bcf5613d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 11 Jul 2014 16:25:20 -0700 Subject: allow formatted messages --- common/nslog_log.hpp | 1 + common/nslog_log.mm | 13 +++++++++++++ common/stderr_log.cpp | 9 +++++++++ common/stderr_log.hpp | 1 + 4 files changed, 24 insertions(+) (limited to 'common') diff --git a/common/nslog_log.hpp b/common/nslog_log.hpp index d4ea6f6eaa..9f2f3aee7b 100644 --- a/common/nslog_log.hpp +++ b/common/nslog_log.hpp @@ -10,6 +10,7 @@ public: inline ~NSLogBackend() = 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); }; diff --git a/common/nslog_log.mm b/common/nslog_log.mm index eed0810aee..ad685ea8dc 100644 --- a/common/nslog_log.mm +++ b/common/nslog_log.mm @@ -11,6 +11,19 @@ void NSLogBackend::record(EventSeverity severity, Event event, const std::string encoding:NSUTF8StringEncoding]); } + +void NSLogBackend::record(EventSeverity severity, Event event, const char* format, ...) { + va_list args; + va_start(args, format); + const int len = vsnprintf(NULL, 0, format, args); + va_end(args); + std::unique_ptr buffer(new char[len + 1]); + va_start(args, format); + vsnprintf(buffer.get(), len + 1, format, args); + va_end(args); + NSLog(@"[%s] %s: %s", stringifyEventSeverity(severity), stringifyEvent(event), buffer.get()); +} + void NSLogBackend::record(EventSeverity severity, Event event, int64_t code) { NSLog(@"[%s] %s: (%lld)", stringifyEventSeverity(severity), stringifyEvent(event), code); } diff --git a/common/stderr_log.cpp b/common/stderr_log.cpp index b3b1fea333..18ad9b3f10 100644 --- a/common/stderr_log.cpp +++ b/common/stderr_log.cpp @@ -8,6 +8,15 @@ void StderrLogBackend::record(EventSeverity severity, Event event, const std::st std::cerr << "[" << severity << "] " << event << ": " << msg << std::endl; } +void StderrLogBackend::record(EventSeverity severity, Event event, const char* format, ...) { + std::cerr << "[" << severity << "] " << event << ": "; + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + std::cerr << std::endl; +} + void StderrLogBackend::record(EventSeverity severity, Event event, int64_t code) { std::cerr << "[" << severity << "] " << event << ": (" << code << ")" << std::endl; } diff --git a/common/stderr_log.hpp b/common/stderr_log.hpp index 32b6850cb7..e2492d3af5 100644 --- a/common/stderr_log.hpp +++ b/common/stderr_log.hpp @@ -10,6 +10,7 @@ public: inline ~StderrLogBackend() = 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); }; -- cgit v1.2.1