Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce log overhead when logging disabled #711

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package/android/src/main/cpp/AndroidLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include "MmkvLogger.h"
#include <android/log.h>

void MmkvLogger::log(const std::string& tag, const std::string& message) {
void MmkvLogger::log(const std::string& tag, const std::string& formatString, Args... args) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, not a template.

we can't even template this because our implementation is in two differnt spots (ios and android). so we might not be able to support 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
}
17 changes: 2 additions & 15 deletions package/cpp/MmkvLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,12 @@ class MmkvLogger {
private:
MmkvLogger() = delete;

private:
template <typename... Args>
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_t>(size_s);
std::unique_ptr<char[]> 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a template, you cannot use Args... here. This is why the C++ build is now failing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I understand, but I ended up not using this project for my work, so I don't have time to fully flesh out this patch. I was intending to simply illustrate some ways of avoiding the logging overhead and/or avoiding NSLog in release builds (which is discouraged)


template <typename... Args>
inline static void log(const std::string& tag, const std::string& formatString, Args&&... args) {
std::string formattedString = string_format(formatString, std::forward<Args>(args)...);
log(tag, formattedString);
log(tag, formatString, std::forward<Args>(args)...);
}
};
6 changes: 4 additions & 2 deletions package/ios/AppleLogger.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#import "MmkvLogger.h"
#import <Foundation/Foundation.h>

void MmkvLogger::log(const std::string& tag, const std::string& message) {
void MmkvLogger::log(const std::string& tag, const std::string& formatString, Args... args) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, not a template.

we can't even template this because our implementation is in two differnt spots (ios and android). so we might not be able to support 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
}