diff options
Diffstat (limited to 'src/reporting.c')
-rw-r--r-- | src/reporting.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/reporting.c b/src/reporting.c new file mode 100644 index 0000000..329d850 --- /dev/null +++ b/src/reporting.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <stdarg.h> +#include <unistd.h> +#include <sys/types.h> +#include <syslog.h> + +#include "reporting.h" + +int verbosity_level = REPORT_WARNING; +static int syslog_flag = 0; +/* for converting our own levels to syslog's levels: */ +static int log_levels[5] = { LOG_ALERT, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG }; + +void reporting_init(const char *program_name) +{ + openlog(program_name, LOG_PID, LOG_USER); + /* reporting to syslog is on by default if stderr is not printed at a terminal */ + if (!isatty(fileno(stderr))) + reporting_use_syslog(1); +} + +void reporting_use_syslog(int flag) +{ + syslog_flag = flag; +} + +void reporting_verbosity(int level) +{ + verbosity_level = level; +} + +void report_message(int level, const char *format, ...) +{ + va_list args; + va_start(args, format); + + if(syslog_flag || level == REPORT_ALERT) { + syslog(LOG_USER | log_levels[level], format, args); + /* va_list can only be used once */ + va_end(args); + va_start(args, format); + } + + vfprintf(stderr, format, args); + va_end(args); +} + +/* For messages which contain sensitive information, and should not be shown to + * all users: */ +void report_private_message(int level, const char *format, ...) +{ + if(level <= verbosity_level) + { + va_list args; + va_start(args, format); + + if(syslog_flag || level == REPORT_ALERT) { + syslog(LOG_AUTHPRIV | log_levels[level], format, args); + /* va_list can only be used once */ + va_end(args); + va_start(args, format); + } + + if (getuid() == 0) /* being run by root */ + vfprintf(stderr, format, args); + + va_end(args); + } +}
\ No newline at end of file |