diff options
-rw-r--r-- | src/verbose.c | 38 | ||||
-rw-r--r-- | src/verbose.h | 44 |
2 files changed, 61 insertions, 21 deletions
diff --git a/src/verbose.c b/src/verbose.c index 2b009073..2d60dfca 100644 --- a/src/verbose.c +++ b/src/verbose.c @@ -21,22 +21,27 @@ #include "verbose.h" +#if !defined(DEFAULT_VERBOSITY) +# define DEFAULT_VERBOSITY Verbosity_Level_Warning +#endif + int verbosity = 1; +void (*verbose_observer)(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args); -#define LEVEL(x) ((x) < 0 ? 0 : (x) > 7 ? 7 : (x)) +#define CROP_LOGLEVEL(x) ((x) < Log_Level_Emergency ? Log_Level_Emergency : (x) > Log_Level_Debug ? Log_Level_Debug : (x)) #if defined(VERBOSE_WITH_SYSLOG) #include <syslog.h> -void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args) +static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args) { char *p; if (file == NULL || vasprintf(&p, fmt, args) < 0) - vsyslog(level, fmt, args); + vsyslog(loglevel, fmt, args); else { - syslog(LEVEL(level), "%s [%s:%d, function]", p, file, line, function); + syslog(CROP_LOGLEVEL(loglevel), "%s [%s:%d, function]", p, file, line, function); free(p); } } @@ -56,15 +61,15 @@ static const char *appname; static int appauthority; -void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args) +static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args) { char lino[20]; if (file == NULL) { - sd_journal_printv(level, fmt, args); + sd_journal_printv(loglevel, fmt, args); } else { sprintf(lino, "%d", line); - sd_journal_printv_with_location(level, file, lino, function, fmt, args); + sd_journal_printv_with_location(loglevel, file, lino, function, fmt, args); } } @@ -94,13 +99,13 @@ static const char *prefixes[] = { "<7> DEBUG" }; -void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args) +static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args) { int saverr = errno; int tty = isatty(fileno(stderr)); errno = saverr; - fprintf(stderr, "%s: ", prefixes[LEVEL(level)] + (tty ? 4 : 0)); + fprintf(stderr, "%s: ", prefixes[CROP_LOGLEVEL(loglevel)] + (tty ? 4 : 0)); vfprintf(stderr, fmt, args); if (file != NULL && (!tty || verbosity > 2)) fprintf(stderr, " [%s:%d,%s]\n", file, line, function); @@ -116,12 +121,23 @@ void verbose_set_name(const char *name, int authority) #endif -void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...) +void verbose(int loglevel, const char *file, int line, const char *function, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - vverbose(level, file, line, function, fmt, ap); + vverbose(loglevel, file, line, function, fmt, ap); va_end(ap); } +void vverbose(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args) +{ + if (verbose_observer) { + va_list ap; + va_copy(ap, args); + verbose_observer(loglevel, file, line, function, fmt, ap); + va_end(ap); + } + _vverbose_(loglevel, file, line, function, fmt, args); +} + diff --git a/src/verbose.h b/src/verbose.h index 6bedc323..8b605796 100644 --- a/src/verbose.h +++ b/src/verbose.h @@ -34,10 +34,19 @@ */ extern int verbosity; +enum verbosity_levels +{ + Verbosity_Level_Error = 0, + Verbosity_Level_Warning = 1, + Verbosity_Level_Notice = 1, + Verbosity_Level_Info = 2, + Verbosity_Level_Debug = 3 +}; + extern void verbose_set_name(const char *name, int authority); /* - Level is defined by syslog standard: + Log level is defined by syslog standard: KERN_EMERG 0 System is unusable KERN_ALERT 1 Action must be taken immediately KERN_CRIT 2 Critical conditions @@ -47,14 +56,29 @@ extern void verbose_set_name(const char *name, int authority); KERN_INFO 6 Informational KERN_DEBUG 7 Debug-level messages */ -extern void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf, 5, 6))); -extern void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args); -# define ERROR(...) do{if(verbosity>=0)verbose(3,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0) -# define WARNING(...) do{if(verbosity>=1)verbose(4,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0) -# define NOTICE(...) do{if(verbosity>=1)verbose(5,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0) -# define INFO(...) do{if(verbosity>=2)verbose(6,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0) -# define DEBUG(...) do{if(verbosity>=3)verbose(7,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0) -# define LOGUSER(app) verbose_set_name(app,0) -# define LOGAUTH(app) verbose_set_name(app,1) +enum log_levels +{ + Log_Level_Emergency = 0, + Log_Level_Alert = 1, + Log_Level_Critical = 2, + Log_Level_Error = 3, + Log_Level_Warning = 4, + Log_Level_Notice = 5, + Log_Level_Info = 6, + Log_Level_Debug = 7 +}; + +extern void verbose(int loglevel, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf, 5, 6))); +extern void vverbose(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args); + +# define _VERBOSE_(vlvl,llvl,...) do{ if (verbosity >= vlvl) verbose(llvl, __FILE__, __LINE__, __func__, __VA_ARGS__); } while(0) +# define ERROR(...) _VERBOSE_(Verbosity_Level_Error, Log_Level_Error, __VA_ARGS__) +# define WARNING(...) _VERBOSE_(Verbosity_Level_Warning, Log_Level_Warning, __VA_ARGS__) +# define NOTICE(...) _VERBOSE_(Verbosity_Level_Notice, Log_Level_Notice, __VA_ARGS__) +# define INFO(...) _VERBOSE_(Verbosity_Level_Info, Log_Level_Info, __VA_ARGS__) +# define DEBUG(...) _VERBOSE_(Verbosity_Level_Debug, Log_Level_Debug, __VA_ARGS__) +# define LOGUSER(app) verbose_set_name(app,0) +# define LOGAUTH(app) verbose_set_name(app,1) +extern void (*verbose_observer)(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args); |