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

Conversation

isaacl
Copy link

@isaacl isaacl commented Aug 2, 2024

Avoid a string format unless logging is enabled.

Fixes #710.

Copy link
Author

@isaacl isaacl left a comment

Choose a reason for hiding this comment

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

Note: this doesn't compile, but it's reasonably close to a working impl


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
Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Owner

@mrousavy mrousavy left a comment

Choose a reason for hiding this comment

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

thanks for your PR - but C++ build now fails (see other comments), and it's not linted properly (run yarn lint-cpp)


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)

@@ -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....

@@ -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....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MmkvLogger creates log strings even if logging is disabled
2 participants