Change message system to use fputs instead of write

By default g_log_default_handler always assumes that stdout
and stderr are file descriptors 1 and 2. On Win32 this isn't
always the case as the win32 API functions AttachConsole and
freopen can be used to dynamically attach GUI applications to
a console and the file descriptors of stderr and stdout will
become different than 1 and 2.

Fix it by using fputs with the FILE directly instead of
using the file descriptors.

https://bugzilla.gnome.org/show_bug.cgi?id=692085
This commit is contained in:
Ignacio Casal Quinteiro
2015-04-09 18:05:18 +02:00
parent 144d38fb9d
commit fb9df27776

View File

@@ -360,13 +360,10 @@ dowrite (int fd,
#endif #endif
static void static void
write_string (int fd, write_string (FILE *stream,
const gchar *string) const gchar *string)
{ {
int res; fputs (string, stream);
do
res = write (fd, string, strlen (string));
while (G_UNLIKELY (res == -1 && errno == EINTR));
} }
static GLogDomain* static GLogDomain*
@@ -881,7 +878,7 @@ format_unsigned (gchar *buf,
/* these are filtered by G_MESSAGES_DEBUG by the default log handler */ /* these are filtered by G_MESSAGES_DEBUG by the default log handler */
#define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG) #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
static int static FILE *
mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE], mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
GLogLevelFlags log_level) GLogLevelFlags log_level)
{ {
@@ -932,7 +929,7 @@ mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ()) if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
win32_keep_fatal_message = TRUE; win32_keep_fatal_message = TRUE;
#endif #endif
return to_stdout ? 1 : 2; return to_stdout ? stdout : stderr;
} }
typedef struct { typedef struct {
@@ -1282,7 +1279,7 @@ _g_log_fallback_handler (const gchar *log_domain,
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
gchar pid_string[FORMAT_UNSIGNED_BUFSIZE]; gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
#endif #endif
int fd; FILE *stream;
/* we cannot call _any_ GLib functions in this fallback handler, /* we cannot call _any_ GLib functions in this fallback handler,
* which is why we skip UTF-8 conversion, etc. * which is why we skip UTF-8 conversion, etc.
@@ -1291,7 +1288,7 @@ _g_log_fallback_handler (const gchar *log_domain,
* the process ID unconditionally however. * the process ID unconditionally however.
*/ */
fd = mklevel_prefix (level_prefix, log_level); stream = mklevel_prefix (level_prefix, log_level);
if (!message) if (!message)
message = "(NULL) message"; message = "(NULL) message";
@@ -1300,24 +1297,24 @@ _g_log_fallback_handler (const gchar *log_domain,
#endif #endif
if (log_domain) if (log_domain)
write_string (fd, "\n"); write_string (stream, "\n");
else else
write_string (fd, "\n** "); write_string (stream, "\n** ");
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
write_string (fd, "(process:"); write_string (stream, "(process:");
write_string (fd, pid_string); write_string (stream, pid_string);
write_string (fd, "): "); write_string (stream, "): ");
#endif #endif
if (log_domain) if (log_domain)
{ {
write_string (fd, log_domain); write_string (stream, log_domain);
write_string (fd, "-"); write_string (stream, "-");
} }
write_string (fd, level_prefix); write_string (stream, level_prefix);
write_string (fd, ": "); write_string (stream, ": ");
write_string (fd, message); write_string (stream, message);
} }
static void static void
@@ -1418,7 +1415,7 @@ g_log_default_handler (const gchar *log_domain,
{ {
gchar level_prefix[STRING_BUFFER_SIZE], *string; gchar level_prefix[STRING_BUFFER_SIZE], *string;
GString *gstring; GString *gstring;
int fd; FILE *stream;
const gchar *domains; const gchar *domains;
if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT)) if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
@@ -1438,7 +1435,7 @@ g_log_default_handler (const gchar *log_domain,
return; return;
} }
fd = mklevel_prefix (level_prefix, log_level); stream = mklevel_prefix (level_prefix, log_level);
gstring = g_string_new (NULL); gstring = g_string_new (NULL);
if (log_level & ALERT_LEVELS) if (log_level & ALERT_LEVELS)
@@ -1489,7 +1486,7 @@ g_log_default_handler (const gchar *log_domain,
string = g_string_free (gstring, FALSE); string = g_string_free (gstring, FALSE);
write_string (fd, string); write_string (stream, string);
g_free (string); g_free (string);
} }