From f1ca9b89e92de4d876dc5e7e85710c4d2dc87638 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Tue, 17 Oct 2006 20:52:13 +0000 Subject: 2006-10-17 Havoc Pennington * dbus/dbus-internals.c (_dbus_warn_check_failed): new function to be used for return_if_fail type warnings; prefixes the pid, and fatal by default. --- dbus/dbus-internals.c | 83 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 13 deletions(-) (limited to 'dbus/dbus-internals.c') diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c index ecddfb6f..2233fc68 100644 --- a/dbus/dbus-internals.c +++ b/dbus/dbus-internals.c @@ -192,9 +192,44 @@ const char _dbus_no_memory_message[] = "Not enough memory"; static dbus_bool_t warn_initted = FALSE; static dbus_bool_t fatal_warnings = FALSE; +static dbus_bool_t fatal_warnings_on_check_failed = TRUE; + +static void +init_warnings(void) +{ + if (!warn_initted) + { + const char *s; + s = _dbus_getenv ("DBUS_FATAL_WARNINGS"); + if (s && *s) + { + if (*s == '0') + { + fatal_warnings = FALSE; + fatal_warnings_on_check_failed = FALSE; + } + else if (*s == '1') + { + fatal_warnings = TRUE; + fatal_warnings_on_check_failed = TRUE; + } + else + { + fprintf(stderr, "DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'", + s); + } + } + + warn_initted = TRUE; + } +} /** - * Prints a warning message to stderr. + * Prints a warning message to stderr. Can optionally be made to exit + * fatally by setting DBUS_FATAL_WARNINGS, but this is rarely + * used. This function should be considered pretty much equivalent to + * fprintf(stderr). _dbus_warn_check_failed() on the other hand is + * suitable for use when a programming mistake has been made. * * @param format printf-style format string. */ @@ -205,20 +240,43 @@ _dbus_warn (const char *format, va_list args; if (!warn_initted) - { - const char *s; - s = _dbus_getenv ("DBUS_FATAL_WARNINGS"); - if (s && *s) - fatal_warnings = TRUE; + init_warnings (); + + va_start (args, format); + vfprintf (stderr, format, args); + va_end (args); - warn_initted = TRUE; + if (fatal_warnings) + { + fflush (stderr); + _dbus_abort (); } +} + +/** + * Prints a "critical" warning to stderr when an assertion fails; + * differs from _dbus_warn primarily in that it prefixes the pid and + * defaults to fatal. This should be used only when a programming + * error has been detected. (NOT for unavoidable errors that an app + * might handle - those should be returned as DBusError.) Calling this + * means "there is a bug" + */ +void +_dbus_warn_check_failed(const char *format, + ...) +{ + va_list args; + + if (!warn_initted) + init_warnings (); + + fprintf (stderr, "process %lu: ", _dbus_getpid ()); va_start (args, format); vfprintf (stderr, format, args); va_end (args); - if (fatal_warnings) + if (fatal_warnings_on_check_failed) { fflush (stderr); _dbus_abort (); @@ -664,10 +722,9 @@ _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str) * here. But in a production build, we want to be nice and loud about * this. */ - _dbus_warn ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n", - error.message); - _dbus_warn ("See the manual page for dbus-uuidgen to correct this issue.\n"); - _dbus_warn ("Continuing with a bogus made-up machine UUID, which may cause problems."); + _dbus_warn_check_failed ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n" + "See the manual page for dbus-uuidgen to correct this issue.\n", + error.message); #endif dbus_error_free (&error); @@ -722,7 +779,7 @@ _dbus_header_field_to_string (int header_field) #ifndef DBUS_DISABLE_CHECKS /** String used in _dbus_return_if_fail macro */ const char _dbus_return_if_fail_warning_format[] = -"%lu: arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n" +"arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n" "This is normally a bug in some application using the D-Bus library.\n"; #endif -- cgit