Blame glib/gmessages.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:messages
Packit ae235b
 * @Title: Message Output and Debugging Functions
Packit ae235b
 * @Short_description: functions to output messages and help debug applications
Packit ae235b
 *
Packit ae235b
 * These functions provide support for outputting messages.
Packit ae235b
 *
Packit ae235b
 * The g_return family of macros (g_return_if_fail(),
Packit ae235b
 * g_return_val_if_fail(), g_return_if_reached(),
Packit ae235b
 * g_return_val_if_reached()) should only be used for programming
Packit ae235b
 * errors, a typical use case is checking for invalid parameters at
Packit ae235b
 * the beginning of a public function. They should not be used if
Packit ae235b
 * you just mean "if (error) return", they should only be used if
Packit ae235b
 * you mean "if (bug in program) return". The program behavior is
Packit ae235b
 * generally considered undefined after one of these checks fails.
Packit ae235b
 * They are not intended for normal control flow, only to give a
Packit ae235b
 * perhaps-helpful warning before giving up.
Packit ae235b
 *
Packit ae235b
 * Structured logging output is supported using g_log_structured(). This differs
Packit ae235b
 * from the traditional g_log() API in that log messages are handled as a
Packit ae235b
 * collection of key–value pairs representing individual pieces of information,
Packit ae235b
 * rather than as a single string containing all the information in an arbitrary
Packit ae235b
 * format.
Packit ae235b
 *
Packit ae235b
 * The convenience macros g_info(), g_message(), g_debug(), g_warning() and g_error()
Packit ae235b
 * will use the traditional g_log() API unless you define the symbol
Packit ae235b
 * %G_LOG_USE_STRUCTURED before including `glib.h`. But note that even messages
Packit ae235b
 * logged through the traditional g_log() API are ultimatively passed to
Packit ae235b
 * g_log_structured(), so that all log messages end up in same destination.
Packit ae235b
 * If %G_LOG_USE_STRUCTURED is defined, g_test_expect_message() will become
Packit ae235b
 * ineffective for the wrapper macros g_warning() and friends (see
Packit ae235b
 * [Testing for Messages][testing-for-messages]).
Packit ae235b
 *
Packit ae235b
 * The support for structured logging was motivated by the following needs (some
Packit ae235b
 * of which were supported previously; others weren’t):
Packit ae235b
 *  * Support for multiple logging levels.
Packit ae235b
 *  * Structured log support with the ability to add `MESSAGE_ID`s (see
Packit ae235b
 *    g_log_structured()).
Packit ae235b
 *  * Moving the responsibility for filtering log messages from the program to
Packit ae235b
 *    the log viewer — instead of libraries and programs installing log handlers
Packit ae235b
 *    (with g_log_set_handler()) which filter messages before output, all log
Packit ae235b
 *    messages are outputted, and the log viewer program (such as `journalctl`)
Packit ae235b
 *    must filter them. This is based on the idea that bugs are sometimes hard
Packit ae235b
 *    to reproduce, so it is better to log everything possible and then use
Packit ae235b
 *    tools to analyse the logs than it is to not be able to reproduce a bug to
Packit ae235b
 *    get additional log data. Code which uses logging in performance-critical
Packit ae235b
 *    sections should compile out the g_log_structured() calls in
Packit ae235b
 *    release builds, and compile them in in debugging builds.
Packit ae235b
 *  * A single writer function which handles all log messages in a process, from
Packit ae235b
 *    all libraries and program code; rather than multiple log handlers with
Packit ae235b
 *    poorly defined interactions between them. This allows a program to easily
Packit ae235b
 *    change its logging policy by changing the writer function, for example to
Packit ae235b
 *    log to an additional location or to change what logging output fallbacks
Packit ae235b
 *    are used. The log writer functions provided by GLib are exposed publicly
Packit ae235b
 *    so they can be used from programs’ log writers. This allows log writer
Packit ae235b
 *    policy and implementation to be kept separate.
Packit ae235b
 *  * If a library wants to add standard information to all of its log messages
Packit ae235b
 *    (such as library state) or to redact private data (such as passwords or
Packit ae235b
 *    network credentials), it should use a wrapper function around its
Packit ae235b
 *    g_log_structured() calls or implement that in the single log writer
Packit ae235b
 *    function.
Packit ae235b
 *  * If a program wants to pass context data from a g_log_structured() call to
Packit ae235b
 *    its log writer function so that, for example, it can use the correct
Packit ae235b
 *    server connection to submit logs to, that user data can be passed as a
Packit ae235b
 *    zero-length #GLogField to g_log_structured_array().
Packit ae235b
 *  * Color output needed to be supported on the terminal, to make reading
Packit ae235b
 *    through logs easier.
Packit ae235b
 *
Packit ae235b
 * ## Using Structured Logging ## {#using-structured-logging}
Packit ae235b
 *
Packit ae235b
 * To use structured logging (rather than the old-style logging), either use
Packit ae235b
 * the g_log_structured() and g_log_structured_array() functions; or define
Packit ae235b
 * `G_LOG_USE_STRUCTURED` before including any GLib header, and use the
Packit ae235b
 * g_message(), g_debug(), g_error() (etc.) macros.
Packit ae235b
 *
Packit ae235b
 * You do not need to define `G_LOG_USE_STRUCTURED` to use g_log_structured(),
Packit ae235b
 * but it is a good idea to avoid confusion.
Packit ae235b
 *
Packit ae235b
 * ## Log Domains ## {#log-domains}
Packit ae235b
 *
Packit ae235b
 * Log domains may be used to broadly split up the origins of log messages.
Packit ae235b
 * Typically, there are one or a few log domains per application or library.
Packit ae235b
 * %G_LOG_DOMAIN should be used to define the default log domain for the current
Packit ae235b
 * compilation unit — it is typically defined at the top of a source file, or in
Packit ae235b
 * the preprocessor flags for a group of source files.
Packit ae235b
 *
Packit ae235b
 * Log domains must be unique, and it is recommended that they are the
Packit ae235b
 * application or library name, optionally followed by a hyphen and a sub-domain
Packit ae235b
 * name. For example, `bloatpad` or `bloatpad-io`.
Packit ae235b
 *
Packit ae235b
 * ## Debug Message Output ## {#debug-message-output}
Packit ae235b
 *
Packit ae235b
 * The default log functions (g_log_default_handler() for the old-style API and
Packit ae235b
 * g_log_writer_default() for the structured API) both drop debug and
Packit ae235b
 * informational messages by default, unless the log domains of those messages
Packit ae235b
 * are listed in the `G_MESSAGES_DEBUG` environment variable (or it is set to
Packit ae235b
 * `all`).
Packit ae235b
 *
Packit ae235b
 * It is recommended that custom log writer functions re-use the
Packit ae235b
 * `G_MESSAGES_DEBUG` environment variable, rather than inventing a custom one,
Packit ae235b
 * so that developers can re-use the same debugging techniques and tools across
Packit ae235b
 * projects.
Packit ae235b
 *
Packit ae235b
 * ## Testing for Messages ## {#testing-for-messages}
Packit ae235b
 *
Packit ae235b
 * With the old g_log() API, g_test_expect_message() and
Packit ae235b
 * g_test_assert_expected_messages() could be used in simple cases to check
Packit ae235b
 * whether some code under test had emitted a given log message. These
Packit ae235b
 * functions have been deprecated with the structured logging API, for several
Packit ae235b
 * reasons:
Packit ae235b
 *  * They relied on an internal queue which was too inflexible for many use
Packit ae235b
 *    cases, where messages might be emitted in several orders, some
Packit ae235b
 *    messages might not be emitted deterministically, or messages might be
Packit ae235b
 *    emitted by unrelated log domains.
Packit ae235b
 *  * They do not support structured log fields.
Packit ae235b
 *  * Examining the log output of code is a bad approach to testing it, and
Packit ae235b
 *    while it might be necessary for legacy code which uses g_log(), it should
Packit ae235b
 *    be avoided for new code using g_log_structured().
Packit ae235b
 *
Packit ae235b
 * They will continue to work as before if g_log() is in use (and
Packit ae235b
 * %G_LOG_USE_STRUCTURED is not defined). They will do nothing if used with the
Packit ae235b
 * structured logging API.
Packit ae235b
 *
Packit ae235b
 * Examining the log output of code is discouraged: libraries should not emit to
Packit ae235b
 * `stderr` during defined behaviour, and hence this should not be tested. If
Packit ae235b
 * the log emissions of a library during undefined behaviour need to be tested,
Packit ae235b
 * they should be limited to asserting that the library aborts and prints a
Packit ae235b
 * suitable error message before aborting. This should be done with
Packit ae235b
 * g_test_trap_assert_stderr().
Packit ae235b
 *
Packit ae235b
 * If it is really necessary to test the structured log messages emitted by a
Packit ae235b
 * particular piece of code – and the code cannot be restructured to be more
Packit ae235b
 * suitable to more conventional unit testing – you should write a custom log
Packit ae235b
 * writer function (see g_log_set_writer_func()) which appends all log messages
Packit ae235b
 * to a queue. When you want to check the log messages, examine and clear the
Packit ae235b
 * queue, ignoring irrelevant log messages (for example, from log domains other
Packit ae235b
 * than the one under test).
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdarg.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <signal.h>
Packit ae235b
#include <locale.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#if defined(__linux__) && !defined(__BIONIC__)
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/socket.h>
Packit ae235b
#include <sys/un.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <sys/uio.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib-init.h"
Packit ae235b
#include "galloca.h"
Packit ae235b
#include "gbacktrace.h"
Packit ae235b
#include "gcharset.h"
Packit ae235b
#include "gconvert.h"
Packit ae235b
#include "genviron.h"
Packit ae235b
#include "gmain.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gprintfint.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gstring.h"
Packit ae235b
#include "gpattern.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <process.h>		/* For getpid() */
Packit ae235b
#include <io.h>
Packit ae235b
#  include <windows.h>
Packit ae235b
Packit ae235b
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
Packit ae235b
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* XXX: Remove once XP support really dropped */
Packit ae235b
#if _WIN32_WINNT < 0x0600
Packit ae235b
Packit ae235b
typedef enum _FILE_INFO_BY_HANDLE_CLASS
Packit ae235b
{
Packit ae235b
  FileBasicInfo                   = 0,
Packit ae235b
  FileStandardInfo                = 1,
Packit ae235b
  FileNameInfo                    = 2,
Packit ae235b
  FileRenameInfo                  = 3,
Packit ae235b
  FileDispositionInfo             = 4,
Packit ae235b
  FileAllocationInfo              = 5,
Packit ae235b
  FileEndOfFileInfo               = 6,
Packit ae235b
  FileStreamInfo                  = 7,
Packit ae235b
  FileCompressionInfo             = 8,
Packit ae235b
  FileAttributeTagInfo            = 9,
Packit ae235b
  FileIdBothDirectoryInfo         = 10,
Packit ae235b
  FileIdBothDirectoryRestartInfo  = 11,
Packit ae235b
  FileIoPriorityHintInfo          = 12,
Packit ae235b
  FileRemoteProtocolInfo          = 13,
Packit ae235b
  FileFullDirectoryInfo           = 14,
Packit ae235b
  FileFullDirectoryRestartInfo    = 15,
Packit ae235b
  FileStorageInfo                 = 16,
Packit ae235b
  FileAlignmentInfo               = 17,
Packit ae235b
  FileIdInfo                      = 18,
Packit ae235b
  FileIdExtdDirectoryInfo         = 19,
Packit ae235b
  FileIdExtdDirectoryRestartInfo  = 20,
Packit ae235b
  MaximumFileInfoByHandlesClass
Packit ae235b
} FILE_INFO_BY_HANDLE_CLASS;
Packit ae235b
Packit ae235b
typedef struct _FILE_NAME_INFO
Packit ae235b
{
Packit ae235b
  DWORD FileNameLength;
Packit ae235b
  WCHAR FileName[1];
Packit ae235b
} FILE_NAME_INFO;
Packit ae235b
Packit ae235b
typedef BOOL (WINAPI fGetFileInformationByHandleEx) (HANDLE,
Packit ae235b
                                                     FILE_INFO_BY_HANDLE_CLASS,
Packit ae235b
                                                     LPVOID,
Packit ae235b
                                                     DWORD);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if defined (_MSC_VER) && (_MSC_VER >=1400)
Packit ae235b
/* This is ugly, but we need it for isatty() in case we have bad fd's,
Packit ae235b
 * otherwise Windows will abort() the program on msvcrt80.dll and later
Packit ae235b
 */
Packit ae235b
#include <crtdbg.h>
Packit ae235b
Packit ae235b
_GLIB_EXTERN void
Packit ae235b
myInvalidParameterHandler(const wchar_t *expression,
Packit ae235b
                          const wchar_t *function,
Packit ae235b
                          const wchar_t *file,
Packit ae235b
                          unsigned int   line,
Packit ae235b
                          uintptr_t      pReserved)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gwin32.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOG_DOMAIN:
Packit ae235b
 *
