Consistently include <config.h> in all C source files and never in header files.
[platform/upstream/dbus.git] / dbus / dbus-internals.c
index c99bee1..1ab6731 100644 (file)
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
+
+#include <config.h>
 #include "dbus-internals.h"
 #include "dbus-protocol.h"
 #include "dbus-marshal-basic.h"
 #include <stdarg.h>
 #include <string.h>
 #include <stdlib.h>
+#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
+#include <windows.h>
+#include <mbstring.h>
+#endif
 
 /**
  * @defgroup DBusInternals D-Bus secret internal implementation details
  * making up a different string every time and wasting
  * space.
  */
-const char _dbus_no_memory_message[] = "Not enough memory";
+const char *_dbus_no_memory_message = "Not enough memory";
 
 static dbus_bool_t warn_initted = FALSE;
 static dbus_bool_t fatal_warnings = FALSE;
@@ -295,19 +301,75 @@ static dbus_bool_t verbose = TRUE;
 #include <pthread.h>
 #endif
 
-#ifdef DBUS_WIN
+#ifdef _MSC_VER
 #define inline
 #endif
+#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
+static char module_name[1024];
+#endif
 
 static inline void
 _dbus_verbose_init (void)
 {
   if (!verbose_initted)
     {
-      const char *p = _dbus_getenv ("DBUS_VERBOSE"); 
+      const char *p = _dbus_getenv ("DBUS_VERBOSE");
       verbose = p != NULL && *p == '1';
       verbose_initted = TRUE;
+#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
+      {
+        char *last_period, *last_slash;
+        GetModuleFileName(0,module_name,sizeof(module_name)-1);
+        last_period = _mbsrchr(module_name,'.');
+        if (last_period)
+          *last_period ='\0';
+        last_slash = _mbsrchr(module_name,'\\');
+        if (last_slash)
+          strcpy(module_name,last_slash+1);
+        strcat(module_name,": ");
+      }
+#endif
+    }
+}
+
+/** @def DBUS_IS_DIR_SEPARATOR(c)
+ * macro for checking if character c is a patch separator
+ * 
+ * @todo move to a header file so that others can use this too
+ */
+#ifdef DBUS_WIN 
+#define DBUS_IS_DIR_SEPARATOR(c) (c == '\\' || c == '/')
+#else
+#define DBUS_IS_DIR_SEPARATOR(c) (c == '/')
+#endif
+
+/** 
+ remove source root from file path 
+ the source root is determined by 
+*/ 
+static char *_dbus_file_path_extract_elements_from_tail(const char *file,int level)
+{
+  static int prefix = -1;
+  char *p;
+
+  if (prefix == -1) 
+    {
+      char *p = (char *)file + strlen(file);
+      int i = 0;
+      prefix = 0;
+      for (;p >= file;p--)
+        {
+          if (DBUS_IS_DIR_SEPARATOR(*p))
+            {
+              if (++i >= level) 
+                {
+                  prefix = p-file+1;
+                  break;
+                }
+           }
+        }
     }
+  return (char *)file+prefix;
 }
 
 /**
@@ -331,7 +393,14 @@ _dbus_is_verbose_real (void)
  * @param format printf-style format string.
  */
 void
+#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
+_dbus_verbose_real (const char *file, 
+                    const int line, 
+                    const char *function, 
+                    const char *format,
+#else
 _dbus_verbose_real (const char *format,
+#endif
                     ...)
 {
   va_list args;
@@ -345,6 +414,7 @@ _dbus_verbose_real (const char *format,
   if (!_dbus_is_verbose_real())
     return;
 
+#ifndef DBUS_USE_OUTPUT_DEBUG_STRING
   /* Print out pid before the line */
   if (need_pid)
     {
@@ -354,7 +424,7 @@ _dbus_verbose_real (const char *format,
       fprintf (stderr, "%lu: ", _dbus_pid_for_log ());
 #endif
     }
-      
+#endif
 
   /* Only print pid again if the next line is a new line */
   len = strlen (format);
@@ -362,12 +432,29 @@ _dbus_verbose_real (const char *format,
     need_pid = TRUE;
   else
     need_pid = FALSE;
-  
+
   va_start (args, format);
+#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
+  {
+  char buf[1024];
+  strcpy(buf,module_name);
+#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
+  sprintf (buf+strlen(buf), "[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
+#endif
+  vsprintf (buf+strlen(buf),format, args);
+  va_end (args);
+  OutputDebugString(buf);
+  }
+#else
+#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
+  fprintf (stderr, "[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
+#endif
+
   vfprintf (stderr, format, args);
   va_end (args);
 
   fflush (stderr);
+#endif
 }
 
 /**
@@ -543,8 +630,18 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
   DBusString decoded;
   int end;
   
-  _dbus_string_init (&contents);
-  _dbus_string_init (&decoded);
+  if (!_dbus_string_init (&contents))
+    {
+      _DBUS_SET_OOM (error);
+      return FALSE;
+    }
+
+  if (!_dbus_string_init (&decoded))
+    {
+      _dbus_string_free (&contents);
+      _DBUS_SET_OOM (error);
+      return FALSE;
+    }
   
   if (!_dbus_file_get_contents (&contents, filename, error))
     goto error;
@@ -608,7 +705,11 @@ _dbus_create_uuid_file_exclusively (const DBusString *filename,
 {
   DBusString encoded;
 
-  _dbus_string_init (&encoded);
+  if (!_dbus_string_init (&encoded))
+    {
+      _DBUS_SET_OOM (error);
+      return FALSE;
+    }
 
   _dbus_generate_uuid (uuid);
   
@@ -783,7 +884,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[] =
+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