diff options
author | Ara Ayvazyan <Aivazyan@microsoft.com> | 2018-04-04 17:37:40 -0700 |
---|---|---|
committer | Ara Ayvazyan <Aivazyan@microsoft.com> | 2018-04-04 17:37:40 -0700 |
commit | e7995f213c90ec4e0b1159cb630d6ca9761ee35a (patch) | |
tree | bd0dca4d4fa870d8fb30564cfb6c2667253dca71 | |
parent | 586704b980b0eac843488386de4b8adf170bc94e (diff) |
Avoid low severity log message construction
-rw-r--r-- | include/grpc/support/log.h | 2 | ||||
-rw-r--r-- | src/core/lib/gpr/log.cc | 10 | ||||
-rw-r--r-- | src/core/lib/gpr/log_android.cc | 4 | ||||
-rw-r--r-- | src/core/lib/gpr/log_linux.cc | 4 | ||||
-rw-r--r-- | src/core/lib/gpr/log_posix.cc | 4 | ||||
-rw-r--r-- | src/core/lib/gpr/log_windows.cc | 5 |
6 files changed, 27 insertions, 2 deletions
diff --git a/include/grpc/support/log.h b/include/grpc/support/log.h index 2236703db3..7521453b80 100644 --- a/include/grpc/support/log.h +++ b/include/grpc/support/log.h @@ -61,6 +61,8 @@ GPRAPI const char* gpr_log_severity_string(gpr_log_severity severity); GPRAPI void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) GPR_PRINT_FORMAT_CHECK(4, 5); +GPRAPI int gpr_should_log(gpr_log_severity severity); + GPRAPI void gpr_log_message(const char* file, int line, gpr_log_severity severity, const char* message); diff --git a/src/core/lib/gpr/log.cc b/src/core/lib/gpr/log.cc index 72787ab724..01ef112fb3 100644 --- a/src/core/lib/gpr/log.cc +++ b/src/core/lib/gpr/log.cc @@ -44,10 +44,16 @@ const char* gpr_log_severity_string(gpr_log_severity severity) { GPR_UNREACHABLE_CODE(return "UNKNOWN"); } +int gpr_should_log(gpr_log_severity severity) { + return static_cast<gpr_atm>(severity) >= + gpr_atm_no_barrier_load(&g_min_severity_to_print) + ? 1 + : 0; +} + void gpr_log_message(const char* file, int line, gpr_log_severity severity, const char* message) { - if (static_cast<gpr_atm>(severity) < - gpr_atm_no_barrier_load(&g_min_severity_to_print)) { + if (gpr_should_log(severity) == 0) { return; } diff --git a/src/core/lib/gpr/log_android.cc b/src/core/lib/gpr/log_android.cc index 0d3ac0fe52..40ef4c640d 100644 --- a/src/core/lib/gpr/log_android.cc +++ b/src/core/lib/gpr/log_android.cc @@ -41,6 +41,10 @@ static android_LogPriority severity_to_log_priority(gpr_log_severity severity) { void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } char* message = NULL; va_list args; va_start(args, format); diff --git a/src/core/lib/gpr/log_linux.cc b/src/core/lib/gpr/log_linux.cc index e4417d9d5d..561276f0c2 100644 --- a/src/core/lib/gpr/log_linux.cc +++ b/src/core/lib/gpr/log_linux.cc @@ -44,6 +44,10 @@ static long gettid(void) { return syscall(__NR_gettid); } void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } char* message = nullptr; va_list args; va_start(args, format); diff --git a/src/core/lib/gpr/log_posix.cc b/src/core/lib/gpr/log_posix.cc index 6f93cdefcd..0acb225572 100644 --- a/src/core/lib/gpr/log_posix.cc +++ b/src/core/lib/gpr/log_posix.cc @@ -34,6 +34,10 @@ static intptr_t gettid(void) { return (intptr_t)pthread_self(); } void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } char buf[64]; char* allocated = nullptr; char* message = nullptr; diff --git a/src/core/lib/gpr/log_windows.cc b/src/core/lib/gpr/log_windows.cc index caaa973f5a..060be572b8 100644 --- a/src/core/lib/gpr/log_windows.cc +++ b/src/core/lib/gpr/log_windows.cc @@ -34,6 +34,11 @@ void gpr_log(const char* file, int line, gpr_log_severity severity, const char* format, ...) { + /* Avoid message construction if gpr_log_message won't log */ + if (gpr_should_log(severity) == 0) { + return; + } + char* message = NULL; va_list args; int ret; |