failures.c revision 65497fd5c3efa66319a1486addb272413403653a
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen failures.c : Failure manager
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen Copyright (c) 2001-2002 Timo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen Permission is hereby granted, free of charge, to any person obtaining
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen a copy of this software and associated documentation files (the
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen "Software"), to deal in the Software without restriction, including
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen without limitation the rights to use, copy, modify, merge, publish,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen permit persons to whom the Software is furnished to do so, subject to
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen the following conditions:
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen The above copyright notice and this permission notice shall be
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen included in all copies or substantial portions of the Software.
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40a5aeebf6b4858b93f0ddff0bf12fba769cf903Timo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
40a5aeebf6b4858b93f0ddff0bf12fba769cf903Timo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_panic_handler(const char *format, va_list args)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_fatal_handler(int status, const char *format, va_list args)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_error_handler(const char *format, va_list args);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_warning_handler(const char *format, va_list args);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_info_handler(const char *format, va_list args);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen/* Initialize working defaults */
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc panic_handler __attr_noreturn__ = default_panic_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FatalFailureFunc fatal_handler __attr_noreturn__ = default_fatal_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc error_handler = default_error_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc warning_handler = default_warning_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc info_handler = default_info_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic FILE *log_fd = NULL, *log_info_fd = NULL;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic char *log_prefix = NULL, *log_stamp_format = NULL;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (strftime(str, sizeof(str), log_stamp_format, tm) > 0)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_panic_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_fatal_handler(int status, const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (fflush(log_fd) < 0 && status == FATAL_DEFAULT)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_error_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_warning_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_info_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_info_fd == NULL) log_info_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_info_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_fatal_status(int status, const char *format, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_panic_handler(FailureFunc func __attr_noreturn__)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_fatal_handler(FatalFailureFunc func __attr_noreturn__)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_panic_handler(const char *fmt, va_list args)
8e361d2906b0e44f7175a20981f8d2280645b58bTimo Sirainenvoid i_syslog_fatal_handler(int status, const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_error_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_warning_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_info_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_failure_syslog(const char *ident, int options, int facility)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_set_warning_handler(i_syslog_warning_handler);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void open_log_file(FILE **file, const char *path)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_failure_file(const char *path, const char *prefix)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_info_fd != NULL && log_info_fd != stderr) {
fad068d459cc7b04fedade4e0bb343be62e6d310Timo Sirainenvoid i_set_failure_timestamp_format(const char *fmt)