Skip to content

Commit

Permalink
Merge pull request #36 from lyft/log-macro
Browse files Browse the repository at this point in the history
log: convert log functions to macros
  • Loading branch information
charlievieth authored Jan 25, 2019
2 parents b6d038f + 90971ea commit 848f91d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ void stats_set_log_level(enum statsrelay_log_level level) {
g_level = level;
}

void stats_vlog(const char *prefix,
enum statsrelay_log_level stats_get_log_level() {
return g_level;
}

static void stats_vlog(const char *prefix,
const char *format,
va_list ap) {
int fmt_len;
Expand Down Expand Up @@ -109,7 +113,7 @@ void stats_vlog(const char *prefix,
return;
}

void stats_debug_log(const char *format, ...) {
void noinline stats_debug_log_impl(const char *format, ...) {
if (g_level <= STATSRELAY_LOG_DEBUG) {
va_list args;
va_start(args, format);
Expand All @@ -118,7 +122,7 @@ void stats_debug_log(const char *format, ...) {
}
}

void stats_log(const char *format, ...) {
void stats_log_impl(const char *format, ...) {
if (g_level <= STATSRELAY_LOG_INFO) {
va_list args;
va_start(args, format);
Expand All @@ -127,7 +131,7 @@ void stats_log(const char *format, ...) {
}
}

void stats_error_log(const char *format, ...) {
void stats_error_log_impl(const char *format, ...) {
if (g_level <= STATSRELAY_LOG_ERROR) {
bool orig_verbose = g_verbose;
g_verbose = true;
Expand Down
25 changes: 20 additions & 5 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <stdarg.h>
#include <stdbool.h>

#define noinline __attribute__((__noinline__))
#define stats_printf __attribute__((__format__(__printf__, 1, 2)))
#define unlikely(x) __builtin_expect(!!(x), 0)

enum statsrelay_log_level {
STATSRELAY_LOG_DEBUG = 10,
STATSRELAY_LOG_INFO = 20,
Expand All @@ -19,20 +23,31 @@ void stats_log_syslog(bool syslog);

void stats_set_log_level(enum statsrelay_log_level level);

// variadic log function
void stats_vlog(const char *prefix, const char *format, va_list ap);
enum statsrelay_log_level stats_get_log_level() __attribute__((pure));

// log a message
void stats_log(const char *format, ...);
void noinline stats_log_impl(const char *format, ...) stats_printf;

// log a debug message
void stats_debug_log(const char *format, ...);
void noinline stats_debug_log_impl(const char *format, ...) stats_printf;

// log an error message
void stats_error_log(const char *format, ...);
void noinline stats_error_log_impl(const char *format, ...) stats_printf;

// finish logging; this ensures that the internally allocated buffer is freed;
// it can safely be called multiple times
void stats_log_end(void);

#define stats_log(format, ...) \
if (unlikely(stats_get_log_level() <= STATSRELAY_LOG_INFO)) \
stats_log_impl(format, ##__VA_ARGS__);

#define stats_error_log(format, ...) \
if (unlikely(stats_get_log_level() <= STATSRELAY_LOG_ERROR)) \
stats_error_log_impl(format, ##__VA_ARGS__);

#define stats_debug_log(format, ...) \
if (unlikely(stats_get_log_level() <= STATSRELAY_LOG_DEBUG)) \
stats_debug_log_impl(format, ##__VA_ARGS__);

#endif

0 comments on commit 848f91d

Please sign in to comment.