Commit a7a2e3de authored by Sergey Lyubka's avatar Sergey Lyubka Committed by Cesanta Bot

Introduce log filter

PUBLISHED_FROM=b8f538f87e034452032284d45b78090338e87f72
parent cae0a1cb
...@@ -223,36 +223,34 @@ enum cs_log_level { ...@@ -223,36 +223,34 @@ enum cs_log_level {
_LL_MAX = 5, _LL_MAX = 5,
}; };
/* Set log level. */
void cs_log_set_level(enum cs_log_level level); void cs_log_set_level(enum cs_log_level level);
/* Set log filter. NULL (a default) logs everything. */
void cs_log_set_filter(char *source_file_name);
int cs_log_print_prefix(enum cs_log_level level, const char *func,
const char *filename);
extern enum cs_log_level cs_log_threshold;
#if CS_ENABLE_STDIO #if CS_ENABLE_STDIO
void cs_log_set_file(FILE *file); void cs_log_set_file(FILE *file);
extern enum cs_log_level cs_log_threshold;
void cs_log_print_prefix(enum cs_log_level level, const char *func);
void cs_log_printf(const char *fmt, ...) void cs_log_printf(const char *fmt, ...)
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__((format(printf, 1, 2))) __attribute__((format(printf, 1, 2)))
#endif #endif
; ;
#define LOG(l, x) \ #define LOG(l, x) \
do { \ do { \
if (cs_log_threshold >= l) { \ if (cs_log_print_prefix(l, __func__, __FILE__)) cs_log_printf x; \
cs_log_print_prefix(l, __func__); \
cs_log_printf x; \
} \
} while (0) } while (0)
#ifndef CS_NDEBUG #ifndef CS_NDEBUG
#define DBG(x) \ #define DBG(x) LOG(LL_VERBOSE_DEBUG, x)
do { \
if (cs_log_threshold >= LL_VERBOSE_DEBUG) { \
cs_log_print_prefix(LL_VERBOSE_DEBUG, __func__); \
cs_log_printf x; \
} \
} while (0)
#else /* NDEBUG */ #else /* NDEBUG */
...@@ -287,6 +285,7 @@ void cs_log_printf(const char *fmt, ...) ...@@ -287,6 +285,7 @@ void cs_log_printf(const char *fmt, ...)
#include <string.h> #include <string.h>
/* Amalgamated: #include "common/cs_time.h" */ /* Amalgamated: #include "common/cs_time.h" */
/* Amalgamated: #include "common/str_util.h" */
enum cs_log_level cs_log_threshold WEAK = enum cs_log_level cs_log_threshold WEAK =
#if CS_ENABLE_DEBUG #if CS_ENABLE_DEBUG
...@@ -295,6 +294,9 @@ enum cs_log_level cs_log_threshold WEAK = ...@@ -295,6 +294,9 @@ enum cs_log_level cs_log_threshold WEAK =
LL_ERROR; LL_ERROR;
#endif #endif
static char *s_filter_pattern = NULL;
static size_t s_filter_pattern_len;
#if CS_ENABLE_STDIO #if CS_ENABLE_STDIO
FILE *cs_log_file WEAK = NULL; FILE *cs_log_file WEAK = NULL;
...@@ -305,9 +307,30 @@ double cs_log_ts WEAK; ...@@ -305,9 +307,30 @@ double cs_log_ts WEAK;
enum cs_log_level cs_log_cur_msg_level WEAK = LL_NONE; enum cs_log_level cs_log_cur_msg_level WEAK = LL_NONE;
void cs_log_print_prefix(enum cs_log_level level, const char *func) WEAK; void cs_log_set_filter(char *str) WEAK;
void cs_log_print_prefix(enum cs_log_level level, const char *func) { void cs_log_set_filter(char *str) {
free(s_filter_pattern);
if (str != NULL) {
s_filter_pattern = strdup(str);
s_filter_pattern_len = strlen(str);
} else {
s_filter_pattern = NULL;
s_filter_pattern_len = 0;
}
};
int cs_log_print_prefix(enum cs_log_level, const char *, const char *) WEAK;
int cs_log_print_prefix(enum cs_log_level level, const char *func,
const char *filename) {
char prefix[21]; char prefix[21];
if (level > cs_log_threshold) return 0;
if (s_filter_pattern != NULL &&
mg_match_prefix(s_filter_pattern, s_filter_pattern_len, func) < 0 &&
mg_match_prefix(s_filter_pattern, s_filter_pattern_len, filename) < 0) {
return 0;
}
strncpy(prefix, func, 20); strncpy(prefix, func, 20);
prefix[20] = '\0'; prefix[20] = '\0';
if (cs_log_file == NULL) cs_log_file = stderr; if (cs_log_file == NULL) cs_log_file = stderr;
...@@ -320,6 +343,7 @@ void cs_log_print_prefix(enum cs_log_level level, const char *func) { ...@@ -320,6 +343,7 @@ void cs_log_print_prefix(enum cs_log_level level, const char *func) {
cs_log_ts = now; cs_log_ts = now;
} }
#endif #endif
return 1;
} }
void cs_log_printf(const char *fmt, ...) WEAK; void cs_log_printf(const char *fmt, ...) WEAK;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment