Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/glib/ChangeLog,v
retrieving revision 1.1572.2.48
diff -u -2 -r1.1572.2.48 ChangeLog
--- ChangeLog	17 Sep 2004 01:15:13 -0000	1.1572.2.48
+++ ChangeLog	22 Sep 2004 20:28:13 -0000
@@ -1,2 +1,13 @@
+2004-09-22  Tor Lillqvist  <tml@iki.fi>
+
+	* glib/gmessages.c: [Win32] Don't open a console window if
+	standard output or standard error are invalid (as they are for GUI
+	applications). These console windows that open up unexpectedly
+	have caused endless amounts of confusion among end-users.
+
+	Don't output the process id on Windows. Only output the program
+	name. If not set with g_set_prgname(), fetch the application
+	executable's name and use that.
+
 Thu Sep 16 18:15:32 2004  Manish Singh  <yosh@gimp.org>
 
Index: glib/gmessages.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gmessages.c,v
retrieving revision 1.60
diff -u -2 -r1.60 gmessages.c
--- glib/gmessages.c	22 Apr 2004 20:17:54 -0000	1.60
+++ glib/gmessages.c	22 Sep 2004 20:28:13 -0000
@@ -49,7 +49,4 @@
 #ifdef G_OS_WIN32
 #include <io.h>
-typedef FILE* GFileDescriptor;
-#else
-typedef gint GFileDescriptor;
 #endif
 
@@ -89,18 +86,37 @@
 #  include <windows.h>
 #  undef STRICT
-#  include <process.h>          /* For _getpid() */
 static gboolean win32_keep_fatal_message = FALSE;
 
+/* This function lifted from GLib HEAD */
+static guint
+g_win32_get_windows_version (void)
+{
+  static gboolean beenhere = FALSE;
+  static guint version;
+
+  if (!beenhere)
+    {
+      if (getenv ("G_WIN32_PRETEND_WIN9X"))
+	version = 0x80000004;
+      else
+	version = GetVersion ();
+      beenhere = TRUE;
+    }
+  return version;
+}
+
+#define G_WIN32_IS_NT_BASED() (g_win32_get_windows_version () < 0x80000000)
+#define G_WIN32_HAVE_WIDECHAR_API() (G_WIN32_IS_NT_BASED ())
+
 /* This default message will usually be overwritten. */
-/* Yes, a fixed size buffer is bad. So sue me. But g_error is never
- * with huge strings, is it?
+/* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
+ * called with huge strings, is it?
  */
 static gchar  fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
 static gchar *fatal_msg_ptr = fatal_msg_buf;
 
-/* Just use stdio. If we're out of memory, we're hosed anyway. */
 #undef write
 static inline int
-dowrite (GFileDescriptor fd,
+dowrite (int          fd,
 	 const void  *buf,
 	 unsigned int len)
@@ -114,6 +130,5 @@
     }
 
-  fwrite (buf, len, 1, fd);
-  fflush (fd);
+  write (fd, buf, len);
 
   return len;
@@ -121,56 +136,9 @@
 #define write(fd, buf, len) dowrite(fd, buf, len)
 
-static void
-ensure_stdout_valid (void)
-{
-  static gboolean alloc_console_called = FALSE;
-  HANDLE handle;
-
-  if (win32_keep_fatal_message)
-    return;
-
-  if (!alloc_console_called)
-    {
-      handle = (HANDLE) _get_osfhandle (fileno (stdout)); 
-  
-      if (handle == INVALID_HANDLE_VALUE)
-	{
-	  AllocConsole ();
-	  alloc_console_called = TRUE;
-	  freopen ("CONOUT$", "w", stdout);
-	}
-    }
-}
-
-static void
-ensure_stderr_valid (void)
-{
-  static gboolean alloc_console_called = FALSE;
-  HANDLE handle;
-
-  if (win32_keep_fatal_message)
-    return;
-
-  if (!alloc_console_called)
-    {
-      handle = (HANDLE) _get_osfhandle (fileno (stderr)); 
-
-      if (handle == INVALID_HANDLE_VALUE)
-	{
-	  AllocConsole ();
-	  alloc_console_called = TRUE;
-	  freopen ("CONOUT$", "w", stderr);
-	}
-    }
-}
-
-#else
-#define ensure_stdout_valid()	/* Define as empty */
-#define ensure_stderr_valid()
 #endif
 
 static void
