2003-10-14 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-internals.c
index de98c1f..44f3ff4 100644 (file)
@@ -174,6 +174,8 @@ _dbus_warn (const char *format,
   va_end (args);
 }
 
+static dbus_bool_t verbose_initted = FALSE;
+
 /**
  * Prints a warning message to stderr
  * if the user has enabled verbose mode.
@@ -188,8 +190,8 @@ _dbus_verbose_real (const char *format,
 {
   va_list args;
   static dbus_bool_t verbose = TRUE;
-  static dbus_bool_t initted = FALSE;
-
+  static dbus_bool_t need_pid = TRUE;
+  
   /* things are written a bit oddly here so that
    * in the non-verbose case we just have the one
    * conditional and return immediately.
@@ -197,13 +199,26 @@ _dbus_verbose_real (const char *format,
   if (!verbose)
     return;
   
-  if (!initted)
+  if (!verbose_initted)
     {
       verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
-      initted = TRUE;
+      verbose_initted = TRUE;
       if (!verbose)
         return;
     }
+
+  if (need_pid)
+    {
+      int len;
+      
+      fprintf (stderr, "%lu: ", _dbus_getpid ());
+
+      len = strlen (format);
+      if (format[len-1] == '\n')
+        need_pid = TRUE;
+      else
+        need_pid = FALSE;
+    }
   
   va_start (args, format);
   vfprintf (stderr, format, args);
@@ -211,6 +226,18 @@ _dbus_verbose_real (const char *format,
 }
 
 /**
+ * Reinitializes the verbose logging code, used
+ * as a hack in dbus-spawn.c so that a child
+ * process re-reads its pid
+ *
+ */
+void
+_dbus_verbose_reset_real (void)
+{
+  verbose_initted = FALSE;
+}
+
+/**
  * Duplicates a string. Result must be freed with
  * dbus_free(). Returns #NULL if memory allocation fails.
  * If the string to be duplicated is #NULL, returns #NULL.
@@ -221,7 +248,7 @@ _dbus_verbose_real (const char *format,
 char*
 _dbus_strdup (const char *str)
 {
-  int len;
+  size_t len;
   char *copy;
   
   if (str == NULL)
@@ -239,6 +266,29 @@ _dbus_strdup (const char *str)
 }
 
 /**
+ * Duplicates a block of memory. Returns
+ * #NULL on failure.
+ *
+ * @param mem memory to copy
+ * @param n_bytes number of bytes to copy
+ * @returns the copy
+ */
+void*
+_dbus_memdup (const void  *mem,
+              size_t       n_bytes)
+{
+  void *copy;
+
+  copy = dbus_malloc (n_bytes);
+  if (copy == NULL)
+    return NULL;
+
+  memcpy (copy, mem, n_bytes);
+  
+  return copy;
+}
+
+/**
  * Duplicates a string array. Result may be freed with
  * dbus_free_string_array(). Returns #NULL if memory allocation fails.
  * If the array to be duplicated is #NULL, returns #NULL.
@@ -339,6 +389,45 @@ _dbus_type_to_string (int type)
     }
 }
 
+/**
+ * Returns a string describing the given name.
+ *
+ * @param header_field the field to describe
+ * @returns a constant string describing the field
+ */
+const char *
+_dbus_header_field_to_string (int header_field)
+{
+  switch (header_field)
+    {
+    case DBUS_HEADER_FIELD_INVALID:
+      return "invalid";
+    case DBUS_HEADER_FIELD_PATH:
+      return "path";
+    case DBUS_HEADER_FIELD_INTERFACE:
+      return "interface";
+    case DBUS_HEADER_FIELD_MEMBER:
+      return "member";
+    case DBUS_HEADER_FIELD_ERROR_NAME:
+      return "error-name";
+    case DBUS_HEADER_FIELD_REPLY_SERIAL:
+      return "reply-serial";
+    case DBUS_HEADER_FIELD_SERVICE:
+      return "service";
+    case DBUS_HEADER_FIELD_SENDER_SERVICE:
+      return "sender-service";
+    default:
+      return "unknown";
+    }
+}
+
+#ifndef DBUS_DISABLE_CHECKS
+/** String used in _dbus_return_if_fail macro */
+const char _dbus_return_if_fail_warning_format[] =
+"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
+
 #ifndef DBUS_DISABLE_ASSERT
 /**
  * Internals of _dbus_assert(); it's a function
@@ -359,8 +448,8 @@ _dbus_real_assert (dbus_bool_t  condition,
 {
   if (!condition)
     {
-      _dbus_warn ("Assertion failed \"%s\" file \"%s\" line %d\n",
-                  condition_text, file, line);
+      _dbus_warn ("Assertion failed \"%s\" file \"%s\" line %d process %lu\n",
+                  condition_text, file, line, _dbus_getpid ());
       _dbus_abort ();
     }
 }
@@ -380,8 +469,8 @@ _dbus_real_assert_not_reached (const char *explanation,
                                const char *file,
                                int         line)
 {
-  _dbus_warn ("File \"%s\" line %d should not have been reached: %s\n",
-              file, line, explanation);
+  _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
+              file, line, _dbus_getpid (), explanation);
   _dbus_abort ();
 }
 #endif /* DBUS_DISABLE_ASSERT */
@@ -438,12 +527,14 @@ _dbus_test_oom_handling (const char             *description,
   
   _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
 
+  _dbus_verbose ("Running once to count mallocs\n");
+  
   if (!(* func) (data))
     return FALSE;
   
   approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
 
-  _dbus_verbose ("=================\n%s: about %d mallocs total\n=================\n",
+  _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
                  description, approx_mallocs);
 
   _dbus_set_fail_alloc_failures (1);
@@ -462,7 +553,7 @@ _dbus_test_oom_handling (const char             *description,
   if (!run_failing_each_malloc (approx_mallocs, description, func, data))
     return FALSE;
   
-  _dbus_verbose ("=================\n%s: all iterations passed\n=================\n",
+  _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
                  description);
 
   return TRUE;