From a451784dd7ded72cfe2923cb9c0ff0b9da13f6b8 Mon Sep 17 00:00:00 2001 From: Isaac Levy Date: Thu, 1 Aug 2024 21:31:51 -0400 Subject: [PATCH 1/3] Prototype to reduce log overhead --- package/android/src/main/cpp/AndroidLogger.cpp | 4 ++-- package/cpp/MmkvLogger.h | 17 ++--------------- package/ios/AppleLogger.mm | 6 ++++-- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/package/android/src/main/cpp/AndroidLogger.cpp b/package/android/src/main/cpp/AndroidLogger.cpp index bd953b00..e4c6a28e 100644 --- a/package/android/src/main/cpp/AndroidLogger.cpp +++ b/package/android/src/main/cpp/AndroidLogger.cpp @@ -8,9 +8,9 @@ #include "MmkvLogger.h" #include -void MmkvLogger::log(const std::string& tag, const std::string& message) { +void MmkvLogger::log(const std::string& tag, const std::string& formatString, Args... args) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-security" - __android_log_print(ANDROID_LOG_INFO, tag.c_str(), message.c_str()); + __android_log_print(ANDROID_LOG_INFO, tag.c_str(), formatString.c_str(), args); #pragma clang diagnostic pop } diff --git a/package/cpp/MmkvLogger.h b/package/cpp/MmkvLogger.h index 2a524bae..72c3e59c 100644 --- a/package/cpp/MmkvLogger.h +++ b/package/cpp/MmkvLogger.h @@ -11,25 +11,12 @@ class MmkvLogger { private: MmkvLogger() = delete; -private: - template - static std::string string_format(const std::string& format, Args... args) { - int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0' - if (size_s <= 0) { - throw std::runtime_error("Failed to format string!"); - } - auto size = static_cast(size_s); - std::unique_ptr buf(new char[size]); - std::snprintf(buf.get(), size, format.c_str(), args...); - return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside - } public: - static void log(const std::string& tag, const std::string& message); + static void log(const std::string& tag, const std::string& formatString, Args... args); template inline static void log(const std::string& tag, const std::string& formatString, Args&&... args) { - std::string formattedString = string_format(formatString, std::forward(args)...); - log(tag, formattedString); + log(tag, formatString, std::forward(args)...); } }; diff --git a/package/ios/AppleLogger.mm b/package/ios/AppleLogger.mm index 80aee610..5deb0a41 100644 --- a/package/ios/AppleLogger.mm +++ b/package/ios/AppleLogger.mm @@ -8,9 +8,11 @@ #import "MmkvLogger.h" #import -void MmkvLogger::log(const std::string& tag, const std::string& message) { +void MmkvLogger::log(const std::string& tag, const std::string& formatString, Args... args) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-security" - NSLog(@"[%s]: %s", tag.c_str(), message.c_str()); +if (NSDebugEnabled) { + NSLog(@"[%s]: " + formatString.c_str(), tag.c_str(), args); +} #pragma clang diagnostic pop } From d5fce7b49c8e847f43a4a3ef32bb5d0265685973 Mon Sep 17 00:00:00 2001 From: Isaac Levy Date: Thu, 1 Aug 2024 21:43:50 -0400 Subject: [PATCH 2/3] Add debug guess for NSLog --- package/cpp/MmkvLogger.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package/cpp/MmkvLogger.h b/package/cpp/MmkvLogger.h index 72c3e59c..dea2ce71 100644 --- a/package/cpp/MmkvLogger.h +++ b/package/cpp/MmkvLogger.h @@ -17,6 +17,8 @@ class MmkvLogger { template inline static void log(const std::string& tag, const std::string& formatString, Args&&... args) { +#ifdef DEBUG log(tag, formatString, std::forward(args)...); +#endif } }; From 3ff32d813ce10c2efc0c35c14058bbef8672f3b5 Mon Sep 17 00:00:00 2001 From: Isaac Levy Date: Thu, 1 Aug 2024 21:45:14 -0400 Subject: [PATCH 3/3] Update AppleLogger.mm --- package/ios/AppleLogger.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package/ios/AppleLogger.mm b/package/ios/AppleLogger.mm index 5deb0a41..a9a2c99b 100644 --- a/package/ios/AppleLogger.mm +++ b/package/ios/AppleLogger.mm @@ -11,8 +11,8 @@ void MmkvLogger::log(const std::string& tag, const std::string& formatString, Args... args) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-security" -if (NSDebugEnabled) { +#ifdef DEBUG NSLog(@"[%s]: " + formatString.c_str(), tag.c_str(), args); -} +#endif #pragma clang diagnostic pop }