-write_string (GFileDescriptor fd,
-	      const gchar    *string)
+write_string (int          fd,
+	      const gchar *string)
 {
   write (fd, string, strlen (string));
@@ -583,5 +551,4 @@
 	    {
 	      warned = TRUE;
-	      ensure_stderr_valid ();
 	      _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
 	    }
@@ -669,5 +636,5 @@
 #define	ALERT_LEVELS		(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
 
-static GFileDescriptor
+static int
 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
 		guint log_level)
@@ -718,18 +685,6 @@
 #ifdef G_OS_WIN32
   win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
-
-  if (to_stdout)
-    {
-      ensure_stdout_valid ();
-      return stdout;
-    }
-  else
-    {
-      ensure_stderr_valid ();
-      return stderr;
-    }
-#else
-  return to_stdout ? 1 : 2;
 #endif
+  return to_stdout ? 1 : 2;
 }
 
@@ -740,7 +695,10 @@
 			 gpointer       unused_data)
 {
-  gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
+  gchar level_prefix[STRING_BUFFER_SIZE];
+#ifndef G_OS_WIN32
+  gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
+#endif
   gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
-  GFileDescriptor fd;
+  int fd;
 
   /* we can not call _any_ GLib functions in this fallback handler,
@@ -755,5 +713,7 @@
     message = "(NULL) message";
 
+#ifndef G_OS_WIN32
   format_unsigned (pid_string, getpid (), 10);
+#endif
 
   if (log_domain)
@@ -761,7 +721,11 @@
   else
     write_string (fd, "\n** ");
+
+#ifndef G_OS_WIN32
   write_string (fd, "(process:");
   write_string (fd, pid_string);
   write_string (fd, "): ");
+#endif
+
   if (log_domain)
     {
@@ -847,5 +811,5 @@
   gchar level_prefix[STRING_BUFFER_SIZE], *string;
   GString *gstring;
-  GFileDescriptor fd;
+  int fd;
 
   /* we can be called externally with recursion for whatever reason */
@@ -870,8 +834,51 @@
       const gchar *prg_name = g_get_prgname ();
       
+#ifdef G_OS_WIN32
+      if (prg_name)
+	prg_name = g_strdup (prg_name);
+      else
+	{
+	  if (G_WIN32_HAVE_WIDECHAR_API ())
+	    {
+	      wchar_t buf[MAX_PATH+1];
+	      if (GetModuleFileNameW (GetModuleHandle (NULL),
+				      buf, G_N_ELEMENTS (buf)) > 0)
+		{
+		  gchar *utf8_buf = g_utf16_to_utf8 (buf, -1,
+						     NULL, NULL, NULL);
+		  if (utf8_buf)
+		    {
+		      prg_name = g_path_get_basename (utf8_buf);
+		      g_free (utf8_buf);
+		    }
+		}
+	    }
+	  else
+	    {
+	      gchar buf[MAX_PATH+1];
+	      if (GetModuleFileNameA (GetModuleHandle (NULL),
+				      buf, G_N_ELEMENTS (buf)) > 0)
+		{
+		  gchar *locale_buf = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
+		  if (locale_buf)
+		    {
+		      prg_name = g_path_get_basename (locale_buf);
+		      g_free (locale_buf);
+		    }
+		}
+	    }
+	}
+
+      if (prg_name)
+	{
+	  g_string_append_printf (gstring, "(%s): ", prg_name);
+	  g_free ((gchar *) prg_name);
+	}
+#else
       if (!prg_name)
 	g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
       else
 	g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
+#endif
     }
 
@@ -953,5 +960,4 @@
       const gchar *charset;
 
-      ensure_stdout_valid ();
       if (g_get_charset (&charset))
 	fputs (string, stdout); /* charset is UTF-8 already */
@@ -1005,5 +1011,4 @@
       const gchar *charset;
 
-      ensure_stderr_valid ();
       if (g_get_charset (&charset))
 	fputs (string, stderr); /* charset is UTF-8 already */
