Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-internals.c
index 96302d7..fcea079 100644 (file)
@@ -20,6 +20,8 @@
  * 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"
@@ -30,6 +32,7 @@
 #include <stdlib.h>
 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 #include <windows.h>
+#include <mbstring.h>
 #endif
 
 /**
@@ -298,9 +301,6 @@ static dbus_bool_t verbose = TRUE;
 #include <pthread.h>
 #endif
 
-#ifdef DBUS_WIN
-#define inline
-#endif
 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 static char module_name[1024];
 #endif
@@ -310,20 +310,62 @@ _dbus_verbose_init (void)
 {
   if (!verbose_initted)
     {
-      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
-      GetModuleFileName(0,module_name,sizeof(module_name)-1);
-      p = strrchr(module_name,'.');
-      if (p)
-        *p ='\0';
-      p = strrchr(module_name,'\\');
-      if (p)
-        strcpy(module_name,p+1);
-      strcat(module_name,": ");
+      {
+        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;
+
+  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;
 }
 
 /**
@@ -347,7 +389,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;
@@ -379,17 +428,24 @@ _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);
+  OutputDebugStringA(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);
 
@@ -659,27 +715,13 @@ _dbus_create_uuid_file_exclusively (const DBusString *filename,
       goto error;
     }
   
-  /* FIXME this is racy; we need a save_file_exclusively
-   * function. But in practice this should be fine for now.
-   *
-   * - first be sure we can create the file and it
-   *   doesn't exist by creating it empty with O_EXCL
-   * - then create it by creating a temporary file and
-   *   overwriting atomically with rename()
-   */
-  if (!_dbus_create_file_exclusively (filename, error))
-    goto error;
-
   if (!_dbus_string_append_byte (&encoded, '\n'))
     {
       _DBUS_SET_OOM (error);
       goto error;
     }
   
-  if (!_dbus_string_save_to_file (&encoded, filename, error))
-    goto error;
-
-  if (!_dbus_make_file_world_readable (filename, error))
+  if (!_dbus_string_save_to_file (&encoded, filename, TRUE, error))
     goto error;
 
   _dbus_string_free (&encoded);