gmessages: Add API to move info and debug messages to stderr

GLib code normally prints info and debug messages to stdout,
but that interferes with programs that are documented to produce
machine-readable output such as JSON or XML on stdout. In particular,
if such a program uses a GLib-based library, setting G_MESSAGES_DEBUG
will typically result in that library's debug messages going to the
program's stdout and corrupting the machine-readable output.

Unix programs can avoid this by using dup2() to move the original stdout
to another fd, then dup2() again to make the new stdout a copy of stderr,
but it's easier if we provide a way to not write debug messages to
stdout in the first place. Calling
g_log_writer_default_set_use_stderr (TRUE) results in behaviour
resembling Python's logging.basicConfig(), with all diagnostics going
to stderr.

Suggested by Allison Karlitskaya on glib#2087.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie
2020-07-23 16:27:02 +01:00
committed by Philip Withnall
parent 97956c2d3d
commit 10b0ece9d8
4 changed files with 65 additions and 3 deletions

View File

@@ -1484,6 +1484,7 @@ g_log_writer_format_fields
g_log_writer_journald
g_log_writer_standard_streams
g_log_writer_default
g_log_writer_default_set_use_stderr
<SUBSECTION Private>
g_log_structured_standard