#include #include #include #include #include #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); } }