aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/support
diff options
context:
space:
mode:
authorGravatar ctiller <ctiller@google.com>2015-01-09 14:22:10 -0800
committerGravatar Nicolas Noble <nnoble@google.com>2015-01-12 11:22:02 -0800
commit0cd69562fd8c005361e3f32ac0d3f9d43b713827 (patch)
treebc440cfd32f15708ddc4a817afc9b7e486c2414c /src/core/support
parent033afb3879eba2d2e06091aa496b54066bddee07 (diff)
Allow logging mechanism to be overridden by applications.
This necessitated the removal of gpr_vlog to keep a clean interface. Change on 2015/01/09 by ctiller <ctiller@google.com> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=83634996
Diffstat (limited to 'src/core/support')
-rw-r--r--src/core/support/log.c23
-rw-r--r--src/core/support/log_android.c24
-rw-r--r--src/core/support/log_linux.c28
-rw-r--r--src/core/support/log_posix.c41
-rw-r--r--src/core/support/log_win32.c40
5 files changed, 114 insertions, 42 deletions
diff --git a/src/core/support/log.c b/src/core/support/log.c
index b9e2897efc..7f102efea8 100644
--- a/src/core/support/log.c
+++ b/src/core/support/log.c
@@ -34,6 +34,10 @@
#include <grpc/support/log.h>
#include <stdio.h>
+#include <string.h>
+
+extern void gpr_default_log(gpr_log_func_args *args);
+static gpr_log_func g_log_func = gpr_default_log;
const char *gpr_log_severity_string(gpr_log_severity severity) {
switch (severity) {
@@ -47,12 +51,15 @@ const char *gpr_log_severity_string(gpr_log_severity severity) {
return "UNKNOWN";
}
-void gpr_log(const char *file, int line, gpr_log_severity severity,
- const char *format, ...) {
- va_list args;
- va_start(args, format);
-
- gpr_vlog(file, line, severity, format, args);
-
- va_end(args);
+void gpr_log_message(const char *file, int line, gpr_log_severity severity,
+ const char *message) {
+ gpr_log_func_args lfargs;
+ memset(&lfargs, 0, sizeof(lfargs));
+ lfargs.file = file;
+ lfargs.line = line;
+ lfargs.severity = severity;
+ lfargs.message = message;
+ g_log_func(&lfargs);
}
+
+void gpr_set_log_function(gpr_log_func f) { g_log_func = f; }
diff --git a/src/core/support/log_android.c b/src/core/support/log_android.c
index 4c83e09914..11129e3e06 100644
--- a/src/core/support/log_android.c
+++ b/src/core/support/log_android.c
@@ -54,25 +54,31 @@ static android_LogPriority severity_to_log_priority(gpr_log_severity severity) {
return ANDROID_LOG_DEFAULT;
}
-void gpr_vlog(const char *file, int line, gpr_log_severity severity,
- const char *format, va_list args) {
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
+ char *message = NULL;
+ va_list args;
+ va_start(args, format);
+ vasprintf(&message, format, args);
+ va_end(args);
+ gpr_log_message(file, line, severity, message);
+ free(message);
+}
+
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
- char *prefix = NULL;
- char *suffix = NULL;
char *output = NULL;
- final_slash = strrchr(file, '/');
+ final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = file;
else
display_file = final_slash + 1;
- asprintf(&prefix, "%s:%d] ", display_file, line);
- vasprintf(&suffix, format, args);
- asprintf(&output, "%s%s", prefix, suffix);
+ asprintf(&prefix, "%s:%d] %s", display_file, args->line, args->message);
- __android_log_write(severity_to_log_priority(severity), "GRPC", output);
+ __android_log_write(severity_to_log_priority(args->severity), "GRPC", output);
/* allocated by asprintf => use free, not gpr_free */
free(prefix);
diff --git a/src/core/support/log_linux.c b/src/core/support/log_linux.c
index 322ff07dd9..f3b528bdc4 100644
--- a/src/core/support/log_linux.c
+++ b/src/core/support/log_linux.c
@@ -49,17 +49,27 @@
static long gettid() { return syscall(__NR_gettid); }
-void gpr_vlog(const char *file, int line, gpr_log_severity severity,
- const char *format, va_list args) {
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
+ char *message = NULL;
+ va_list args;
+ va_start(args, format);
+ vasprintf(&message, format, args);
+ va_end(args);
+ gpr_log_message(file, line, severity, message);
+ free(message);
+}
+
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
char time_buffer[64];
gpr_timespec now = gpr_now();
struct tm tm;
- final_slash = strrchr(file, '/');
+ final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
- display_file = file;
+ display_file = args->file;
else
display_file = final_slash + 1;
@@ -70,12 +80,10 @@ void gpr_vlog(const char *file, int line, gpr_log_severity severity,
strcpy(time_buffer, "error:strftime");
}
- flockfile(stderr);
- fprintf(stderr, "%s%s.%09d %7ld %s:%d] ", gpr_log_severity_string(severity),
- time_buffer, (int)(now.tv_nsec), gettid(), display_file, line);
- vfprintf(stderr, format, args);
- fputc('\n', stderr);
- funlockfile(stderr);
+ fprintf(stderr, "%s%s.%09d %7ld %s:%d] %s\n",
+ gpr_log_severity_string(args->severity), time_buffer,
+ (int)(now.tv_nsec), gettid(), display_file, args->line,
+ args->message);
}
#endif
diff --git a/src/core/support/log_posix.c b/src/core/support/log_posix.c
index b47c433cd7..0420570a3e 100644
--- a/src/core/support/log_posix.c
+++ b/src/core/support/log_posix.c
@@ -47,17 +47,40 @@
static long gettid() { return pthread_self(); }
-void gpr_vlog(const char *file, int line, gpr_log_severity severity,
- const char *format, va_list args) {
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
+ char buf[64];
+ char *allocated = NULL;
+ char *message = NULL;
+ int ret;
+ va_list args;
+ va_start(args, format);
+ ret = vsnprintf(buf, format, args);
+ va_end(args);
+ if (ret < 0) {
+ message = NULL;
+ } else if (ret <= sizeof(buf) - 1) {
+ message = buf;
+ } else {
+ message = allocated = gpr_malloc(ret + 1);
+ va_start(args, format);
+ vsnprintf(message, format, args);
+ va_end(args);
+ }
+ gpr_log_message(file, line, severity, message);
+ gpr_free(allocated);
+}
+
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
char time_buffer[64];
gpr_timespec now = gpr_now();
struct tm tm;
- final_slash = strrchr(file, '/');
+ final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
- display_file = file;
+ display_file = args->file;
else
display_file = final_slash + 1;
@@ -68,12 +91,10 @@ void gpr_vlog(const char *file, int line, gpr_log_severity severity,
strcpy(time_buffer, "error:strftime");
}
- flockfile(stderr);
- fprintf(stderr, "%s%s.%09d %7ld %s:%d] ", gpr_log_severity_string(severity),
- time_buffer, (int)(now.tv_nsec), gettid(), display_file, line);
- vfprintf(stderr, format, args);
- fputc('\n', stderr);
- funlockfile(stderr);
+ fprintf(stderr, "%s%s.%09d %7ld %s:%d] %s\n",
+ gpr_log_severity_string(args->severity), time_buffer,
+ (int)(now.tv_nsec), gettid(), display_file, args->line,
+ args->message);
}
#endif /* defined(GPR_POSIX_LOG) */
diff --git a/src/core/support/log_win32.c b/src/core/support/log_win32.c
index e6567dca7e..ae5f23a90d 100644
--- a/src/core/support/log_win32.c
+++ b/src/core/support/log_win32.c
@@ -39,12 +39,42 @@
#include <stdio.h>
#include <stdarg.h>
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *message) {
+ const char *message = NULL;
+ va_list args;
+ int ret;
+
+ /* Determine the length. */
+ va_start(args, format);
+ ret = _vscprintf(format, args);
+ va_end(args);
+ if (!(0 <= ret && ret < ~(size_t)0)) {
+ message = NULL;
+ } else {
+ /* Allocate a new buffer, with space for the NUL terminator. */
+ strp_buflen = (size_t)ret + 1;
+ message = gpr_malloc(strp_buflen);
+
+ /* Print to the buffer. */
+ va_start(args, format);
+ ret = vsnprintf_s(message, strp_buflen, _TRUNCATE, format, args);
+ va_end(args);
+ if (ret != strp_buflen - 1) {
+ /* This should never happen. */
+ gpr_free(message);
+ message = NULL;
+ }
+ }
+
+ gpr_log_message(file, line, severity, message);
+ gpr_free(message);
+}
+
/* Simple starter implementation */
-void gpr_vlog(const char *file, int line, gpr_log_severity severity,
- const char *format, va_list args) {
- fprintf(stderr, "%s %s:%d: ", gpr_log_severity_string(severity), file, line);
- vfprintf(stderr, format, args);
- fputc('\n', stderr);
+void gpr_default_log(gpr_log_func_args *args) {
+ fprintf(stderr, "%s %s:%d: %s\n", gpr_log_severity_string(severity),
+ args->file, args->line, args->message);
}
#endif