Packit ae235b
 * Defines the log domain. See [Log Domains](#log-domains).
Packit ae235b
 *
Packit ae235b
 * Libraries should define this so that any messages
Packit ae235b
 * which they log can be differentiated from messages from other
Packit ae235b
 * libraries and application code. But be careful not to define
Packit ae235b
 * it in any public header files.
Packit ae235b
 *
Packit ae235b
 * Log domains must be unique, and it is recommended that they are the
Packit ae235b
 * application or library name, optionally followed by a hyphen and a sub-domain
Packit ae235b
 * name. For example, `bloatpad` or `bloatpad-io`.
Packit ae235b
 *
Packit ae235b
 * If undefined, it defaults to the default %NULL (or `""`) log domain; this is
Packit ae235b
 * not advisable, as it cannot be filtered against using the `G_MESSAGES_DEBUG`
Packit ae235b
 * environment variable.
Packit ae235b
 *
Packit ae235b
 * For example, GTK+ uses this in its `Makefile.am`:
Packit ae235b
 * |[
Packit ae235b
 * AM_CPPFLAGS = -DG_LOG_DOMAIN=\"Gtk\"
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Applications can choose to leave it as the default %NULL (or `""`)
Packit ae235b
 * domain. However, defining the domain offers the same advantages as
Packit ae235b
 * above.
Packit ae235b
 *
Packit ae235b
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOG_FATAL_MASK:
Packit ae235b
 *
Packit ae235b
 * GLib log levels that are considered fatal by default.
Packit ae235b
 *
Packit ae235b
 * This is not used if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GLogFunc:
Packit ae235b
 * @log_domain: the log domain of the message
Packit ae235b
 * @log_level: the log level of the message (including the
Packit ae235b
 *     fatal and recursion flags)
Packit ae235b
 * @message: the message to process
Packit ae235b
 * @user_data: user data, set in g_log_set_handler()
Packit ae235b
 *
Packit ae235b
 * Specifies the prototype of log handler functions.
Packit ae235b
 *
Packit ae235b
 * The default log handler, g_log_default_handler(), automatically appends a
Packit ae235b
 * new-line character to @message when printing it. It is advised that any
Packit ae235b
 * custom log handler functions behave similarly, so that logging calls in user
Packit ae235b
 * code do not need modifying to add a new-line character to the message if the
Packit ae235b
 * log handler is changed.
Packit ae235b
 *
Packit ae235b
 * This is not used if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GLogLevelFlags:
Packit ae235b
 * @G_LOG_FLAG_RECURSION: internal flag
Packit ae235b
 * @G_LOG_FLAG_FATAL: internal flag
Packit ae235b
 * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
Packit ae235b
 *     This level is also used for messages produced by g_assert().
Packit ae235b
 * @G_LOG_LEVEL_CRITICAL: log level for critical warning messages, see
Packit ae235b
 *     g_critical().
Packit ae235b
 *     This level is also used for messages produced by g_return_if_fail()
Packit ae235b
 *     and g_return_val_if_fail().
Packit ae235b
 * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
Packit ae235b
 * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
Packit ae235b
 * @G_LOG_LEVEL_INFO: log level for informational messages, see g_info()
Packit ae235b
 * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
Packit ae235b
 * @G_LOG_LEVEL_MASK: a mask including all log levels
Packit ae235b
 *
Packit ae235b
 * Flags specifying the level of log messages.
Packit ae235b
 *
Packit ae235b
 * It is possible to change how GLib treats messages of the various
Packit ae235b
 * levels using g_log_set_handler() and g_log_set_fatal_mask().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOG_LEVEL_USER_SHIFT:
Packit ae235b
 *
Packit ae235b
 * Log levels below 1<
Packit ae235b
 * Higher bits can be used for user-defined log levels.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_message:
Packit ae235b
 * @...: format string, followed by parameters to insert
Packit ae235b
 *     into the format string (as with printf())
Packit ae235b
 *
Packit ae235b
 * A convenience function/macro to log a normal message.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * If structured logging is enabled, this will use g_log_structured();
Packit ae235b
 * otherwise it will use g_log(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_warning:
Packit ae235b
 * @...: format string, followed by parameters to insert
Packit ae235b
 *     into the format string (as with printf())
Packit ae235b
 *
Packit ae235b
 * A convenience function/macro to log a warning message. The message should
Packit ae235b
 * typically *not* be translated to the user's language.
Packit ae235b
 *
Packit ae235b
 * This is not intended for end user error reporting. Use of #GError is
Packit ae235b
 * preferred for that instead, as it allows calling functions to perform actions
Packit ae235b
 * conditional on the type of error.
Packit ae235b
 *
Packit ae235b
 * You can make warnings fatal at runtime by setting the `G_DEBUG`
Packit ae235b
 * environment variable (see
Packit ae235b
 * [Running GLib Applications](glib-running.html)):
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   G_DEBUG=fatal-warnings gdb ./my-program
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Any unrelated failures can be skipped over in
Packit ae235b
 * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function,
Packit ae235b
 * a newline character will automatically be appended to @..., and
Packit ae235b
 * need not be entered manually.
Packit ae235b
 *
Packit ae235b
 * If structured logging is enabled, this will use g_log_structured();
Packit ae235b
 * otherwise it will use g_log(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_critical:
Packit ae235b
 * @...: format string, followed by parameters to insert
Packit ae235b
 *     into the format string (as with printf())
Packit ae235b
 *
Packit ae235b
 * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
Packit ae235b
 * It's more or less application-defined what constitutes
Packit ae235b
 * a critical vs. a regular warning. You could call
Packit ae235b
 * g_log_set_always_fatal() to make critical warnings exit
Packit ae235b
 * the program, then use g_critical() for fatal errors, for
Packit ae235b
 * example.
Packit ae235b
 *
Packit ae235b
 * You can also make critical warnings fatal at runtime by
Packit ae235b
 * setting the `G_DEBUG` environment variable (see
Packit ae235b
 * [Running GLib Applications](glib-running.html)):
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   G_DEBUG=fatal-warnings gdb ./my-program
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Any unrelated failures can be skipped over in
Packit ae235b
 * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
Packit ae235b
 *
Packit ae235b
 * The message should typically *not* be translated to the
Packit ae235b
 * user's language.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * If structured logging is enabled, this will use g_log_structured();
Packit ae235b
 * otherwise it will use g_log(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error:
Packit ae235b
 * @...: format string, followed by parameters to insert
Packit ae235b
 *     into the format string (as with printf())
Packit ae235b
 *
Packit ae235b
 * A convenience function/macro to log an error message. The message should
Packit ae235b
 * typically *not* be translated to the user's language.
Packit ae235b
 *
Packit ae235b
 * This is not intended for end user error reporting. Use of #GError is
Packit ae235b
 * preferred for that instead, as it allows calling functions to perform actions
Packit ae235b
 * conditional on the type of error.
Packit ae235b
 *
Packit ae235b
 * Error messages are always fatal, resulting in a call to
Packit ae235b
 * abort() to terminate the application. This function will
Packit ae235b
 * result in a core dump; don't use it for errors you expect.
Packit ae235b
 * Using this function indicates a bug in your program, i.e.
Packit ae235b
 * an assertion failure.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * If structured logging is enabled, this will use g_log_structured();
Packit ae235b
 * otherwise it will use g_log(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_info:
Packit ae235b
 * @...: format string, followed by parameters to insert
Packit ae235b
 *     into the format string (as with printf())
Packit ae235b
 *
Packit ae235b
 * A convenience function/macro to log an informational message. Seldom used.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * Such messages are suppressed by the g_log_default_handler() and
Packit ae235b
 * g_log_writer_default() unless the `G_MESSAGES_DEBUG` environment variable is
Packit ae235b
 * set appropriately.
Packit ae235b
 *
Packit ae235b
 * If structured logging is enabled, this will use g_log_structured();
Packit ae235b
 * otherwise it will use g_log(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_debug:
Packit ae235b
 * @...: format string, followed by parameters to insert
Packit ae235b
 *     into the format string (as with printf())
Packit ae235b
 *
Packit ae235b
 * A convenience function/macro to log a debug message. The message should
Packit ae235b
 * typically *not* be translated to the user's language.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * Such messages are suppressed by the g_log_default_handler() and
Packit ae235b
 * g_log_writer_default() unless the `G_MESSAGES_DEBUG` environment variable is
Packit ae235b
 * set appropriately.
Packit ae235b
 *
Packit ae235b
 * If structured logging is enabled, this will use g_log_structured();
Packit ae235b
 * otherwise it will use g_log(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* --- structures --- */
Packit ae235b
typedef struct _GLogDomain	GLogDomain;
Packit ae235b
typedef struct _GLogHandler	GLogHandler;
Packit ae235b
struct _GLogDomain
Packit ae235b
{
Packit ae235b
  gchar		*log_domain;
Packit ae235b
  GLogLevelFlags fatal_mask;
Packit ae235b
  GLogHandler	*handlers;
Packit ae235b
  GLogDomain	*next;
Packit ae235b
};
Packit ae235b
struct _GLogHandler
Packit ae235b
{
Packit ae235b
  guint		 id;
Packit ae235b
  GLogLevelFlags log_level;
Packit ae235b
  GLogFunc	 log_func;
Packit ae235b
  gpointer	 data;
Packit ae235b
  GDestroyNotify destroy;
Packit ae235b
  GLogHandler	*next;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
/* --- variables --- */
Packit ae235b
static GMutex         g_messages_lock;
Packit ae235b
static GLogDomain    *g_log_domains = NULL;
Packit ae235b
static GPrintFunc     glib_print_func = NULL;
Packit ae235b
static GPrintFunc     glib_printerr_func = NULL;
Packit ae235b
static GPrivate       g_log_depth;
Packit ae235b
static GPrivate       g_log_structured_depth;
Packit ae235b
static GLogFunc       default_log_func = g_log_default_handler;
Packit ae235b
static gpointer       default_log_data = NULL;
Packit ae235b
static GTestLogFatalFunc fatal_log_func = NULL;
Packit ae235b
static gpointer          fatal_log_data;
Packit ae235b
static GLogWriterFunc log_writer_func = g_log_writer_default;
Packit ae235b
static gpointer       log_writer_user_data = NULL;
Packit ae235b
static GDestroyNotify log_writer_user_data_free = NULL;
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
Packit ae235b
static void _g_log_abort (gboolean breakpoint);
Packit ae235b
Packit ae235b
static void
Packit ae235b
_g_log_abort (gboolean breakpoint)
Packit ae235b
{
Packit ae235b
  gboolean debugger_present;
Packit ae235b
Packit ae235b
  if (g_test_subprocess ())
Packit ae235b
    {
Packit ae235b
      /* If this is a test case subprocess then it probably caused
Packit ae235b
       * this error message on purpose, so just exit() rather than
Packit ae235b
       * abort()ing, to avoid triggering any system crash-reporting
Packit ae235b
       * daemon.
Packit ae235b
       */
Packit ae235b
      _exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  debugger_present = IsDebuggerPresent ();
Packit ae235b
#else
Packit ae235b
  /* Assume GDB is attached. */
Packit ae235b
  debugger_present = TRUE;
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
  if (debugger_present && breakpoint)
Packit ae235b
    G_BREAKPOINT ();
Packit ae235b
  else
Packit ae235b
    g_abort ();
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
static gboolean win32_keep_fatal_message = FALSE;
Packit ae235b
Packit ae235b
/* This default message will usually be overwritten. */
Packit ae235b
/* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
Packit ae235b
 * called with huge strings, is it?
Packit ae235b
 */
Packit ae235b
static gchar  fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
Packit ae235b
static gchar *fatal_msg_ptr = fatal_msg_buf;
Packit ae235b
Packit ae235b
#undef write
Packit ae235b
static inline int
Packit ae235b
dowrite (int          fd,
Packit ae235b
	 const void  *buf,
Packit ae235b
	 unsigned int len)
Packit ae235b
{
Packit ae235b
  if (win32_keep_fatal_message)
Packit ae235b
    {
Packit ae235b
      memcpy (fatal_msg_ptr, buf, len);
Packit ae235b
      fatal_msg_ptr += len;
Packit ae235b
      *fatal_msg_ptr = 0;
Packit ae235b
      return len;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  write (fd, buf, len);
Packit ae235b
Packit ae235b
  return len;
Packit ae235b
}
Packit ae235b
#define write(fd, buf, len) dowrite(fd, buf, len)
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_string (FILE        *stream,
Packit ae235b
	      const gchar *string)
Packit ae235b
{
Packit ae235b
  fputs (string, stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_string_sized (FILE        *stream,
Packit ae235b
                    const gchar *string,
Packit ae235b
                    gssize       length)
Packit ae235b
{
Packit ae235b
  /* Is it nul-terminated? */
Packit ae235b
  if (length < 0)
Packit ae235b
    write_string (stream, string);
Packit ae235b
  else
Packit ae235b
    fwrite (string, 1, length, stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GLogDomain*
Packit ae235b
g_log_find_domain_L (const gchar *log_domain)
Packit ae235b
{
Packit ae235b
  GLogDomain *domain;
Packit ae235b
  
Packit ae235b
  domain = g_log_domains;
Packit ae235b
  while (domain)
Packit ae235b
    {
Packit ae235b
      if (strcmp (domain->log_domain, log_domain) == 0)
Packit ae235b
	return domain;
Packit ae235b
      domain = domain->next;
Packit ae235b
    }
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GLogDomain*
Packit ae235b
g_log_domain_new_L (const gchar *log_domain)
Packit ae235b
{
Packit ae235b
  GLogDomain *domain;
Packit ae235b
Packit ae235b
  domain = g_new (GLogDomain, 1);
Packit ae235b
  domain->log_domain = g_strdup (log_domain);
Packit ae235b
  domain->fatal_mask = G_LOG_FATAL_MASK;
Packit ae235b
  domain->handlers = NULL;
Packit ae235b
  
Packit ae235b
  domain->next = g_log_domains;
Packit ae235b
  g_log_domains = domain;
Packit ae235b
  
Packit ae235b
  return domain;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_log_domain_check_free_L (GLogDomain *domain)
Packit ae235b
{
Packit ae235b
  if (domain->fatal_mask == G_LOG_FATAL_MASK &&
Packit ae235b
      domain->handlers == NULL)
Packit ae235b
    {
Packit ae235b
      GLogDomain *last, *work;
Packit ae235b
      
Packit ae235b
      last = NULL;  
Packit ae235b
Packit ae235b
      work = g_log_domains;
Packit ae235b
      while (work)
Packit ae235b
	{
Packit ae235b
	  if (work == domain)
Packit ae235b
	    {
Packit ae235b
	      if (last)
Packit ae235b
		last->next = domain->next;
Packit ae235b
	      else
Packit ae235b
		g_log_domains = domain->next;
Packit ae235b
	      g_free (domain->log_domain);
Packit ae235b
	      g_free (domain);
Packit ae235b
	      break;
Packit ae235b
	    }
Packit ae235b
	  last = work;
Packit ae235b
	  work = last->next;
Packit ae235b
	}  
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GLogFunc
Packit ae235b
g_log_domain_get_handler_L (GLogDomain	*domain,
Packit ae235b
			    GLogLevelFlags log_level,
Packit ae235b
			    gpointer	*data)
Packit ae235b
{
Packit ae235b
  if (domain && log_level)
Packit ae235b
    {
Packit ae235b
      GLogHandler *handler;
Packit ae235b
      
Packit ae235b
      handler = domain->handlers;
Packit ae235b
      while (handler)
Packit ae235b
	{
Packit ae235b
	  if ((handler->log_level & log_level) == log_level)
Packit ae235b
	    {
Packit ae235b
	      *data = handler->data;
Packit ae235b
	      return handler->log_func;
Packit ae235b
	    }
Packit ae235b
	  handler = handler->next;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *data = default_log_data;
Packit ae235b
  return default_log_func;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_set_always_fatal:
Packit ae235b
 * @fatal_mask: the mask containing bits set for each level
Packit ae235b
 *     of error which is to be fatal
Packit ae235b
 *
Packit ae235b
 * Sets the message levels which are always fatal, in any log domain.
Packit ae235b
 * When a message with any of these levels is logged the program terminates.
Packit ae235b
 * You can only set the levels defined by GLib to be fatal.
Packit ae235b
 * %G_LOG_LEVEL_ERROR is always fatal.
Packit ae235b
 *
Packit ae235b
 * You can also make some message levels fatal at runtime by setting
Packit ae235b
 * the `G_DEBUG` environment variable (see
Packit ae235b
 * [Running GLib Applications](glib-running.html)).
Packit ae235b
 *
Packit ae235b
 * Libraries should not call this function, as it affects all messages logged
Packit ae235b
 * by a process, including those from other libraries.
Packit ae235b
 *
Packit ae235b
 * Structured log messages (using g_log_structured() and
Packit ae235b
 * g_log_structured_array()) are fatal only if the default log writer is used;
Packit ae235b
 * otherwise it is up to the writer function to determine which log messages
Packit ae235b
 * are fatal. See [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Returns: the old fatal mask
Packit ae235b
 */
Packit ae235b
GLogLevelFlags
Packit ae235b
g_log_set_always_fatal (GLogLevelFlags fatal_mask)
Packit ae235b
{
Packit ae235b
  GLogLevelFlags old_mask;
Packit ae235b
Packit ae235b
  /* restrict the global mask to levels that are known to glib
Packit ae235b
   * since this setting applies to all domains
Packit ae235b
   */
Packit ae235b
  fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
Packit ae235b
  /* force errors to be fatal */
Packit ae235b
  fatal_mask |= G_LOG_LEVEL_ERROR;
Packit ae235b
  /* remove bogus flag */
Packit ae235b
  fatal_mask &= ~G_LOG_FLAG_FATAL;
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  old_mask = g_log_always_fatal;
Packit ae235b
  g_log_always_fatal = fatal_mask;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  return old_mask;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_set_fatal_mask:
Packit ae235b
 * @log_domain: the log domain
Packit ae235b
 * @fatal_mask: the new fatal mask
Packit ae235b
 *
Packit ae235b
 * Sets the log levels which are fatal in the given domain.
Packit ae235b
 * %G_LOG_LEVEL_ERROR is always fatal.
Packit ae235b
 *
Packit ae235b
 * This has no effect on structured log messages (using g_log_structured() or
Packit ae235b
 * g_log_structured_array()). To change the fatal behaviour for specific log
Packit ae235b
 * messages, programs must install a custom log writer function using
Packit ae235b
 * g_log_set_writer_func(). See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Returns: the old fatal mask for the log domain
Packit ae235b
 */
Packit ae235b
GLogLevelFlags
Packit ae235b
g_log_set_fatal_mask (const gchar   *log_domain,
Packit ae235b
		      GLogLevelFlags fatal_mask)
Packit ae235b
{
Packit ae235b
  GLogLevelFlags old_flags;
Packit ae235b
  GLogDomain *domain;
Packit ae235b
  
Packit ae235b
  if (!log_domain)
Packit ae235b
    log_domain = "";
Packit ae235b
Packit ae235b
  /* force errors to be fatal */
Packit ae235b
  fatal_mask |= G_LOG_LEVEL_ERROR;
Packit ae235b
  /* remove bogus flag */
Packit ae235b
  fatal_mask &= ~G_LOG_FLAG_FATAL;
Packit ae235b
  
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
Packit ae235b
  domain = g_log_find_domain_L (log_domain);
Packit ae235b
  if (!domain)
Packit ae235b
    domain = g_log_domain_new_L (log_domain);
Packit ae235b
  old_flags = domain->fatal_mask;
Packit ae235b
  
Packit ae235b
  domain->fatal_mask = fatal_mask;
Packit ae235b
  g_log_domain_check_free_L (domain);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  return old_flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_set_handler:
Packit ae235b
 * @log_domain: (nullable): the log domain, or %NULL for the default ""
Packit ae235b
 *     application domain
Packit ae235b
 * @log_levels: the log levels to apply the log handler for.
Packit ae235b
 *     To handle fatal and recursive messages as well, combine
Packit ae235b
 *     the log levels with the #G_LOG_FLAG_FATAL and
Packit ae235b
 *     #G_LOG_FLAG_RECURSION bit flags.
Packit ae235b
 * @log_func: the log handler function
Packit ae235b
 * @user_data: data passed to the log handler
Packit ae235b
 *
Packit ae235b
 * Sets the log handler for a domain and a set of log levels.
Packit ae235b
 * To handle fatal and recursive messages the @log_levels parameter
Packit ae235b
 * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
Packit ae235b
 * bit flags.
Packit ae235b
 *
Packit ae235b
 * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
Packit ae235b
 * you want to set a handler for this log level you must combine it with
Packit ae235b
 * #G_LOG_FLAG_FATAL.
Packit ae235b
 *
Packit ae235b
 * This has no effect if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Here is an example for adding a log handler for all warning messages
Packit ae235b
 * in the default domain:
Packit ae235b
 * |[ 
Packit ae235b
 * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
Packit ae235b
 *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * This example adds a log handler for all critical messages from GTK+:
Packit ae235b
 * |[ 
Packit ae235b
 * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
Packit ae235b
 *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * This example adds a log handler for all messages from GLib:
Packit ae235b
 * |[ 
Packit ae235b
 * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
Packit ae235b
 *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: the id of the new handler
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_log_set_handler (const gchar	 *log_domain,
Packit ae235b
                   GLogLevelFlags log_levels,
Packit ae235b
                   GLogFunc       log_func,
Packit ae235b
                   gpointer       user_data)
Packit ae235b
{
Packit ae235b
  return g_log_set_handler_full (log_domain, log_levels, log_func, user_data, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_set_handler_full: (rename-to g_log_set_handler)
Packit ae235b
 * @log_domain: (nullable): the log domain, or %NULL for the default ""
Packit ae235b
 *     application domain
Packit ae235b
 * @log_levels: the log levels to apply the log handler for.
Packit ae235b
 *     To handle fatal and recursive messages as well, combine
Packit ae235b
 *     the log levels with the #G_LOG_FLAG_FATAL and
Packit ae235b
 *     #G_LOG_FLAG_RECURSION bit flags.
Packit ae235b
 * @log_func: the log handler function
Packit ae235b
 * @user_data: data passed to the log handler
Packit ae235b
 * @destroy: destroy notify for @user_data, or %NULL
Packit ae235b
 *
Packit ae235b
 * Like g_log_set_handler(), but takes a destroy notify for the @user_data.
Packit ae235b
 *
Packit ae235b
 * This has no effect if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Returns: the id of the new handler
Packit ae235b
 *
Packit ae235b
 * Since: 2.46
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_log_set_handler_full (const gchar    *log_domain,
Packit ae235b
                        GLogLevelFlags  log_levels,
Packit ae235b
                        GLogFunc        log_func,
Packit ae235b
                        gpointer        user_data,
Packit ae235b
                        GDestroyNotify  destroy)
Packit ae235b
{
Packit ae235b
  static guint handler_id = 0;
Packit ae235b
  GLogDomain *domain;
Packit ae235b
  GLogHandler *handler;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
Packit ae235b
  g_return_val_if_fail (log_func != NULL, 0);
Packit ae235b
  
Packit ae235b
  if (!log_domain)
Packit ae235b
    log_domain = "";
Packit ae235b
Packit ae235b
  handler = g_new (GLogHandler, 1);
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
Packit ae235b
  domain = g_log_find_domain_L (log_domain);
Packit ae235b
  if (!domain)
Packit ae235b
    domain = g_log_domain_new_L (log_domain);
Packit ae235b
  
Packit ae235b
  handler->id = ++handler_id;
Packit ae235b
  handler->log_level = log_levels;
Packit ae235b
  handler->log_func = log_func;
Packit ae235b
  handler->data = user_data;
Packit ae235b
  handler->destroy = destroy;
Packit ae235b
  handler->next = domain->handlers;
Packit ae235b
  domain->handlers = handler;
Packit ae235b
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
  
Packit ae235b
  return handler_id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_set_default_handler:
Packit ae235b
 * @log_func: the log handler function
Packit ae235b
 * @user_data: data passed to the log handler
Packit ae235b
 *
Packit ae235b
 * Installs a default log handler which is used if no
Packit ae235b
 * log handler has been set for the particular log domain
Packit ae235b
 * and log level combination. By default, GLib uses
Packit ae235b
 * g_log_default_handler() as default log handler.
Packit ae235b
 *
Packit ae235b
 * This has no effect if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Returns: the previous default log handler
Packit ae235b
 *
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
GLogFunc
Packit ae235b
g_log_set_default_handler (GLogFunc log_func,
Packit ae235b
			   gpointer user_data)
Packit ae235b
{
Packit ae235b
  GLogFunc old_log_func;
Packit ae235b
  
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  old_log_func = default_log_func;
Packit ae235b
  default_log_func = log_func;
Packit ae235b
  default_log_data = user_data;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
  
Packit ae235b
  return old_log_func;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_log_set_fatal_handler:
Packit ae235b
 * @log_func: the log handler function.
Packit ae235b
 * @user_data: data passed to the log handler.
Packit ae235b
 *
Packit ae235b
 * Installs a non-error fatal log handler which can be
Packit ae235b
 * used to decide whether log messages which are counted
Packit ae235b
 * as fatal abort the program.
Packit ae235b
 *
Packit ae235b
 * The use case here is that you are running a test case
Packit ae235b
 * that depends on particular libraries or circumstances
Packit ae235b
 * and cannot prevent certain known critical or warning
Packit ae235b
 * messages. So you install a handler that compares the
Packit ae235b
 * domain and message to precisely not abort in such a case.
Packit ae235b
 *
Packit ae235b
 * Note that the handler is reset at the beginning of
Packit ae235b
 * any test case, so you have to set it inside each test
Packit ae235b
 * function which needs the special behavior.
Packit ae235b
 *
Packit ae235b
 * This handler has no effect on g_error messages.
Packit ae235b
 *
Packit ae235b
 * This handler also has no effect on structured log messages (using
Packit ae235b
 * g_log_structured() or g_log_structured_array()). To change the fatal
Packit ae235b
 * behaviour for specific log messages, programs must install a custom log
Packit ae235b
 * writer function using g_log_set_writer_func().See
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
Packit ae235b
                              gpointer          user_data)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  fatal_log_func = log_func;
Packit ae235b
  fatal_log_data = user_data;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_remove_handler:
Packit ae235b
 * @log_domain: the log domain
Packit ae235b
 * @handler_id: the id of the handler, which was returned
Packit ae235b
 *     in g_log_set_handler()
Packit ae235b
 *
Packit ae235b
 * Removes the log handler.
Packit ae235b
 *
Packit ae235b
 * This has no effect if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_log_remove_handler (const gchar *log_domain,
Packit ae235b
		      guint	   handler_id)
Packit ae235b
{
Packit ae235b
  GLogDomain *domain;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (handler_id > 0);
Packit ae235b
  
Packit ae235b
  if (!log_domain)
Packit ae235b
    log_domain = "";
Packit ae235b
  
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  domain = g_log_find_domain_L (log_domain);
Packit ae235b
  if (domain)
Packit ae235b
    {
Packit ae235b
      GLogHandler *work, *last;
Packit ae235b
      
Packit ae235b
      last = NULL;
Packit ae235b
      work = domain->handlers;
Packit ae235b
      while (work)
Packit ae235b
	{
Packit ae235b
	  if (work->id == handler_id)
Packit ae235b
	    {
Packit ae235b
	      if (last)
Packit ae235b
		last->next = work->next;
Packit ae235b
	      else
Packit ae235b
		domain->handlers = work->next;
Packit ae235b
	      g_log_domain_check_free_L (domain); 
Packit ae235b
	      g_mutex_unlock (&g_messages_lock);
Packit ae235b
              if (work->destroy)
Packit ae235b
                work->destroy (work->data);
Packit ae235b
	      g_free (work);
Packit ae235b
	      return;
Packit ae235b
	    }
Packit ae235b
	  last = work;
Packit ae235b
	  work = last->next;
Packit ae235b
	}
Packit ae235b
    } 
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
  g_warning ("%s: could not find handler with id '%d' for domain \"%s\"",
Packit ae235b
	     G_STRLOC, handler_id, log_domain);
Packit ae235b
}
Packit ae235b
Packit ae235b
#define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
Packit ae235b
			    (wc == 0x7f) || \
Packit ae235b
			    (wc >= 0x80 && wc < 0xa0)))
Packit ae235b
     
Packit ae235b
static gchar*
Packit ae235b
strdup_convert (const gchar *string,
Packit ae235b
		const gchar *charset)
Packit ae235b
{
Packit ae235b
  if (!g_utf8_validate (string, -1, NULL))
Packit ae235b
    {
Packit ae235b
      GString *gstring = g_string_new ("[Invalid UTF-8] ");
Packit ae235b
      guchar *p;
Packit ae235b
Packit ae235b
      for (p = (guchar *)string; *p; p++)
Packit ae235b
	{
Packit ae235b
	  if (CHAR_IS_SAFE(*p) &&
Packit ae235b
	      !(*p == '\r' && *(p + 1) != '\n') &&
Packit ae235b
	      *p < 0x80)
Packit ae235b
	    g_string_append_c (gstring, *p);
Packit ae235b
	  else
Packit ae235b
	    g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      return g_string_free (gstring, FALSE);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GError *err = NULL;
Packit ae235b
      
Packit ae235b
      gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err;;
Packit ae235b
      if (result)
Packit ae235b
	return result;
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  /* Not thread-safe, but doesn't matter if we print the warning twice
Packit ae235b
	   */
Packit ae235b
	  static gboolean warned = FALSE; 
Packit ae235b
	  if (!warned)
Packit ae235b
	    {
Packit ae235b
	      warned = TRUE;
Packit ae235b
	      _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
Packit ae235b
	    }
Packit ae235b
	  g_error_free (err);
Packit ae235b
	  
Packit ae235b
	  return g_strdup (string);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* For a radix of 8 we need at most 3 output bytes for 1 input
Packit ae235b
 * byte. Additionally we might need up to 2 output bytes for the
Packit ae235b
 * readix prefix and 1 byte for the trailing NULL.
Packit ae235b
 */
Packit ae235b
#define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
Packit ae235b
Packit ae235b
static void
Packit ae235b
format_unsigned (gchar  *buf,
Packit ae235b
		 gulong  num,
Packit ae235b
		 guint   radix)
Packit ae235b
{
Packit ae235b
  gulong tmp;
Packit ae235b
  gchar c;
Packit ae235b
  gint i, n;
Packit ae235b
Packit ae235b
  /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
Packit ae235b
Packit ae235b
  if (radix != 8 && radix != 10 && radix != 16)
Packit ae235b
    {
Packit ae235b
      *buf = '\000';
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (!num)
Packit ae235b
    {
Packit ae235b
      *buf++ = '0';
Packit ae235b
      *buf = '\000';
Packit ae235b
      return;
Packit ae235b
    } 
Packit ae235b
  
Packit ae235b
  if (radix == 16)
Packit ae235b
    {
Packit ae235b
      *buf++ = '0';
Packit ae235b
      *buf++ = 'x';
Packit ae235b
    }
Packit ae235b
  else if (radix == 8)
Packit ae235b
    {
Packit ae235b
      *buf++ = '0';
Packit ae235b
    }
Packit ae235b
	
Packit ae235b
  n = 0;
Packit ae235b
  tmp = num;
Packit ae235b
  while (tmp)
Packit ae235b
    {
Packit ae235b
      tmp /= radix;
Packit ae235b
      n++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  i = n;
Packit ae235b
Packit ae235b
  /* Again we can't use g_assert; actually this check should _never_ fail. */
Packit ae235b
  if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
Packit ae235b
    {
Packit ae235b
      *buf = '\000';
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (num)
Packit ae235b
    {
Packit ae235b
      i--;
Packit ae235b
      c = (num % radix);
Packit ae235b
      if (c < 10)
Packit ae235b
	buf[i] = c + '0';
Packit ae235b
      else
Packit ae235b
	buf[i] = c + 'a' - 10;
Packit ae235b
      num /= radix;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  buf[n] = '\000';
Packit ae235b
}
Packit ae235b
Packit ae235b
/* string size big enough to hold level prefix */
Packit ae235b
#define	STRING_BUFFER_SIZE	(FORMAT_UNSIGNED_BUFSIZE + 32)
Packit ae235b
Packit ae235b
#define	ALERT_LEVELS		(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
Packit ae235b
Packit ae235b
/* these are emitted by the default log handler */
Packit ae235b
#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
Packit ae235b
/* these are filtered by G_MESSAGES_DEBUG by the default log handler */
Packit ae235b
#define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
Packit ae235b
Packit ae235b
static const gchar *log_level_to_color (GLogLevelFlags log_level,
Packit ae235b
                                        gboolean       use_color);
Packit ae235b
static const gchar *color_reset        (gboolean       use_color);
Packit ae235b
Packit ae235b
static FILE *
Packit ae235b
mklevel_prefix (gchar          level_prefix[STRING_BUFFER_SIZE],
Packit ae235b
                GLogLevelFlags log_level,
Packit ae235b
                gboolean       use_color)
Packit ae235b
{
Packit ae235b
  gboolean to_stdout = TRUE;
Packit ae235b
Packit ae235b
  /* we may not call _any_ GLib functions here */
Packit ae235b
Packit ae235b
  strcpy (level_prefix, log_level_to_color (log_level, use_color));
Packit ae235b
Packit ae235b
  switch (log_level & G_LOG_LEVEL_MASK)
Packit ae235b
    {
Packit ae235b
    case G_LOG_LEVEL_ERROR:
Packit ae235b
      strcat (level_prefix, "ERROR");
Packit ae235b
      to_stdout = FALSE;
Packit ae235b
      break;
Packit ae235b
    case G_LOG_LEVEL_CRITICAL:
Packit ae235b
      strcat (level_prefix, "CRITICAL");
Packit ae235b
      to_stdout = FALSE;
Packit ae235b
      break;
Packit ae235b
    case G_LOG_LEVEL_WARNING:
Packit ae235b
      strcat (level_prefix, "WARNING");
Packit ae235b
      to_stdout = FALSE;
Packit ae235b
      break;
Packit ae235b
    case G_LOG_LEVEL_MESSAGE:
Packit ae235b
      strcat (level_prefix, "Message");
Packit ae235b
      to_stdout = FALSE;
Packit ae235b
      break;
Packit ae235b
    case G_LOG_LEVEL_INFO:
Packit ae235b
      strcat (level_prefix, "INFO");
Packit ae235b
      break;
Packit ae235b
    case G_LOG_LEVEL_DEBUG:
Packit ae235b
      strcat (level_prefix, "DEBUG");
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      if (log_level)
Packit ae235b
	{
Packit ae235b
	  strcat (level_prefix, "LOG-");
Packit ae235b
	  format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	strcat (level_prefix, "LOG");
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strcat (level_prefix, color_reset (use_color));
Packit ae235b
Packit ae235b
  if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
    strcat (level_prefix, " (recursed)");
Packit ae235b
  if (log_level & ALERT_LEVELS)
Packit ae235b
    strcat (level_prefix, " **");
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
Packit ae235b
    win32_keep_fatal_message = TRUE;
Packit ae235b
#endif
Packit ae235b
  return to_stdout ? stdout : stderr;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  gchar          *log_domain;
Packit ae235b
  GLogLevelFlags  log_level;
Packit ae235b
  gchar          *pattern;
Packit ae235b
} GTestExpectedMessage;
Packit ae235b
Packit ae235b
static GSList *expected_messages = NULL;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_logv:
Packit ae235b
 * @log_domain: (nullable): the log domain, or %NULL for the default ""
Packit ae235b
 * application domain
Packit ae235b
 * @log_level: the log level
Packit ae235b
 * @format: the message format. See the printf() documentation
Packit ae235b
 * @args: the parameters to insert into the format string
Packit ae235b
 *
Packit ae235b
 * Logs an error or debugging message.
Packit ae235b
 *
Packit ae235b
 * If the log level has been set as fatal, the abort()
Packit ae235b
 * function is called to terminate the program.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * If [structured logging is enabled][using-structured-logging] this will
Packit ae235b
 * output via the structured log writer function (see g_log_set_writer_func()).
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_logv (const gchar   *log_domain,
Packit ae235b
	GLogLevelFlags log_level,
Packit ae235b
	const gchar   *format,
Packit ae235b
	va_list	       args)
Packit ae235b
{
Packit ae235b
  gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
Packit ae235b
  gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
Packit ae235b
  gchar buffer[1025], *msg, *msg_alloc = NULL;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  log_level &= G_LOG_LEVEL_MASK;
Packit ae235b
  if (!log_level)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
    {
Packit ae235b
      /* we use a stack buffer of fixed size, since we're likely
Packit ae235b
       * in an out-of-memory situation
Packit ae235b
       */
Packit ae235b
      gsize size G_GNUC_UNUSED;
Packit ae235b
Packit ae235b
      size = _g_vsnprintf (buffer, 1024, format, args);
Packit ae235b
      msg = buffer;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    msg = msg_alloc = g_strdup_vprintf (format, args);
Packit ae235b
Packit ae235b
  if (expected_messages)
Packit ae235b
    {
Packit ae235b
      GTestExpectedMessage *expected = expected_messages->data;
Packit ae235b
Packit ae235b
      if (g_strcmp0 (expected->log_domain, log_domain) == 0 &&
Packit ae235b
          ((log_level & expected->log_level) == expected->log_level) &&
Packit ae235b
          g_pattern_match_simple (expected->pattern, msg))
Packit ae235b
        {
Packit ae235b
          expected_messages = g_slist_delete_link (expected_messages,
Packit ae235b
                                                   expected_messages);
Packit ae235b
          g_free (expected->log_domain);
Packit ae235b
          g_free (expected->pattern);
Packit ae235b
          g_free (expected);
Packit ae235b
          g_free (msg_alloc);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
      else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
Packit ae235b
        {
Packit ae235b
          gchar level_prefix[STRING_BUFFER_SIZE];
Packit ae235b
          gchar *expected_message;
Packit ae235b
Packit ae235b
          mklevel_prefix (level_prefix, expected->log_level, FALSE);
Packit ae235b
          expected_message = g_strdup_printf ("Did not see expected message %s-%s: %s",
Packit ae235b
                                              expected->log_domain ? expected->log_domain : "**",
Packit ae235b
                                              level_prefix, expected->pattern);
Packit ae235b
          g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, expected_message, NULL);
Packit ae235b
          g_free (expected_message);
Packit ae235b
Packit ae235b
          log_level |= G_LOG_FLAG_FATAL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
Packit ae235b
    {
Packit ae235b
      GLogLevelFlags test_level;
Packit ae235b
Packit ae235b
      test_level = 1 << i;
Packit ae235b
      if (log_level & test_level)
Packit ae235b
	{
Packit ae235b
	  GLogDomain *domain;
Packit ae235b
	  GLogFunc log_func;
Packit ae235b
	  GLogLevelFlags domain_fatal_mask;
Packit ae235b
	  gpointer data = NULL;
Packit ae235b
          gboolean masquerade_fatal = FALSE;
Packit ae235b
          guint depth;
Packit ae235b
Packit ae235b
	  if (was_fatal)
Packit ae235b
	    test_level |= G_LOG_FLAG_FATAL;
Packit ae235b
	  if (was_recursion)
Packit ae235b
	    test_level |= G_LOG_FLAG_RECURSION;
Packit ae235b
Packit ae235b
	  /* check recursion and lookup handler */
Packit ae235b
	  g_mutex_lock (&g_messages_lock);
Packit ae235b
          depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
Packit ae235b
	  domain = g_log_find_domain_L (log_domain ? log_domain : "");
Packit ae235b
	  if (depth)
Packit ae235b
	    test_level |= G_LOG_FLAG_RECURSION;
Packit ae235b
	  depth++;
Packit ae235b
	  domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
Packit ae235b
	  if ((domain_fatal_mask | g_log_always_fatal) & test_level)
Packit ae235b
	    test_level |= G_LOG_FLAG_FATAL;
Packit ae235b
	  if (test_level & G_LOG_FLAG_RECURSION)
Packit ae235b
	    log_func = _g_log_fallback_handler;
Packit ae235b
	  else
Packit ae235b
	    log_func = g_log_domain_get_handler_L (domain, test_level, &data);
Packit ae235b
	  domain = NULL;
Packit ae235b
	  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
	  g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
Packit ae235b
Packit ae235b
          log_func (log_domain, test_level, msg, data);
Packit ae235b
Packit ae235b
          if ((test_level & G_LOG_FLAG_FATAL)
Packit ae235b
              && !(test_level & G_LOG_LEVEL_ERROR))
Packit ae235b
            {
Packit ae235b
              masquerade_fatal = fatal_log_func
Packit ae235b
                && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
Packit ae235b
            {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
              if (win32_keep_fatal_message)
Packit ae235b
                {
Packit ae235b
                  gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
                  MessageBox (NULL, locale_msg, NULL,
Packit ae235b
                              MB_ICONERROR|MB_SETFOREGROUND);
Packit ae235b
                }
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
              _g_log_abort (!(test_level & G_LOG_FLAG_RECURSION));
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  depth--;
Packit ae235b
	  g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (msg_alloc);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log:
Packit ae235b
 * @log_domain: (nullable): the log domain, usually #G_LOG_DOMAIN, or %NULL
Packit ae235b
 * for the default
Packit ae235b
 * @log_level: the log level, either from #GLogLevelFlags
Packit ae235b
 *     or a user-defined level
Packit ae235b
 * @format: the message format. See the printf() documentation
Packit ae235b
 * @...: the parameters to insert into the format string
Packit ae235b
 *
Packit ae235b
 * Logs an error or debugging message.
Packit ae235b
 *
Packit ae235b
 * If the log level has been set as fatal, the abort()
Packit ae235b
 * function is called to terminate the program.
Packit ae235b
 *
Packit ae235b
 * If g_log_default_handler() is used as the log handler function, a new-line
Packit ae235b
 * character will automatically be appended to @..., and need not be entered
Packit ae235b
 * manually.
Packit ae235b
 *
Packit ae235b
 * If [structured logging is enabled][using-structured-logging] this will
Packit ae235b
 * output via the structured log writer function (see g_log_set_writer_func()).
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_log (const gchar   *log_domain,
Packit ae235b
       GLogLevelFlags log_level,
Packit ae235b
       const gchar   *format,
Packit ae235b
       ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  
Packit ae235b
  va_start (args, format);
Packit ae235b
  g_logv (log_domain, log_level, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Return value must be 1 byte long (plus nul byte).
Packit ae235b
 * Reference: http://man7.org/linux/man-pages/man3/syslog.3.html#DESCRIPTION
Packit ae235b
 */
Packit ae235b
static const gchar *
Packit ae235b
log_level_to_priority (GLogLevelFlags log_level)
Packit ae235b
{
Packit ae235b
  if (log_level & G_LOG_LEVEL_ERROR)
Packit ae235b
    return "3";
Packit ae235b
  else if (log_level & G_LOG_LEVEL_CRITICAL)
Packit ae235b
    return "4";
Packit ae235b
  else if (log_level & G_LOG_LEVEL_WARNING)
Packit ae235b
    return "4";
Packit ae235b
  else if (log_level & G_LOG_LEVEL_MESSAGE)
Packit ae235b
    return "5";
Packit ae235b
  else if (log_level & G_LOG_LEVEL_INFO)
Packit ae235b
    return "6";
Packit ae235b
  else if (log_level & G_LOG_LEVEL_DEBUG)
Packit ae235b
    return "7";
Packit ae235b
Packit ae235b
  /* Default to LOG_NOTICE for custom log levels. */
Packit ae235b
  return "5";
Packit ae235b
}
Packit ae235b
Packit ae235b
static FILE *
Packit ae235b
log_level_to_file (GLogLevelFlags log_level)
Packit ae235b
{
Packit ae235b
  if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL |
Packit ae235b
                   G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE))
Packit ae235b
    return stderr;
Packit ae235b
  else
Packit ae235b
    return stdout;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
log_level_to_color (GLogLevelFlags log_level,
Packit ae235b
                    gboolean       use_color)
Packit ae235b
{
Packit ae235b
  /* we may not call _any_ GLib functions here */
Packit ae235b
Packit ae235b
  if (!use_color)
Packit ae235b
    return "";
Packit ae235b
Packit ae235b
  if (log_level & G_LOG_LEVEL_ERROR)
Packit ae235b
    return "\033[1;31m"; /* red */
Packit ae235b
  else if (log_level & G_LOG_LEVEL_CRITICAL)
Packit ae235b
    return "\033[1;35m"; /* magenta */
Packit ae235b
  else if (log_level & G_LOG_LEVEL_WARNING)
Packit ae235b
    return "\033[1;33m"; /* yellow */
Packit ae235b
  else if (log_level & G_LOG_LEVEL_MESSAGE)
Packit ae235b
    return "\033[1;32m"; /* green */
Packit ae235b
  else if (log_level & G_LOG_LEVEL_INFO)
Packit ae235b
    return "\033[1;32m"; /* green */
Packit ae235b
  else if (log_level & G_LOG_LEVEL_DEBUG)
Packit ae235b
    return "\033[1;32m"; /* green */
Packit ae235b
Packit ae235b
  /* No color for custom log levels. */
Packit ae235b
  return "";
Packit ae235b
}
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
color_reset (gboolean use_color)
Packit ae235b
{
Packit ae235b
  /* we may not call _any_ GLib functions here */
Packit ae235b
Packit ae235b
  if (!use_color)
Packit ae235b
    return "";
Packit ae235b
Packit ae235b
  return "\033[0m";
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* We might be using tty emulators such as mintty, so try to detect it, if we passed in a valid FD
Packit ae235b
 * so we need to check the name of the pipe if _isatty (fd) == 0
Packit ae235b
 */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
win32_is_pipe_tty (int fd)
Packit ae235b
{
Packit ae235b
  gboolean result = FALSE;
Packit ae235b
  int error;
Packit ae235b
  HANDLE h_fd;
Packit ae235b
  FILE_NAME_INFO *info = NULL;
Packit ae235b
  gint info_size = sizeof (FILE_NAME_INFO) + sizeof (WCHAR) * MAX_PATH;
Packit ae235b
  wchar_t *name = NULL;
Packit ae235b
  gint length;
Packit ae235b
Packit ae235b
  /* XXX: Remove once XP support really dropped */
Packit ae235b
#if _WIN32_WINNT < 0x0600
Packit ae235b
  HANDLE h_kerneldll = NULL;
Packit ae235b
  fGetFileInformationByHandleEx *GetFileInformationByHandleEx;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  h_fd = (HANDLE) _get_osfhandle (fd);
Packit ae235b
Packit ae235b
  if (h_fd == INVALID_HANDLE_VALUE || GetFileType (h_fd) != FILE_TYPE_PIPE)
Packit ae235b
    goto done_query;
Packit ae235b
Packit ae235b
  /* The following check is available on Vista or later, so on XP, no color support */
Packit ae235b
  /* mintty uses a pipe, in the form of \{cygwin|msys}-xxxxxxxxxxxxxxxx-ptyN-{from|to}-master */
Packit ae235b
Packit ae235b
  /* XXX: Remove once XP support really dropped */
Packit ae235b
#if _WIN32_WINNT < 0x0600
Packit ae235b
  h_kerneldll = LoadLibraryW (L"kernel32.dll");
Packit ae235b
Packit ae235b
  if (h_kerneldll == NULL)
Packit ae235b
    goto done_query;
Packit ae235b
Packit ae235b
  GetFileInformationByHandleEx =
Packit ae235b
    (fGetFileInformationByHandleEx *) GetProcAddress (h_kerneldll, "GetFileInformationByHandleEx");
Packit ae235b
Packit ae235b
  if (GetFileInformationByHandleEx == NULL)
Packit ae235b
    goto done_query;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  info = g_try_malloc (info_size);
Packit ae235b
Packit ae235b
  if (info == NULL ||
Packit ae235b
      !GetFileInformationByHandleEx (h_fd, FileNameInfo, info, info_size))
Packit ae235b
    goto done_query;
Packit ae235b
Packit ae235b
  info->FileName[info->FileNameLength / sizeof (WCHAR)] = L'\0';
Packit ae235b
  name = info->FileName;
Packit ae235b
Packit ae235b
  length = wcslen (L"\\cygwin-");
Packit ae235b
  if (wcsncmp (name, L"\\cygwin-", length))
Packit ae235b
    {
Packit ae235b
      length = wcslen (L"\\msys-");
Packit ae235b
      if (wcsncmp (name, L"\\msys-", length))
Packit ae235b
        goto done_query;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  name += length;
Packit ae235b
  length = wcsspn (name, L"0123456789abcdefABCDEF");
Packit ae235b
  if (length != 16)
Packit ae235b
    goto done_query;
Packit ae235b
Packit ae235b
  name += length;
Packit ae235b
  length = wcslen (L"-pty");
Packit ae235b
  if (wcsncmp (name, L"-pty", length))
Packit ae235b
    goto done_query;
Packit ae235b
Packit ae235b
  name += length;
Packit ae235b
  length = wcsspn (name, L"0123456789");
Packit ae235b
  if (length != 1)
Packit ae235b
    goto done_query;
Packit ae235b
Packit ae235b
  name += length;
Packit ae235b
  length = wcslen (L"-to-master");
Packit ae235b
  if (wcsncmp (name, L"-to-master", length))
Packit ae235b
    {
Packit ae235b
      length = wcslen (L"-from-master");
Packit ae235b
      if (wcsncmp (name, L"-from-master", length))
Packit ae235b
        goto done_query;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = TRUE;
Packit ae235b
Packit ae235b
done_query:
Packit ae235b
  if (info != NULL)
Packit ae235b
    g_free (info);
Packit ae235b
Packit ae235b
  /* XXX: Remove once XP support really dropped */
Packit ae235b
#if _WIN32_WINNT < 0x0600
Packit ae235b
  if (h_kerneldll != NULL)
Packit ae235b
    FreeLibrary (h_kerneldll);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#pragma GCC diagnostic push
Packit ae235b
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_structured:
Packit ae235b
 * @log_domain: log domain, usually %G_LOG_DOMAIN
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @...: key-value pairs of structured data to add to the log entry, followed
Packit ae235b
 *    by the key "MESSAGE", followed by a printf()-style message format,
Packit ae235b
 *    followed by parameters to insert in the format string
Packit ae235b
 *
Packit ae235b
 * Log a message with structured data. The message will be passed through to
Packit ae235b
 * the log writer set by the application using g_log_set_writer_func(). If the
Packit ae235b
 * message is fatal (i.e. its log level is %G_LOG_LEVEL_ERROR), the program will
Packit ae235b
 * be aborted at the end of this function. If the log writer returns
Packit ae235b
 * %G_LOG_WRITER_UNHANDLED (failure), no other fallback writers will be tried.
Packit ae235b
 * See the documentation for #GLogWriterFunc for information on chaining
Packit ae235b
 * writers.
Packit ae235b
 *
Packit ae235b
 * The structured data is provided as key–value pairs, where keys are UTF-8
Packit ae235b
 * strings, and values are arbitrary pointers — typically pointing to UTF-8
Packit ae235b
 * strings, but that is not a requirement. To pass binary (non-nul-terminated)
Packit ae235b
 * structured data, use g_log_structured_array(). The keys for structured data
Packit ae235b
 * should follow the [systemd journal
Packit ae235b
 * fields](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html)
Packit ae235b
 * specification. It is suggested that custom keys are namespaced according to
Packit ae235b
 * the code which sets them. For example, custom keys from GLib all have a
Packit ae235b
 * `GLIB_` prefix.
Packit ae235b
 *
Packit ae235b
 * The @log_domain will be converted into a `GLIB_DOMAIN` field. @log_level will
Packit ae235b
 * be converted into a
Packit ae235b
 * [`PRIORITY`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#PRIORITY=)
Packit ae235b
 * field. The format string will have its placeholders substituted for the provided
Packit ae235b
 * values and be converted into a
Packit ae235b
 * [`MESSAGE`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#MESSAGE=)
Packit ae235b
 * field.
Packit ae235b
 *
Packit ae235b
 * Other fields you may commonly want to pass into this function:
Packit ae235b
 *
Packit ae235b
 *  * [`MESSAGE_ID`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#MESSAGE_ID=)
Packit ae235b
 *  * [`CODE_FILE`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#CODE_FILE=)
Packit ae235b
 *  * [`CODE_LINE`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#CODE_LINE=)
Packit ae235b
 *  * [`CODE_FUNC`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#CODE_FUNC=)
Packit ae235b
 *  * [`ERRNO`](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#ERRNO=)
Packit ae235b
 *
Packit ae235b
 * Note that `CODE_FILE`, `CODE_LINE` and `CODE_FUNC` are automatically set by
Packit ae235b
 * the logging macros, G_DEBUG_HERE(), g_message(), g_warning(), g_critical(),
Packit ae235b
 * g_error(), etc, if the symbols `G_LOG_USE_STRUCTURED` is defined before including
Packit ae235b
 * glib.h.
Packit ae235b
 *
Packit ae235b
 * For example:
Packit ae235b
 * |[
Packit ae235b
 * g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
Packit ae235b
 *                   "MESSAGE_ID", "06d4df59e6c24647bfe69d2c27ef0b4e",
Packit ae235b
 *                   "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
Packit ae235b
 *                   "MESSAGE", "This is a debug message about pointer %p and integer %u.",
Packit ae235b
 *                   some_pointer, some_integer);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Note that each `MESSAGE_ID` must be [uniquely and randomly
Packit ae235b
 * generated](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html#MESSAGE_ID=).
Packit ae235b
 * If adding a `MESSAGE_ID`, consider shipping a [message
Packit ae235b
 * catalog](https://www.freedesktop.org/wiki/Software/systemd/catalog/) with
Packit ae235b
 * your software.
Packit ae235b
 *
Packit ae235b
 * To pass a user data pointer to the log writer function which is specific to
Packit ae235b
 * this logging call, you must use g_log_structured_array() and pass the pointer
Packit ae235b
 * as a field with #GLogField.length set to zero, otherwise it will be
Packit ae235b
 * interpreted as a string.
Packit ae235b
 *
Packit ae235b
 * For example:
Packit ae235b
 * |[
Packit ae235b
 * const GLogField fields[] = {
Packit ae235b
 *   { "MESSAGE", "This is a debug message.", -1 },
Packit ae235b
 *   { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
Packit ae235b
 *   { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 },
Packit ae235b
 *   { "MY_APPLICATION_STATE", state_object, 0 },
Packit ae235b
 * };
Packit ae235b
 * g_log_structured_array (G_LOG_LEVEL_DEBUG, fields, G_N_ELEMENTS (fields));
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Note also that, even if no other structured fields are specified, there
Packit ae235b
 * must always be a `MESSAGE` key before the format string. The `MESSAGE`-format
Packit ae235b
 * pair has to be the last of the key-value pairs, and `MESSAGE` is the only
Packit ae235b
 * field for which printf()-style formatting is supported.
Packit ae235b
 *
Packit ae235b
 * The default writer function for `stdout` and `stderr` will automatically
Packit ae235b
 * append a new-line character after the message, so you should not add one
Packit ae235b
 * manually to the format string.
Packit ae235b
 *
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_log_structured (const gchar    *log_domain,
Packit ae235b
                  GLogLevelFlags  log_level,
Packit ae235b
                  ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gchar buffer[1025], *message_allocated = NULL;
Packit ae235b
  const char *format;
Packit ae235b
  const gchar *message;
Packit ae235b
  gpointer p;
Packit ae235b
  gsize n_fields, i;
Packit ae235b
  GLogField stack_fields[16];
Packit ae235b
  GLogField *fields = stack_fields;
Packit ae235b
  GLogField *fields_allocated = NULL;
Packit ae235b
  GArray *array = NULL;
Packit ae235b
Packit ae235b
  va_start (args, log_level);
Packit ae235b
Packit ae235b
  /* MESSAGE and PRIORITY are a given */
Packit ae235b
  n_fields = 2;
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    n_fields++;
Packit ae235b
Packit ae235b
  for (p = va_arg (args, gchar *), i = n_fields;
Packit ae235b
       strcmp (p, "MESSAGE") != 0;
Packit ae235b
       p = va_arg (args, gchar *), i++)
Packit ae235b
    {
Packit ae235b
      GLogField field;
Packit ae235b
      const gchar *key = p;
Packit ae235b
      gconstpointer value = va_arg (args, gpointer);
Packit ae235b
Packit ae235b
      field.key = key;
Packit ae235b
      field.value = value;
Packit ae235b
      field.length = -1;
Packit ae235b
Packit ae235b
      if (i < 16)
Packit ae235b
        stack_fields[i] = field;
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* Don't allow dynamic allocation, since we're likely
Packit ae235b
           * in an out-of-memory situation. For lack of a better solution,
Packit ae235b
           * just ignore further key-value pairs.
Packit ae235b
           */
Packit ae235b
          if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
            continue;
Packit ae235b
Packit ae235b
          if (i == 16)
Packit ae235b
            {
Packit ae235b
              array = g_array_sized_new (FALSE, FALSE, sizeof (GLogField), 32);
Packit ae235b
              g_array_append_vals (array, stack_fields, 16);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_array_append_val (array, field);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  n_fields = i;
Packit ae235b
Packit ae235b
  if (array)
Packit ae235b
    fields = fields_allocated = (GLogField *) g_array_free (array, FALSE);
Packit ae235b
Packit ae235b
  format = va_arg (args, gchar *);
Packit ae235b
Packit ae235b
  if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
    {
Packit ae235b
      /* we use a stack buffer of fixed size, since we're likely
Packit ae235b
       * in an out-of-memory situation
Packit ae235b
       */
Packit ae235b
      gsize size G_GNUC_UNUSED;
Packit ae235b
Packit ae235b
      size = _g_vsnprintf (buffer, sizeof (buffer), format, args);
Packit ae235b
      message = buffer;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      message = message_allocated = g_strdup_vprintf (format, args);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Add MESSAGE, PRIORITY and GLIB_DOMAIN. */
Packit ae235b
  fields[0].key = "MESSAGE";
Packit ae235b
  fields[0].value = message;
Packit ae235b
  fields[0].length = -1;
Packit ae235b
Packit ae235b
  fields[1].key = "PRIORITY";
Packit ae235b
  fields[1].value = log_level_to_priority (log_level);
Packit ae235b
  fields[1].length = -1;
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    {
Packit ae235b
      fields[2].key = "GLIB_DOMAIN";
Packit ae235b
      fields[2].value = log_domain;
Packit ae235b
      fields[2].length = -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Log it. */
Packit ae235b
  g_log_structured_array (log_level, fields, n_fields);
Packit ae235b
Packit ae235b
  g_free (fields_allocated);
Packit ae235b
  g_free (message_allocated);
Packit ae235b
Packit ae235b
  va_end (args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_variant:
Packit ae235b
 * @log_domain: (nullable): log domain, usually %G_LOG_DOMAIN
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @fields: a dictionary (#GVariant of the type %G_VARIANT_TYPE_VARDICT)
Packit ae235b
 * containing the key-value pairs of message data.
Packit ae235b
 *
Packit ae235b
 * Log a message with structured data, accepting the data within a #GVariant. This
Packit ae235b
 * version is especially useful for use in other languages, via introspection.
Packit ae235b
 *
Packit ae235b
 * The only mandatory item in the @fields dictionary is the "MESSAGE" which must
Packit ae235b
 * contain the text shown to the user.
Packit ae235b
 *
Packit ae235b
 * The values in the @fields dictionary are likely to be of type String
Packit ae235b
 * (#G_VARIANT_TYPE_STRING). Array of bytes (#G_VARIANT_TYPE_BYTESTRING) is also
Packit ae235b
 * supported. In this case the message is handled as binary and will be forwarded
Packit ae235b
 * to the log writer as such. The size of the array should not be higher than
Packit ae235b
 * %G_MAXSSIZE. Otherwise it will be truncated to this size. For other types
Packit ae235b
 * g_variant_print() will be used to convert the value into a string.
Packit ae235b
 *
Packit ae235b
 * For more details on its usage and about the parameters, see g_log_structured().
Packit ae235b
 *
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
Packit ae235b
void
Packit ae235b
g_log_variant (const gchar    *log_domain,
Packit ae235b
               GLogLevelFlags  log_level,
Packit ae235b
               GVariant       *fields)
Packit ae235b
{
Packit ae235b
  GVariantIter iter;
Packit ae235b
  GVariant *value;
Packit ae235b
  gchar *key;
Packit ae235b
  GArray *fields_array;
Packit ae235b
  GLogField field;
Packit ae235b
  GSList *values_list, *print_list;
Packit ae235b
Packit ae235b
  g_return_if_fail (g_variant_is_of_type (fields, G_VARIANT_TYPE_VARDICT));
Packit ae235b
Packit ae235b
  values_list = print_list = NULL;
Packit ae235b
  fields_array = g_array_new (FALSE, FALSE, sizeof (GLogField));
Packit ae235b
Packit ae235b
  field.key = "PRIORITY";
Packit ae235b
  field.value = log_level_to_priority (log_level);
Packit ae235b
  field.length = -1;
Packit ae235b
  g_array_append_val (fields_array, field);
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    {
Packit ae235b
      field.key = "GLIB_DOMAIN";
Packit ae235b
      field.value = log_domain;
Packit ae235b
      field.length = -1;
Packit ae235b
      g_array_append_val (fields_array, field);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_iter_init (&iter, fields);
Packit ae235b
  while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
Packit ae235b
    {
Packit ae235b
      gboolean defer_unref = TRUE;
Packit ae235b
Packit ae235b
      field.key = key;
Packit ae235b
      field.length = -1;
Packit ae235b
Packit ae235b
      if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
Packit ae235b
        {
Packit ae235b
          field.value = g_variant_get_string (value, NULL);
Packit ae235b
        }
Packit ae235b
      else if (g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING))
Packit ae235b
        {
Packit ae235b
          gsize s;
Packit ae235b
          field.value = g_variant_get_fixed_array (value, &s, sizeof (guchar));
Packit ae235b
          if (G_LIKELY (s <= G_MAXSSIZE))
Packit ae235b
            {
Packit ae235b
              field.length = s;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
               _g_fprintf (stderr,
Packit ae235b
                           "Byte array too large (%" G_GSIZE_FORMAT " bytes)"
Packit ae235b
                           " passed to g_log_variant(). Truncating to " G_STRINGIFY (G_MAXSSIZE)
Packit ae235b
                           " bytes.", s);
Packit ae235b
              field.length = G_MAXSSIZE;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          char *s = g_variant_print (value, FALSE);
Packit ae235b
          field.value = s;
Packit ae235b
          print_list = g_slist_prepend (print_list, s);
Packit ae235b
          defer_unref = FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_array_append_val (fields_array, field);
Packit ae235b
Packit ae235b
      if (G_LIKELY (defer_unref))
Packit ae235b
        values_list = g_slist_prepend (values_list, value);
Packit ae235b
      else
Packit ae235b
        g_variant_unref (value);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Log it. */
Packit ae235b
  g_log_structured_array (log_level, (GLogField *) fields_array->data, fields_array->len);
Packit ae235b
Packit ae235b
  g_array_free (fields_array, TRUE);
Packit ae235b
  g_slist_free_full (values_list, (GDestroyNotify) g_variant_unref);
Packit ae235b
  g_slist_free_full (print_list, g_free);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
#pragma GCC diagnostic pop
Packit ae235b
Packit ae235b
static GLogWriterOutput _g_log_writer_fallback (GLogLevelFlags   log_level,
Packit ae235b
                                                const GLogField *fields,
Packit ae235b
                                                gsize            n_fields,
Packit ae235b
                                                gpointer         user_data);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_structured_array:
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @fields: (array length=n_fields): key–value pairs of structured data to add
Packit ae235b
 *    to the log message
Packit ae235b
 * @n_fields: number of elements in the @fields array
Packit ae235b
 *
Packit ae235b
 * Log a message with structured data. The message will be passed through to the
Packit ae235b
 * log writer set by the application using g_log_set_writer_func(). If the
Packit ae235b
 * message is fatal (i.e. its log level is %G_LOG_LEVEL_ERROR), the program will
Packit ae235b
 * be aborted at the end of this function.
Packit ae235b
 *
Packit ae235b
 * See g_log_structured() for more documentation.
Packit ae235b
 *
Packit ae235b
 * This assumes that @log_level is already present in @fields (typically as the
Packit ae235b
 * `PRIORITY` field).
Packit ae235b
 *
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_log_structured_array (GLogLevelFlags   log_level,
Packit ae235b
                        const GLogField *fields,
Packit ae235b
                        gsize            n_fields)
Packit ae235b
{
Packit ae235b
  GLogWriterFunc writer_func;
Packit ae235b
  gpointer writer_user_data;
Packit ae235b
  gboolean recursion;
Packit ae235b
  guint depth;
Packit ae235b
Packit ae235b
  if (n_fields == 0)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  /* Check for recursion and look up the writer function. */
Packit ae235b
  depth = GPOINTER_TO_UINT (g_private_get (&g_log_structured_depth));
Packit ae235b
  recursion = (depth > 0);
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
Packit ae235b
  writer_func = recursion ? _g_log_writer_fallback : log_writer_func;
Packit ae235b
  writer_user_data = log_writer_user_data;
Packit ae235b
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  /* Write the log entry. */
Packit ae235b
  g_private_set (&g_log_structured_depth, GUINT_TO_POINTER (++depth));
Packit ae235b
Packit ae235b
  g_assert (writer_func != NULL);
Packit ae235b
  writer_func (log_level, fields, n_fields, writer_user_data);
Packit ae235b
Packit ae235b
  g_private_set (&g_log_structured_depth, GUINT_TO_POINTER (--depth));
Packit ae235b
Packit ae235b
  /* Abort if the message was fatal. */
Packit ae235b
  if (log_level & G_LOG_FATAL_MASK)
Packit ae235b
    _g_log_abort (!(log_level & G_LOG_FLAG_RECURSION));
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Semi-private helper function to implement the g_message() (etc.) macros
Packit ae235b
 * with support for G_GNUC_PRINTF so that @message_format can be checked
Packit ae235b
 * with -Wformat. */
Packit ae235b
void
Packit ae235b
g_log_structured_standard (const gchar    *log_domain,
Packit ae235b
                           GLogLevelFlags  log_level,
Packit ae235b
                           const gchar    *file,
Packit ae235b
                           const gchar    *line,
Packit ae235b
                           const gchar    *func,
Packit ae235b
                           const gchar    *message_format,
Packit ae235b
                           ...)
Packit ae235b
{
Packit ae235b
  GLogField fields[] =
Packit ae235b
    {
Packit ae235b
      { "PRIORITY", log_level_to_priority (log_level), -1 },
Packit ae235b
      { "CODE_FILE", file, -1 },
Packit ae235b
      { "CODE_LINE", line, -1 },
Packit ae235b
      { "CODE_FUNC", func, -1 },
Packit ae235b
      /* Filled in later: */
Packit ae235b
      { "MESSAGE", NULL, -1 },
Packit ae235b
      /* If @log_domain is %NULL, we will not pass this field: */
Packit ae235b
      { "GLIB_DOMAIN", log_domain, -1 },
Packit ae235b
    };
Packit ae235b
  gsize n_fields;
Packit ae235b
  gchar *message_allocated = NULL;
Packit ae235b
  gchar buffer[1025];
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, message_format);
Packit ae235b
Packit ae235b
  if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
    {
Packit ae235b
      /* we use a stack buffer of fixed size, since we're likely
Packit ae235b
       * in an out-of-memory situation
Packit ae235b
       */
Packit ae235b
      gsize size G_GNUC_UNUSED;
Packit ae235b
Packit ae235b
      size = _g_vsnprintf (buffer, sizeof (buffer), message_format, args);
Packit ae235b
      fields[4].value = buffer;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      fields[4].value = message_allocated = g_strdup_vprintf (message_format, args);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  n_fields = G_N_ELEMENTS (fields) - ((log_domain == NULL) ? 1 : 0);
Packit ae235b
  g_log_structured_array (log_level, fields, n_fields);
Packit ae235b
Packit ae235b
  g_free (message_allocated);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_set_writer_func:
Packit ae235b
 * @func: log writer function, which must not be %NULL
Packit ae235b
 * @user_data: (closure func): user data to pass to @func
Packit ae235b
 * @user_data_free: (destroy func): function to free @user_data once it’s
Packit ae235b
 *    finished with, if non-%NULL
Packit ae235b
 *
Packit ae235b
 * Set a writer function which will be called to format and write out each log
Packit ae235b
 * message. Each program should set a writer function, or the default writer
Packit ae235b
 * (g_log_writer_default()) will be used.
Packit ae235b
 *
Packit ae235b
 * Libraries **must not** call this function — only programs are allowed to
Packit ae235b
 * install a writer function, as there must be a single, central point where
Packit ae235b
 * log messages are formatted and outputted.
Packit ae235b
 *
Packit ae235b
 * There can only be one writer function. It is an error to set more than one.
Packit ae235b
 *
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_log_set_writer_func (GLogWriterFunc func,
Packit ae235b
                       gpointer       user_data,
Packit ae235b
                       GDestroyNotify user_data_free)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (func != NULL);
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  log_writer_func = func;
Packit ae235b
  log_writer_user_data = user_data;
Packit ae235b
  log_writer_user_data_free = user_data_free;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_writer_supports_color:
Packit ae235b
 * @output_fd: output file descriptor to check
Packit ae235b
 *
Packit ae235b
 * Check whether the given @output_fd file descriptor supports ANSI color
Packit ae235b
 * escape sequences. If so, they can safely be used when formatting log
Packit ae235b
 * messages.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if ANSI color escapes are supported, %FALSE otherwise
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_log_writer_supports_color (gint output_fd)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gboolean result = FALSE;
Packit ae235b
Packit ae235b
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
Packit ae235b
  _invalid_parameter_handler oldHandler, newHandler;
Packit ae235b
  int prev_report_mode = 0;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_val_if_fail (output_fd >= 0, FALSE);
Packit ae235b
Packit ae235b
  /* FIXME: This check could easily be expanded in future to be more robust
Packit ae235b
   * against different types of terminal, which still vary in their color
Packit ae235b
   * support. cmd.exe on Windows, for example, supports ANSI colors only
Packit ae235b
   * from Windows 10 onwards; bash on Windows has always supported ANSI colors.
Packit ae235b
   * The Windows 10 color support is supported on:
Packit ae235b
   * -Output in the cmd.exe, MSYS/Cygwin standard consoles.
Packit ae235b
   * -Output in the cmd.exe, MSYS/Cygwin piped to the less program.
Packit ae235b
   * but not:
Packit ae235b
   * -Output in Cygwin via mintty (https://github.com/mintty/mintty/issues/482)
Packit ae235b
   * -Color code output when output redirected to file (i.e. program 2> some.txt)
Packit ae235b
   *
Packit ae235b
   * On UNIX systems, we probably want to use the functions from terminfo to
Packit ae235b
   * work out whether colors are supported.
Packit ae235b
   *
Packit ae235b
   * Some examples:
Packit ae235b
   *  - https://github.com/chalk/supports-color/blob/9434c93918301a6b47faa01999482adfbf1b715c/index.js#L61
Packit ae235b
   *  - http://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences
Packit ae235b
   *  - http://blog.mmediasys.com/2010/11/24/we-all-love-colors/
Packit ae235b
   *  - http://unix.stackexchange.com/questions/198794/where-does-the-term-environment-variable-default-get-set
Packit ae235b
   */
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
#if (defined (_MSC_VER) && _MSC_VER >= 1400)
Packit ae235b
  /* Set up our empty invalid parameter handler, for isatty(),
Packit ae235b
   * in case of bad fd's passed in for isatty(), so that
Packit ae235b
   * msvcrt80.dll+ won't abort the program
Packit ae235b
   */
Packit ae235b
  newHandler = myInvalidParameterHandler;
Packit ae235b
  oldHandler = _set_invalid_parameter_handler (newHandler);
Packit ae235b
Packit ae235b
  /* Disable the message box for assertions. */
Packit ae235b
  prev_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (g_win32_check_windows_version (10, 0, 0, G_WIN32_OS_ANY))
Packit ae235b
    {
Packit ae235b
      HANDLE h_output;
Packit ae235b
      DWORD dw_mode;
Packit ae235b
Packit ae235b
      if (_isatty (output_fd))
Packit ae235b
        {
Packit ae235b
          h_output = (HANDLE) _get_osfhandle (output_fd);
Packit ae235b
Packit ae235b
          if (!GetConsoleMode (h_output, &dw_mode))
Packit ae235b
            goto reset_invalid_param_handler;
Packit ae235b
Packit ae235b
          if (dw_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
Packit ae235b
            result = TRUE;
Packit ae235b
Packit ae235b
          if (!SetConsoleMode (h_output, dw_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING))
Packit ae235b
            goto reset_invalid_param_handler;
Packit ae235b
Packit ae235b
          result = TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* FIXME: Support colored outputs for structured logs for pre-Windows 10,
Packit ae235b
   *        perhaps using WriteConsoleOutput or SetConsoleTextAttribute
Packit ae235b
   *        (bug 775468), on standard Windows consoles, such as cmd.exe
Packit ae235b
   */
Packit ae235b
  if (!result)
Packit ae235b
    result = win32_is_pipe_tty (output_fd);
Packit ae235b
Packit ae235b
reset_invalid_param_handler:
Packit ae235b
#if defined (_MSC_VER) && (_MSC_VER >= 1400)
Packit ae235b
      _CrtSetReportMode(_CRT_ASSERT, prev_report_mode);
Packit ae235b
      _set_invalid_parameter_handler (oldHandler);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
#else
Packit ae235b
  return isatty (output_fd);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
#if defined(__linux__) && !defined(__BIONIC__)
Packit ae235b
static int journal_fd = -1;
Packit ae235b
Packit ae235b
#ifndef SOCK_CLOEXEC
Packit ae235b
#define SOCK_CLOEXEC 0
Packit ae235b
#else
Packit ae235b
#define HAVE_SOCK_CLOEXEC 1
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
open_journal (void)
Packit ae235b
{
Packit ae235b
  if ((journal_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
#ifndef HAVE_SOCK_CLOEXEC
Packit ae235b
  if (fcntl (journal_fd, F_SETFD, FD_CLOEXEC) < 0)
Packit ae235b
    {
Packit ae235b
      close (journal_fd);
Packit ae235b
      journal_fd = -1;
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_writer_is_journald:
Packit ae235b
 * @output_fd: output file descriptor to check
Packit ae235b
 *
Packit ae235b
 * Check whether the given @output_fd file descriptor is a connection to the
Packit ae235b
 * systemd journal, or something else (like a log file or `stdout` or
Packit ae235b
 * `stderr`).
Packit ae235b
 *
Packit ae235b
 * Invalid file descriptors are accepted and return %FALSE, which allows for
Packit ae235b
 * the following construct without needing any additional error handling:
Packit ae235b
 * |[
Packit ae235b
 *   is_journald = g_log_writer_is_journald (fileno (stderr));
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @output_fd points to the journal, %FALSE otherwise
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_log_writer_is_journald (gint output_fd)
Packit ae235b
{
Packit ae235b
#if defined(__linux__) && !defined(__BIONIC__)
Packit ae235b
  /* FIXME: Use the new journal API for detecting whether we’re writing to the
Packit ae235b
   * journal. See: https://github.com/systemd/systemd/issues/2473
Packit ae235b
   */
Packit ae235b
  static gsize initialized;
Packit ae235b
  static gboolean fd_is_journal = FALSE;
Packit ae235b
Packit ae235b
  if (output_fd < 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&initialized))
Packit ae235b
    {
Packit ae235b
      union {
Packit ae235b
        struct sockaddr_storage storage;
Packit ae235b
        struct sockaddr sa;
Packit ae235b
        struct sockaddr_un un;
Packit ae235b
      } addr;
Packit ae235b
      socklen_t addr_len = sizeof(addr);
Packit ae235b
      int err = getpeername (output_fd, &addr.sa, &addr_len);
Packit ae235b
      if (err == 0 && addr.storage.ss_family == AF_UNIX)
Packit ae235b
        fd_is_journal = g_str_has_prefix (addr.un.sun_path, "/run/systemd/journal/");
Packit ae235b
Packit ae235b
      g_once_init_leave (&initialized, TRUE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return fd_is_journal;
Packit ae235b
#else
Packit ae235b
  return FALSE;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static void escape_string (GString *string);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_writer_format_fields:
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @fields: (array length=n_fields): key–value pairs of structured data forming
Packit ae235b
 *    the log message
Packit ae235b
 * @n_fields: number of elements in the @fields array
Packit ae235b
 * @use_color: %TRUE to use ANSI color escape sequences when formatting the
Packit ae235b
 *    message, %FALSE to not
Packit ae235b
 *
Packit ae235b
 * Format a structured log message as a string suitable for outputting to the
Packit ae235b
 * terminal (or elsewhere). This will include the values of all fields it knows
Packit ae235b
 * how to interpret, which includes `MESSAGE` and `GLIB_DOMAIN` (see the
Packit ae235b
 * documentation for g_log_structured()). It does not include values from
Packit ae235b
 * unknown fields.
Packit ae235b
 *
Packit ae235b
 * The returned string does **not** have a trailing new-line character. It is
Packit ae235b
 * encoded in the character set of the current locale, which is not necessarily
Packit ae235b
 * UTF-8.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): string containing the formatted log message, in
Packit ae235b
 *    the character set of the current locale
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_log_writer_format_fields (GLogLevelFlags   log_level,
Packit ae235b
                            const GLogField *fields,
Packit ae235b
                            gsize            n_fields,
Packit ae235b
                            gboolean         use_color)
Packit ae235b
{
Packit ae235b
  gsize i;
Packit ae235b
  const gchar *message = NULL;
Packit ae235b
  const gchar *log_domain = NULL;
Packit ae235b
  gchar level_prefix[STRING_BUFFER_SIZE];
Packit ae235b
  GString *gstring;
Packit ae235b
  gint64 now;
Packit ae235b
  time_t now_secs;
Packit ae235b
  struct tm *now_tm;
Packit ae235b
  gchar time_buf[128];
Packit ae235b
Packit ae235b
  /* Extract some common fields. */
Packit ae235b
  for (i = 0; (message == NULL || log_domain == NULL) && i < n_fields; i++)
Packit ae235b
    {
Packit ae235b
      const GLogField *field = &fields[i];
Packit ae235b
Packit ae235b
      if (g_strcmp0 (field->key, "MESSAGE") == 0)
Packit ae235b
        message = field->value;
Packit ae235b
      else if (g_strcmp0 (field->key, "GLIB_DOMAIN") == 0)
Packit ae235b
        log_domain = field->value;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Format things. */
Packit ae235b
  mklevel_prefix (level_prefix, log_level, use_color);
Packit ae235b
Packit ae235b
  gstring = g_string_new (NULL);
Packit ae235b
  if (log_level & ALERT_LEVELS)
Packit ae235b
    g_string_append (gstring, "\n");
Packit ae235b
  if (!log_domain)
Packit ae235b
    g_string_append (gstring, "** ");
Packit ae235b
Packit ae235b
  if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) ==
Packit ae235b
      (log_level & G_LOG_LEVEL_MASK))
Packit ae235b
    {
Packit ae235b
      const gchar *prg_name = g_get_prgname ();
Packit ae235b
      gulong pid = getpid ();
Packit ae235b
Packit ae235b
      if (prg_name == NULL)
Packit ae235b
        g_string_append_printf (gstring, "(process:%lu): ", pid);
Packit ae235b
      else
Packit ae235b
        g_string_append_printf (gstring, "(%s:%lu): ", prg_name, pid);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (log_domain != NULL)
Packit ae235b
    {
Packit ae235b
      g_string_append (gstring, log_domain);
Packit ae235b
      g_string_append_c (gstring, '-');
Packit ae235b
    }
Packit ae235b
  g_string_append (gstring, level_prefix);
Packit ae235b
Packit ae235b
  g_string_append (gstring, ": ");
Packit ae235b
Packit ae235b
  /* Timestamp */
Packit ae235b
  now = g_get_real_time ();
Packit ae235b
  now_secs = (time_t) (now / 1000000);
Packit ae235b
  now_tm = localtime (&now_secs);
Packit ae235b
  strftime (time_buf, sizeof (time_buf), "%H:%M:%S", now_tm);
Packit ae235b
Packit ae235b
  g_string_append_printf (gstring, "%s%s.%03d%s: ",
Packit ae235b
                          use_color ? "\033[34m" : "",
Packit ae235b
                          time_buf, (gint) ((now / 1000) % 1000),
Packit ae235b
                          color_reset (use_color));
Packit ae235b
Packit ae235b
  if (message == NULL)
Packit ae235b
    {
Packit ae235b
      g_string_append (gstring, "(NULL) message");
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GString *msg;
Packit ae235b
      const gchar *charset;
Packit ae235b
Packit ae235b
      msg = g_string_new (message);
Packit ae235b
      escape_string (msg);
Packit ae235b
Packit ae235b
      if (g_get_charset (&charset))
Packit ae235b
        {
Packit ae235b
          /* charset is UTF-8 already */
Packit ae235b
          g_string_append (gstring, msg->str);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          gchar *lstring = strdup_convert (msg->str, charset);
Packit ae235b
          g_string_append (gstring, lstring);
Packit ae235b
          g_free (lstring);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_string_free (msg, TRUE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_string_free (gstring, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Enable support for the journal if we're on a recent enough Linux */
Packit ae235b
#if defined(__linux__) && !defined(__BIONIC__) && defined(HAVE_MKOSTEMP) && defined(O_CLOEXEC)
Packit ae235b
#define ENABLE_JOURNAL_SENDV
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENABLE_JOURNAL_SENDV
Packit ae235b
static int
Packit ae235b
journal_sendv (struct iovec *iov,
Packit ae235b
               gsize         iovlen)
Packit ae235b
{
Packit ae235b
  int buf_fd = -1;
Packit ae235b
  struct msghdr mh;
Packit ae235b
  struct sockaddr_un sa;
Packit ae235b
  union {
Packit ae235b
    struct cmsghdr cmsghdr;
Packit ae235b
    guint8 buf[CMSG_SPACE(sizeof(int))];
Packit ae235b
  } control;
Packit ae235b
  struct cmsghdr *cmsg;
Packit ae235b
  char path[] = "/dev/shm/journal.XXXXXX";
Packit ae235b
Packit ae235b
  if (journal_fd < 0)
Packit ae235b
    open_journal ();
Packit ae235b
Packit ae235b
  if (journal_fd < 0)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  memset (&sa, 0, sizeof (sa));
Packit ae235b
  sa.sun_family = AF_UNIX;
Packit ae235b
  if (g_strlcpy (sa.sun_path, "/run/systemd/journal/socket", sizeof (sa.sun_path)) >= sizeof (sa.sun_path))
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  memset (&mh, 0, sizeof (mh));
Packit ae235b
  mh.msg_name = &sa;
Packit ae235b
  mh.msg_namelen = offsetof (struct sockaddr_un, sun_path) + strlen (sa.sun_path);
Packit ae235b
  mh.msg_iov = iov;
Packit ae235b
  mh.msg_iovlen = iovlen;
Packit ae235b
Packit ae235b
retry:
Packit ae235b
  if (sendmsg (journal_fd, &mh, MSG_NOSIGNAL) >= 0)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  if (errno == EINTR)
Packit ae235b
    goto retry;
Packit ae235b
Packit ae235b
  if (errno != EMSGSIZE && errno != ENOBUFS)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  /* Message was too large, so dump to temporary file
Packit ae235b
   * and pass an FD to the journal
Packit ae235b
   */
Packit ae235b
  if ((buf_fd = mkostemp (path, O_CLOEXEC|O_RDWR)) < 0)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  if (unlink (path) < 0)
Packit ae235b
    {
Packit ae235b
      close (buf_fd);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (writev (buf_fd, iov, iovlen) < 0)
Packit ae235b
    {
Packit ae235b
      close (buf_fd);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  mh.msg_iov = NULL;
Packit ae235b
  mh.msg_iovlen = 0;
Packit ae235b
Packit ae235b
  memset (&control, 0, sizeof (control));
Packit ae235b
  mh.msg_control = &control;
Packit ae235b
  mh.msg_controllen = sizeof (control);
Packit ae235b
Packit ae235b
  cmsg = CMSG_FIRSTHDR (&mh);
Packit ae235b
  cmsg->cmsg_level = SOL_SOCKET;
Packit ae235b
  cmsg->cmsg_type = SCM_RIGHTS;
Packit ae235b
  cmsg->cmsg_len = CMSG_LEN (sizeof (int));
Packit ae235b
  memcpy (CMSG_DATA (cmsg), &buf_fd, sizeof (int));
Packit ae235b
Packit ae235b
  mh.msg_controllen = cmsg->cmsg_len;
Packit ae235b
Packit ae235b
retry2:
Packit ae235b
  if (sendmsg (journal_fd, &mh, MSG_NOSIGNAL) >= 0)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  if (errno == EINTR)
Packit ae235b
    goto retry2;
Packit ae235b
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
#endif /* ENABLE_JOURNAL_SENDV */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_writer_journald:
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @fields: (array length=n_fields): key–value pairs of structured data forming
Packit ae235b
 *    the log message
Packit ae235b
 * @n_fields: number of elements in the @fields array
Packit ae235b
 * @user_data: user data passed to g_log_set_writer_func()
Packit ae235b
 *
Packit ae235b
 * Format a structured log message and send it to the systemd journal as a set
Packit ae235b
 * of key–value pairs. All fields are sent to the journal, but if a field has
Packit ae235b
 * length zero (indicating program-specific data) then only its key will be
Packit ae235b
 * sent.
Packit ae235b
 *
Packit ae235b
 * This is suitable for use as a #GLogWriterFunc.
Packit ae235b
 *
Packit ae235b
 * If GLib has been compiled without systemd support, this function is still
Packit ae235b
 * defined, but will always return %G_LOG_WRITER_UNHANDLED.
Packit ae235b
 *
Packit ae235b
 * Returns: %G_LOG_WRITER_HANDLED on success, %G_LOG_WRITER_UNHANDLED otherwise
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
GLogWriterOutput
Packit ae235b
g_log_writer_journald (GLogLevelFlags   log_level,
Packit ae235b
                       const GLogField *fields,
Packit ae235b
                       gsize            n_fields,
Packit ae235b
                       gpointer         user_data)
Packit ae235b
{
Packit ae235b
#ifdef ENABLE_JOURNAL_SENDV
Packit ae235b
  const char equals = '=';
Packit ae235b
  const char newline = '\n';
Packit ae235b
  gsize i, k;
Packit ae235b
  struct iovec *iov, *v;
Packit ae235b
  char *buf;
Packit ae235b
  gint retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (fields != NULL, G_LOG_WRITER_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (n_fields > 0, G_LOG_WRITER_UNHANDLED);
Packit ae235b
Packit ae235b
  /* According to systemd.journal-fields(7), the journal allows fields in any
Packit ae235b
   * format (including arbitrary binary), but expects text fields to be UTF-8.
Packit ae235b
   * This is great, because we require input strings to be in UTF-8, so no
Packit ae235b
   * conversion is necessary and we don’t need to care about the current
Packit ae235b
   * locale’s character set.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  iov = g_alloca (sizeof (struct iovec) * 5 * n_fields);
Packit ae235b
  buf = g_alloca (32 * n_fields);
Packit ae235b
Packit ae235b
  k = 0;
Packit ae235b
  v = iov;
Packit ae235b
  for (i = 0; i < n_fields; i++)
Packit ae235b
    {
Packit ae235b
      guint64 length;
Packit ae235b
      gboolean binary;
Packit ae235b
Packit ae235b
      if (fields[i].length < 0)
Packit ae235b
        {
Packit ae235b
          length = strlen (fields[i].value);
Packit ae235b
          binary = strchr (fields[i].value, '\n') != NULL;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          length = fields[i].length;
Packit ae235b
          binary = TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (binary)
Packit ae235b
        {
Packit ae235b
          guint64 nstr;
Packit ae235b
Packit ae235b
          v[0].iov_base = (gpointer)fields[i].key;
Packit ae235b
          v[0].iov_len = strlen (fields[i].key);
Packit ae235b
Packit ae235b
          v[1].iov_base = (gpointer)&newline;
Packit ae235b
          v[1].iov_len = 1;
Packit ae235b
Packit ae235b
          nstr = GUINT64_TO_LE(length);
Packit ae235b
          memcpy (&buf[k], &nstr, sizeof (nstr));
Packit ae235b
Packit ae235b
          v[2].iov_base = &buf[k];
Packit ae235b
          v[2].iov_len = sizeof (nstr);
Packit ae235b
          v += 3;
Packit ae235b
          k += sizeof (nstr);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          v[0].iov_base = (gpointer)fields[i].key;
Packit ae235b
          v[0].iov_len = strlen (fields[i].key);
Packit ae235b
Packit ae235b
          v[1].iov_base = (gpointer)=
Packit ae235b
          v[1].iov_len = 1;
Packit ae235b
          v += 2;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      v[0].iov_base = (gpointer)fields[i].value;
Packit ae235b
      v[0].iov_len = length;
Packit ae235b
Packit ae235b
      v[1].iov_base = (gpointer)&newline;
Packit ae235b
      v[1].iov_len = 1;
Packit ae235b
      v += 2;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = journal_sendv (iov, v - iov);
Packit ae235b
Packit ae235b
  return retval == 0 ? G_LOG_WRITER_HANDLED : G_LOG_WRITER_UNHANDLED;
Packit ae235b
#else
Packit ae235b
  return G_LOG_WRITER_UNHANDLED;
Packit ae235b
#endif /* ENABLE_JOURNAL_SENDV */
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_writer_standard_streams:
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @fields: (array length=n_fields): key–value pairs of structured data forming
Packit ae235b
 *    the log message
Packit ae235b
 * @n_fields: number of elements in the @fields array
Packit ae235b
 * @user_data: user data passed to g_log_set_writer_func()
Packit ae235b
 *
Packit ae235b
 * Format a structured log message and print it to either `stdout` or `stderr`,
Packit ae235b
 * depending on its log level. %G_LOG_LEVEL_INFO and %G_LOG_LEVEL_DEBUG messages
Packit ae235b
 * are sent to `stdout`; all other log levels are sent to `stderr`. Only fields
Packit ae235b
 * which are understood by this function are included in the formatted string
Packit ae235b
 * which is printed.
Packit ae235b
 *
Packit ae235b
 * If the output stream supports ANSI color escape sequences, they will be used
Packit ae235b
 * in the output.
Packit ae235b
 *
Packit ae235b
 * A trailing new-line character is added to the log message when it is printed.
Packit ae235b
 *
Packit ae235b
 * This is suitable for use as a #GLogWriterFunc.
Packit ae235b
 *
Packit ae235b
 * Returns: %G_LOG_WRITER_HANDLED on success, %G_LOG_WRITER_UNHANDLED otherwise
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
GLogWriterOutput
Packit ae235b
g_log_writer_standard_streams (GLogLevelFlags   log_level,
Packit ae235b
                               const GLogField *fields,
Packit ae235b
                               gsize            n_fields,
Packit ae235b
                               gpointer         user_data)
Packit ae235b
{
Packit ae235b
  FILE *stream;
Packit ae235b
  gchar *out = NULL;  /* in the current locale’s character set */
Packit ae235b
Packit ae235b
  g_return_val_if_fail (fields != NULL, G_LOG_WRITER_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (n_fields > 0, G_LOG_WRITER_UNHANDLED);
Packit ae235b
Packit ae235b
  stream = log_level_to_file (log_level);
Packit ae235b
  if (!stream || fileno (stream) < 0)
Packit ae235b
    return G_LOG_WRITER_UNHANDLED;
Packit ae235b
Packit ae235b
  out = g_log_writer_format_fields (log_level, fields, n_fields,
Packit ae235b
                                    g_log_writer_supports_color (fileno (stream)));
Packit ae235b
  _g_fprintf (stream, "%s\n", out);
Packit ae235b
  fflush (stream);
Packit ae235b
  g_free (out);
Packit ae235b
Packit ae235b
  return G_LOG_WRITER_HANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* The old g_log() API is implemented in terms of the new structured log API.
Packit ae235b
 * However, some of the checks do not line up between the two APIs: the
Packit ae235b
 * structured API only handles fatalness of messages for log levels; the old API
Packit ae235b
 * handles it per-domain as well. Consequently, we need to disable fatalness
Packit ae235b
 * handling in the structured log API when called from the old g_log() API.
Packit ae235b
 *
Packit ae235b
 * We can guarantee that g_log_default_handler() will pass GLIB_OLD_LOG_API as
Packit ae235b
 * the first field to g_log_structured_array(), if that is the case.
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
log_is_old_api (const GLogField *fields,
Packit ae235b
                gsize            n_fields)
Packit ae235b
{
Packit ae235b
  return (n_fields >= 1 &&
Packit ae235b
          g_strcmp0 (fields[0].key, "GLIB_OLD_LOG_API") == 0 &&
Packit ae235b
          g_strcmp0 (fields[0].value, "1") == 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_writer_default:
Packit ae235b
 * @log_level: log level, either from #GLogLevelFlags, or a user-defined
Packit ae235b
 *    level
Packit ae235b
 * @fields: (array length=n_fields): key–value pairs of structured data forming
Packit ae235b
 *    the log message
Packit ae235b
 * @n_fields: number of elements in the @fields array
Packit ae235b
 * @user_data: user data passed to g_log_set_writer_func()
Packit ae235b
 *
Packit ae235b
 * Format a structured log message and output it to the default log destination
Packit ae235b
 * for the platform. On Linux, this is typically the systemd journal, falling
Packit ae235b
 * back to `stdout` or `stderr` if running from the terminal or if output is
Packit ae235b
 * being redirected to a file.
Packit ae235b
 *
Packit ae235b
 * Support for other platform-specific logging mechanisms may be added in
Packit ae235b
 * future. Distributors of GLib may modify this function to impose their own
Packit ae235b
 * (documented) platform-specific log writing policies.
Packit ae235b
 *
Packit ae235b
 * This is suitable for use as a #GLogWriterFunc, and is the default writer used
Packit ae235b
 * if no other is set using g_log_set_writer_func().
Packit ae235b
 *
Packit ae235b
 * As with g_log_default_handler(), this function drops debug and informational
Packit ae235b
 * messages unless their log domain (or `all`) is listed in the space-separated
Packit ae235b
 * `G_MESSAGES_DEBUG` environment variable.
Packit ae235b
 *
Packit ae235b
 * Returns: %G_LOG_WRITER_HANDLED on success, %G_LOG_WRITER_UNHANDLED otherwise
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
GLogWriterOutput
Packit ae235b
g_log_writer_default (GLogLevelFlags   log_level,
Packit ae235b
                      const GLogField *fields,
Packit ae235b
                      gsize            n_fields,
Packit ae235b
                      gpointer         user_data)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (fields != NULL, G_LOG_WRITER_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (n_fields > 0, G_LOG_WRITER_UNHANDLED);
Packit ae235b
Packit ae235b
  /* Disable debug message output unless specified in G_MESSAGES_DEBUG. */
Packit ae235b
  if (!(log_level & DEFAULT_LEVELS) && !(log_level >> G_LOG_LEVEL_USER_SHIFT))
Packit ae235b
    {
Packit ae235b
      const gchar *domains, *log_domain = NULL;
Packit ae235b
      gsize i;
Packit ae235b
Packit ae235b
      domains = g_getenv ("G_MESSAGES_DEBUG");
Packit ae235b
Packit ae235b
      if ((log_level & INFO_LEVELS) == 0 ||
Packit ae235b
          domains == NULL)
Packit ae235b
        return G_LOG_WRITER_HANDLED;
Packit ae235b
Packit ae235b
      for (i = 0; i < n_fields; i++)
Packit ae235b
        {
Packit ae235b
          if (g_strcmp0 (fields[i].key, "GLIB_DOMAIN") == 0)
Packit ae235b
            {
Packit ae235b
              log_domain = fields[i].value;
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (strcmp (domains, "all") != 0 &&
Packit ae235b
          (log_domain == NULL || !strstr (domains, log_domain)))
Packit ae235b
        return G_LOG_WRITER_HANDLED;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Mark messages as fatal if they have a level set in
Packit ae235b
   * g_log_set_always_fatal().
Packit ae235b
   */
Packit ae235b
  if ((log_level & g_log_always_fatal) && !log_is_old_api (fields, n_fields))
Packit ae235b
    log_level |= G_LOG_FLAG_FATAL;
Packit ae235b
Packit ae235b
  /* Try logging to the systemd journal as first choice. */
Packit ae235b
  if (g_log_writer_is_journald (fileno (stderr)) &&
Packit ae235b
      g_log_writer_journald (log_level, fields, n_fields, user_data) ==
Packit ae235b
      G_LOG_WRITER_HANDLED)
Packit ae235b
    goto handled;
Packit ae235b
Packit ae235b
  /* FIXME: Add support for the Windows log. */
Packit ae235b
Packit ae235b
  if (g_log_writer_standard_streams (log_level, fields, n_fields, user_data) ==
Packit ae235b
      G_LOG_WRITER_HANDLED)
Packit ae235b
    goto handled;
Packit ae235b
Packit ae235b
  return G_LOG_WRITER_UNHANDLED;
Packit ae235b
Packit ae235b
handled:
Packit ae235b
  /* Abort if the message was fatal. */
Packit ae235b
  if (log_level & G_LOG_FLAG_FATAL)
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      if (!g_test_initialized ())
Packit ae235b
        {
Packit ae235b
          gchar *locale_msg = NULL;
Packit ae235b
Packit ae235b
          locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
Packit ae235b
          MessageBox (NULL, locale_msg, NULL,
Packit ae235b
                      MB_ICONERROR | MB_SETFOREGROUND);
Packit ae235b
          g_free (locale_msg);
Packit ae235b
        }
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
      _g_log_abort (!(log_level & G_LOG_FLAG_RECURSION));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_LOG_WRITER_HANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GLogWriterOutput
Packit ae235b
_g_log_writer_fallback (GLogLevelFlags   log_level,
Packit ae235b
                        const GLogField *fields,
Packit ae235b
                        gsize            n_fields,
Packit ae235b
                        gpointer         user_data)
Packit ae235b
{
Packit ae235b
  FILE *stream;
Packit ae235b
  gsize i;
Packit ae235b
Packit ae235b
  /* we cannot call _any_ GLib functions in this fallback handler,
Packit ae235b
   * which is why we skip UTF-8 conversion, etc.
Packit ae235b
   * since we either recursed or ran out of memory, we're in a pretty
Packit ae235b
   * pathologic situation anyways, what we can do is giving the
Packit ae235b
   * the process ID unconditionally however.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  stream = log_level_to_file (log_level);
Packit ae235b
Packit ae235b
  for (i = 0; i < n_fields; i++)
Packit ae235b
    {
Packit ae235b
      const GLogField *field = &fields[i];
Packit ae235b
Packit ae235b
      /* Only print fields we definitely recognise, otherwise we could end up
Packit ae235b
       * printing a random non-string pointer provided by the user to be
Packit ae235b
       * interpreted by their writer function.
Packit ae235b
       */
Packit ae235b
      if (strcmp (field->key, "MESSAGE") != 0 &&
Packit ae235b
          strcmp (field->key, "MESSAGE_ID") != 0 &&
Packit ae235b
          strcmp (field->key, "PRIORITY") != 0 &&
Packit ae235b
          strcmp (field->key, "CODE_FILE") != 0 &&
Packit ae235b
          strcmp (field->key, "CODE_LINE") != 0 &&
Packit ae235b
          strcmp (field->key, "CODE_FUNC") != 0 &&
Packit ae235b
          strcmp (field->key, "ERRNO") != 0 &&
Packit ae235b
          strcmp (field->key, "SYSLOG_FACILITY") != 0 &&
Packit ae235b
          strcmp (field->key, "SYSLOG_IDENTIFIER") != 0 &&
Packit ae235b
          strcmp (field->key, "SYSLOG_PID") != 0 &&
Packit ae235b
          strcmp (field->key, "GLIB_DOMAIN") != 0)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      write_string (stream, field->key);
Packit ae235b
      write_string (stream, "=");
Packit ae235b
      write_string_sized (stream, field->value, field->length);
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  {
Packit ae235b
    gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
Packit ae235b
Packit ae235b
    format_unsigned (pid_string, getpid (), 10);
Packit ae235b
    write_string (stream, "_PID=");
Packit ae235b
    write_string (stream, pid_string);
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return G_LOG_WRITER_HANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_return_if_fail_warning: (skip)
Packit ae235b
 * @log_domain: (nullable):
Packit ae235b
 * @pretty_function:
Packit ae235b
 * @expression: (nullable):
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_return_if_fail_warning (const char *log_domain,
Packit ae235b
			  const char *pretty_function,
Packit ae235b
			  const char *expression)
Packit ae235b
{
Packit ae235b
  g_log (log_domain,
Packit ae235b
	 G_LOG_LEVEL_CRITICAL,
Packit ae235b
	 "%s: assertion '%s' failed",
Packit ae235b
	 pretty_function,
Packit ae235b
	 expression);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_warn_message: (skip)
Packit ae235b
 * @domain: (nullable):
Packit ae235b
 * @file:
Packit ae235b
 * @line:
Packit ae235b
 * @func:
Packit ae235b
 * @warnexpr: (nullable):
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_warn_message (const char     *domain,
Packit ae235b
                const char     *file,
Packit ae235b
                int             line,
Packit ae235b
                const char     *func,
Packit ae235b
                const char     *warnexpr)
Packit ae235b
{
Packit ae235b
  char *s, lstr[32];
Packit ae235b
  g_snprintf (lstr, 32, "%d", line);
Packit ae235b
  if (warnexpr)
Packit ae235b
    s = g_strconcat ("(", file, ":", lstr, "):",
Packit ae235b
                     func, func[0] ? ":" : "",
Packit ae235b
                     " runtime check failed: (", warnexpr, ")", NULL);
Packit ae235b
  else
Packit ae235b
    s = g_strconcat ("(", file, ":", lstr, "):",
Packit ae235b
                     func, func[0] ? ":" : "",
Packit ae235b
                     " ", "code should not be reached", NULL);
Packit ae235b
  g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
Packit ae235b
  g_free (s);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_assert_warning (const char *log_domain,
Packit ae235b
		  const char *file,
Packit ae235b
		  const int   line,
Packit ae235b
		  const char *pretty_function,
Packit ae235b
		  const char *expression)
Packit ae235b
{
Packit ae235b
  if (expression)
Packit ae235b
    g_log (log_domain,
Packit ae235b
	   G_LOG_LEVEL_ERROR,
Packit ae235b
	   "file %s: line %d (%s): assertion failed: (%s)",
Packit ae235b
	   file,
Packit ae235b
	   line,
Packit ae235b
	   pretty_function,
Packit ae235b
	   expression);
Packit ae235b
  else
Packit ae235b
    g_log (log_domain,
Packit ae235b
	   G_LOG_LEVEL_ERROR,
Packit ae235b
	   "file %s: line %d (%s): should not be reached",
Packit ae235b
	   file,
Packit ae235b
	   line,
Packit ae235b
	   pretty_function);
Packit ae235b
  _g_log_abort (FALSE);
Packit ae235b
  g_abort ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_expect_message:
Packit ae235b
 * @log_domain: (nullable): the log domain of the message
Packit ae235b
 * @log_level: the log level of the message
Packit ae235b
 * @pattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
Packit ae235b
 *
Packit ae235b
 * Indicates that a message with the given @log_domain and @log_level,
Packit ae235b
 * with text matching @pattern, is expected to be logged. When this
Packit ae235b
 * message is logged, it will not be printed, and the test case will
Packit ae235b
 * not abort.
Packit ae235b
 *
Packit ae235b
 * This API may only be used with the old logging API (g_log() without
Packit ae235b
 * %G_LOG_USE_STRUCTURED defined). It will not work with the structured logging
Packit ae235b
 * API. See [Testing for Messages][testing-for-messages].
Packit ae235b
 *
Packit ae235b
 * Use g_test_assert_expected_messages() to assert that all
Packit ae235b
 * previously-expected messages have been seen and suppressed.
Packit ae235b
 *
Packit ae235b
 * You can call this multiple times in a row, if multiple messages are
Packit ae235b
 * expected as a result of a single call. (The messages must appear in
Packit ae235b
 * the same order as the calls to g_test_expect_message().)
Packit ae235b
 *
Packit ae235b
 * For example:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   // g_main_context_push_thread_default() should fail if the
Packit ae235b
 *   // context is already owned by another thread.
Packit ae235b
 *   g_test_expect_message (G_LOG_DOMAIN,
Packit ae235b
 *                          G_LOG_LEVEL_CRITICAL,
Packit ae235b
 *                          "assertion*acquired_context*failed");
Packit ae235b
 *   g_main_context_push_thread_default (bad_context);
Packit ae235b
 *   g_test_assert_expected_messages ();
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Note that you cannot use this to test g_error() messages, since
Packit ae235b
 * g_error() intentionally never returns even if the program doesn't
Packit ae235b
 * abort; use g_test_trap_subprocess() in this case.
Packit ae235b
 *
Packit ae235b
 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
Packit ae235b
 * expected via g_test_expect_message() then they will be ignored.
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_expect_message (const gchar    *log_domain,
Packit ae235b
                       GLogLevelFlags  log_level,
Packit ae235b
                       const gchar    *pattern)
Packit ae235b
{
Packit ae235b
  GTestExpectedMessage *expected;
Packit ae235b
Packit ae235b
  g_return_if_fail (log_level != 0);
Packit ae235b
  g_return_if_fail (pattern != NULL);
Packit ae235b
  g_return_if_fail (~log_level & G_LOG_LEVEL_ERROR);
Packit ae235b
Packit ae235b
  expected = g_new (GTestExpectedMessage, 1);
Packit ae235b
  expected->log_domain = g_strdup (log_domain);
Packit ae235b
  expected->log_level = log_level;
Packit ae235b
  expected->pattern = g_strdup (pattern);
Packit ae235b
Packit ae235b
  expected_messages = g_slist_append (expected_messages, expected);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_test_assert_expected_messages_internal (const char     *domain,
Packit ae235b
                                          const char     *file,
Packit ae235b
                                          int             line,
Packit ae235b
                                          const char     *func)
Packit ae235b
{
Packit ae235b
  if (expected_messages)
Packit ae235b
    {
Packit ae235b
      GTestExpectedMessage *expected;
Packit ae235b
      gchar level_prefix[STRING_BUFFER_SIZE];
Packit ae235b
      gchar *message;
Packit ae235b
Packit ae235b
      expected = expected_messages->data;
Packit ae235b
Packit ae235b
      mklevel_prefix (level_prefix, expected->log_level, FALSE);
Packit ae235b
      message = g_strdup_printf ("Did not see expected message %s-%s: %s",
Packit ae235b
                                 expected->log_domain ? expected->log_domain : "**",
Packit ae235b
                                 level_prefix, expected->pattern);
Packit ae235b
      g_assertion_message (G_LOG_DOMAIN, file, line, func, message);
Packit ae235b
      g_free (message);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_assert_expected_messages:
Packit ae235b
 *
Packit ae235b
 * Asserts that all messages previously indicated via
Packit ae235b
 * g_test_expect_message() have been seen and suppressed.
Packit ae235b
 *
Packit ae235b
 * This API may only be used with the old logging API (g_log() without
Packit ae235b
 * %G_LOG_USE_STRUCTURED defined). It will not work with the structured logging
Packit ae235b
 * API. See [Testing for Messages][testing-for-messages].
Packit ae235b
 *
Packit ae235b
 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
Packit ae235b
 * expected via g_test_expect_message() then they will be ignored.
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_log_fallback_handler (const gchar   *log_domain,
Packit ae235b
			 GLogLevelFlags log_level,
Packit ae235b
			 const gchar   *message,
Packit ae235b
			 gpointer       unused_data)
Packit ae235b
{
Packit ae235b
  gchar level_prefix[STRING_BUFFER_SIZE];
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
Packit ae235b
#endif
Packit ae235b
  FILE *stream;
Packit ae235b
Packit ae235b
  /* we cannot call _any_ GLib functions in this fallback handler,
Packit ae235b
   * which is why we skip UTF-8 conversion, etc.
Packit ae235b
   * since we either recursed or ran out of memory, we're in a pretty
Packit ae235b
   * pathologic situation anyways, what we can do is giving the
Packit ae235b
   * the process ID unconditionally however.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  stream = mklevel_prefix (level_prefix, log_level, FALSE);
Packit ae235b
  if (!message)
Packit ae235b
    message = "(NULL) message";
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  format_unsigned (pid_string, getpid (), 10);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    write_string (stream, "\n");
Packit ae235b
  else
Packit ae235b
    write_string (stream, "\n** ");
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  write_string (stream, "(process:");
Packit ae235b
  write_string (stream, pid_string);
Packit ae235b
  write_string (stream, "): ");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    {
Packit ae235b
      write_string (stream, log_domain);
Packit ae235b
      write_string (stream, "-");
Packit ae235b
    }
Packit ae235b
  write_string (stream, level_prefix);
Packit ae235b
  write_string (stream, ": ");
Packit ae235b
  write_string (stream, message);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
escape_string (GString *string)
Packit ae235b
{
Packit ae235b
  const char *p = string->str;
Packit ae235b
  gunichar wc;
Packit ae235b
Packit ae235b
  while (p < string->str + string->len)
Packit ae235b
    {
Packit ae235b
      gboolean safe;
Packit ae235b
	    
Packit ae235b
      wc = g_utf8_get_char_validated (p, -1);
Packit ae235b
      if (wc == (gunichar)-1 || wc == (gunichar)-2)  
Packit ae235b
	{
Packit ae235b
	  gchar *tmp;
Packit ae235b
	  guint pos;
Packit ae235b
Packit ae235b
	  pos = p - string->str;
Packit ae235b
Packit ae235b
	  /* Emit invalid UTF-8 as hex escapes 
Packit ae235b
           */
Packit ae235b
	  tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
Packit ae235b
	  g_string_erase (string, pos, 1);
Packit ae235b
	  g_string_insert (string, pos, tmp);
Packit ae235b
Packit ae235b
	  p = string->str + (pos + 4); /* Skip over escape sequence */
Packit ae235b
Packit ae235b
	  g_free (tmp);
Packit ae235b
	  continue;
Packit ae235b
	}
Packit ae235b
      if (wc == '\r')
Packit ae235b
	{
Packit ae235b
	  safe = *(p + 1) == '\n';
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  safe = CHAR_IS_SAFE (wc);
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (!safe)
Packit ae235b
	{
Packit ae235b
	  gchar *tmp;
Packit ae235b
	  guint pos;
Packit ae235b
Packit ae235b
	  pos = p - string->str;
Packit ae235b
	  
Packit ae235b
	  /* Largest char we escape is 0x0a, so we don't have to worry
Packit ae235b
	   * about 8-digit \Uxxxxyyyy
Packit ae235b
	   */
Packit ae235b
	  tmp = g_strdup_printf ("\\u%04x", wc); 
Packit ae235b
	  g_string_erase (string, pos, g_utf8_next_char (p) - p);
Packit ae235b
	  g_string_insert (string, pos, tmp);
Packit ae235b
	  g_free (tmp);
Packit ae235b
Packit ae235b
	  p = string->str + (pos + 6); /* Skip over escape sequence */
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	p = g_utf8_next_char (p);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_log_default_handler:
Packit ae235b
 * @log_domain: (nullable): the log domain of the message, or %NULL for the
Packit ae235b
 * default "" application domain
Packit ae235b
 * @log_level: the level of the message
Packit ae235b
 * @message: (nullable): the message
Packit ae235b
 * @unused_data: (nullable): data passed from g_log() which is unused
Packit ae235b
 *
Packit ae235b
 * The default log handler set up by GLib; g_log_set_default_handler()
Packit ae235b
 * allows to install an alternate default log handler.
Packit ae235b
 * This is used if no log handler has been set for the particular log
Packit ae235b
 * domain and log level combination. It outputs the message to stderr
Packit ae235b
 * or stdout and if the log level is fatal it calls abort(). It automatically
Packit ae235b
 * prints a new-line character after the message, so one does not need to be
Packit ae235b
 * manually included in @message.
Packit ae235b
 *
Packit ae235b
 * The behavior of this log handler can be influenced by a number of
Packit ae235b
 * environment variables:
Packit ae235b
 *
Packit ae235b
 * - `G_MESSAGES_PREFIXED`: A :-separated list of log levels for which
Packit ae235b
 *   messages should be prefixed by the program name and PID of the
Packit ae235b
 *   aplication.
Packit ae235b
 *
Packit ae235b
 * - `G_MESSAGES_DEBUG`: A space-separated list of log domains for
Packit ae235b
 *   which debug and informational messages are printed. By default
Packit ae235b
 *   these messages are not printed.
Packit ae235b
 *
Packit ae235b
 * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
Packit ae235b
 * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
Packit ae235b
 * the rest.
Packit ae235b
 *
Packit ae235b
 * This has no effect if structured logging is enabled; see
Packit ae235b
 * [Using Structured Logging][using-structured-logging].
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_log_default_handler (const gchar   *log_domain,
Packit ae235b
		       GLogLevelFlags log_level,
Packit ae235b
		       const gchar   *message,
Packit ae235b
		       gpointer	      unused_data)
Packit ae235b
{
Packit ae235b
  GLogField fields[4];
Packit ae235b
  int n_fields = 0;
Packit ae235b
Packit ae235b
  /* we can be called externally with recursion for whatever reason */
Packit ae235b
  if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
    {
Packit ae235b
      _g_log_fallback_handler (log_domain, log_level, message, unused_data);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  fields[0].key = "GLIB_OLD_LOG_API";
Packit ae235b
  fields[0].value = "1";
Packit ae235b
  fields[0].length = -1;
Packit ae235b
  n_fields++;
Packit ae235b
Packit ae235b
  fields[1].key = "MESSAGE";
Packit ae235b
  fields[1].value = message;
Packit ae235b
  fields[1].length = -1;
Packit ae235b
  n_fields++;
Packit ae235b
Packit ae235b
  fields[2].key = "PRIORITY";
Packit ae235b
  fields[2].value = log_level_to_priority (log_level);
Packit ae235b
  fields[2].length = -1;
Packit ae235b
  n_fields++;
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    {
Packit ae235b
      fields[3].key = "GLIB_DOMAIN";
Packit ae235b
      fields[3].value = log_domain;
Packit ae235b
      fields[3].length = -1;
Packit ae235b
      n_fields++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Print out via the structured log API, but drop any fatal flags since we
Packit ae235b
   * have already handled them. The fatal handling in the structured logging
Packit ae235b
   * API is more coarse-grained than in the old g_log() API, so we don't want
Packit ae235b
   * to use it here.
Packit ae235b
   */
Packit ae235b
  g_log_structured_array (log_level & ~G_LOG_FLAG_FATAL, fields, n_fields);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_set_print_handler:
Packit ae235b
 * @func: the new print handler
Packit ae235b
 *
Packit ae235b
 * Sets the print handler.
Packit ae235b
 *
Packit ae235b
 * Any messages passed to g_print() will be output via
Packit ae235b
 * the new handler. The default handler simply outputs
Packit ae235b
 * the message to stdout. By providing your own handler
Packit ae235b
 * you can redirect the output, to a GTK+ widget or a
Packit ae235b
 * log file for example.
Packit ae235b
 *
Packit ae235b
 * Returns: the old print handler
Packit ae235b
 */
Packit ae235b
GPrintFunc
Packit ae235b
g_set_print_handler (GPrintFunc func)
Packit ae235b
{
Packit ae235b
  GPrintFunc old_print_func;
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  old_print_func = glib_print_func;
Packit ae235b
  glib_print_func = func;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  return old_print_func;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_print:
Packit ae235b
 * @format: the message format. See the printf() documentation
Packit ae235b
 * @...: the parameters to insert into the format string
Packit ae235b
 *
Packit ae235b
 * Outputs a formatted message via the print handler.
Packit ae235b
 * The default print handler simply outputs the message to stdout, without
Packit ae235b
 * appending a trailing new-line character. Typically, @format should end with
Packit ae235b
 * its own new-line character.
Packit ae235b
 *
Packit ae235b
 * g_print() should not be used from within libraries for debugging
Packit ae235b
 * messages, since it may be redirected by applications to special
Packit ae235b
 * purpose message windows or even files. Instead, libraries should
Packit ae235b
 * use g_log(), g_log_structured(), or the convenience macros g_message(),
Packit ae235b
 * g_warning() and g_error().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_print (const gchar *format,
Packit ae235b
         ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gchar *string;
Packit ae235b
  GPrintFunc local_glib_print_func;
Packit ae235b
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  string = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  local_glib_print_func = glib_print_func;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  if (local_glib_print_func)
Packit ae235b
    local_glib_print_func (string);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      const gchar *charset;
Packit ae235b
Packit ae235b
      if (g_get_charset (&charset))
Packit ae235b
        fputs (string, stdout); /* charset is UTF-8 already */
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          gchar *lstring = strdup_convert (string, charset);
Packit ae235b
Packit ae235b
          fputs (lstring, stdout);
Packit ae235b
          g_free (lstring);
Packit ae235b
        }
Packit ae235b
      fflush (stdout);
Packit ae235b
    }
Packit ae235b
  g_free (string);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_set_printerr_handler:
Packit ae235b
 * @func: the new error message handler
Packit ae235b
 *
Packit ae235b
 * Sets the handler for printing error messages.
Packit ae235b
 *
Packit ae235b
 * Any messages passed to g_printerr() will be output via
Packit ae235b
 * the new handler. The default handler simply outputs the
Packit ae235b
 * message to stderr. By providing your own handler you can
Packit ae235b
 * redirect the output, to a GTK+ widget or a log file for
Packit ae235b
 * example.
Packit ae235b
 *
Packit ae235b
 * Returns: the old error message handler
Packit ae235b
 */
Packit ae235b
GPrintFunc
Packit ae235b
g_set_printerr_handler (GPrintFunc func)
Packit ae235b
{
Packit ae235b
  GPrintFunc old_printerr_func;
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  old_printerr_func = glib_printerr_func;
Packit ae235b
  glib_printerr_func = func;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  return old_printerr_func;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_printerr:
Packit ae235b
 * @format: the message format. See the printf() documentation
Packit ae235b
 * @...: the parameters to insert into the format string
Packit ae235b
 *
Packit ae235b
 * Outputs a formatted message via the error message handler.
Packit ae235b
 * The default handler simply outputs the message to stderr, without appending
Packit ae235b
 * a trailing new-line character. Typically, @format should end with its own
Packit ae235b
 * new-line character.
Packit ae235b
 *
Packit ae235b
 * g_printerr() should not be used from within libraries.
Packit ae235b
 * Instead g_log() or g_log_structured() should be used, or the convenience
Packit ae235b
 * macros g_message(), g_warning() and g_error().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_printerr (const gchar *format,
Packit ae235b
            ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gchar *string;
Packit ae235b
  GPrintFunc local_glib_printerr_func;
Packit ae235b
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  string = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  g_mutex_lock (&g_messages_lock);
Packit ae235b
  local_glib_printerr_func = glib_printerr_func;
Packit ae235b
  g_mutex_unlock (&g_messages_lock);
Packit ae235b
Packit ae235b
  if (local_glib_printerr_func)
Packit ae235b
    local_glib_printerr_func (string);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      const gchar *charset;
Packit ae235b
Packit ae235b
      if (g_get_charset (&charset))
Packit ae235b
        fputs (string, stderr); /* charset is UTF-8 already */
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          gchar *lstring = strdup_convert (string, charset);
Packit ae235b
Packit ae235b
          fputs (lstring, stderr);
Packit ae235b
          g_free (lstring);
Packit ae235b
        }
Packit ae235b
      fflush (stderr);
Packit ae235b
    }
Packit ae235b
  g_free (string);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_printf_string_upper_bound:
Packit ae235b
 * @format: the format string. See the printf() documentation
Packit ae235b
 * @args: the parameters to be inserted into the format string
Packit ae235b
 *
Packit ae235b
 * Calculates the maximum space needed to store the output
Packit ae235b
 * of the sprintf() function.
Packit ae235b
 *
Packit ae235b
 * Returns: the maximum space needed to store the formatted string
Packit ae235b
 */
Packit ae235b
gsize
Packit ae235b
g_printf_string_upper_bound (const gchar *format,
Packit ae235b
                             va_list      args)
Packit ae235b
{
Packit ae235b
  gchar c;
Packit ae235b
  return _g_vsnprintf (&c, 1, format, args) + 1;
Packit ae235b
}