-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,25 +11,14 @@ 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a template, you cannot use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
#ifdef DEBUG | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is redundant w/ the directive in iOS. It might be more correct to just guard iOS which has no concept of log levels, and leave Android based on log level. Or you could choose to have the directive here to have more similar behavior between iOS and Android |
||
log(tag, formatString, std::forward<Args>(args)...); | ||
#endif | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wformat-security" | ||
NSLog(@"[%s]: %s", tag.c_str(), message.c_str()); | ||
#ifdef DEBUG | ||
NSLog(@"[%s]: " + formatString.c_str(), tag.c_str(), args); | ||
#endif | ||
#pragma clang diagnostic pop | ||
} |
There was a problem hiding this comment.
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...
.