Commit Graph

225 Commits

Author SHA1 Message Date
Ole André Vadla Ravnås
0ab51f8d4e gwin32: Always use unicode APIs
Instead of the legacy ANSI ones. This also means GLib behaves correctly
when built with unicode preprocessor defines.
2021-01-05 21:42:59 +01:00
Simon McVittie
b17b537a7d gmessages: Expose our default filtering as API
This allows programs that want to change how log messages are printed,
such as gnome-terminal (gnome-terminal#42) and Flatpak, to override
the log-writer or the legacy log-handler without having to reimplement
the G_MESSAGES_DEBUG filtering logic.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-07 14:04:08 +01:00
Simon McVittie
10b0ece9d8 gmessages: Add API to move info and debug messages to stderr
GLib code normally prints info and debug messages to stdout,
but that interferes with programs that are documented to produce
machine-readable output such as JSON or XML on stdout. In particular,
if such a program uses a GLib-based library, setting G_MESSAGES_DEBUG
will typically result in that library's debug messages going to the
program's stdout and corrupting the machine-readable output.

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

Suggested by Allison Karlitskaya on glib#2087.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-07 14:03:50 +01:00
Guido Günther
b4023163ee gmessages: Document that using a custom log writer disables fatal handling
Document that structured logging interwines fatal handling and logging
so replacing the log handler can have undesired effects.
2020-10-06 11:50:41 +02:00
Philip Withnall
00bfb3ab44 tree: Fix various typos and outdated terminology
This was mostly machine generated with the following command:
```
codespell \
    --builtin clear,rare,usage \
    --skip './po/*' --skip './.git/*' --skip './NEWS*' \
    --write-changes .
```
using the latest git version of `codespell` as per [these
instructions](https://github.com/codespell-project/codespell#user-content-updating).

Then I manually checked each change using `git add -p`, made a few
manual fixups and dropped a load of incorrect changes.

There are still some outdated or loaded terms used in GLib, mostly to do
with git branch terminology. They will need to be changed later as part
of a wider migration of git terminology.

If I’ve missed anything, please file an issue!

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-06-12 15:01:08 +01:00
Philip Withnall
18a232be89 glib: Various minor scan-build fixes
These squash various warnings from `scan-build`. None of them are
legitimate bugs, but some of them do improve code readability a bit.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1767
2019-09-05 13:51:27 +01:00
Nirbheek Chauhan
db3e0c256d glib: Don't call MessageBox() when building for UWP
However, it's fine to call it when building for the debug target
(which uses the debug CRT and hence sets -D_DEBUG), so let's keep that
around.

The Windows App Certification Kit only runs on apps built in release
mode.
2019-08-27 00:17:29 +05:30
Patrick Storz
56149722ae Add g_get_console_charset
Queries the charset used by the associated console, which does not
necessarily match the charset of the current locale as returned by
g_get_charset.

Fixes https://gitlab.gnome.org/GNOME/glib/issues/1270
2019-05-27 17:51:40 +00:00
Philip Withnall
1a46bf88bb docs: Add missing documentation for warnings and assertions
These symbols were listed in glib-undocumented.txt. Thanks gtk-doc.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-11-23 12:19:19 +00:00
Will Thompson
f1175704b6
gmessages: don't memoize in g_log_writer_is_journald()
Previously, g_log_writer_is_journald() would cache the result for the
first (non-negative) FD it was called on, and return that result for
all future (non-negative) FDs. While unlikely, it's possible that
applications might call this function on something other than
fileno(stderr).

Move the memoization into g_log_writer_default(), which always passes
fileno(stderr).

Fixes #1589.
2018-11-12 11:20:49 +00:00
Philip Withnall
a0456f9c96 gmessages: Clarify documentation about aborting on fatal log levels
Clarify that we actually raise SIGTRAP rather than calling abort(). We
haven’t called abort() since about 2011, when commit
a04efe6afb changed the logic to use
SIGTRAP to make it possible to skip past fatal log messages in the
debugger if they weren’t relevant to the problem being debugged.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://gitlab.gnome.org/GNOME/glib/issues/1448
2018-08-22 10:37:15 +01:00
Christoph Reiter
a2aa91ae5d win32: Remove all remaining WinXP compat code
Remove all code which is no longer built with Windows 7+
2018-05-29 17:16:38 +02:00
Christoph Reiter
97c28f7fe1 ci: fix warnings and enable --werror for the mingw build
Fix various warnings regarding unused variables, duplicated
branches etc by adjusting the ifdeffery and some missing casts.

gnulib triggers -Wduplicated-branches in one of the copied files,
disable as that just makes updating the code harder.

The warning indicating missing features are made none fatal through
pragmas. They still show but don't abort the build.

https://bugzilla.gnome.org/show_bug.cgi?id=793729
2018-04-25 17:23:50 +02:00
Allison Lortie
ad3b2f2387 gmessages: clarify what log levels are for
For a long time we've had it as 'common knowledge' that criticals are
for programmer errors and warnings are for external errors, but we've
never documented that.  Do so.

(Modified by Philip Withnall <withnall@endlessm.com> to apply cleanly to
master; rearranged to fit in with current master documentation.)

https://bugzilla.gnome.org/show_bug.cgi?id=741049
2018-04-23 13:01:17 +01:00
Matthias Clasen
a4fc4c1e6e trivial: add some helpful comments
Not everybody knows console color codes by heart.
2018-02-06 11:05:57 -05:00
Philip Withnall
32cc60dbff gmessages: Fix -Wformat warnings for g_message() and friends
When compiling with G_LOG_USE_STRUCTURED, g_message(), g_debug(), etc.
use g_log_structured(). The message format string and its format
arguments are passed as the final set of arguments in a longer varargs
list, which includes the log domain and level (and other) fields.
Passing the message format in this way means it’s not possible for the
compiler to know to check its format placeholders when compiling with
-Wformat.

Fix support for this by adding a new semi-private helper function,
_g_log_structured_standard(), which only uses varargs for the message
format and its arguments, and uses fixed arguments for the other fields.
This is then converted to a set of GLogFields and passed to
g_log_structured() as normal.

Support for -Wformat when compiling *without* G_LOG_USE_STRUCTURED was
never broken.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793074
2018-02-02 10:10:43 +01:00
Benjamin Berg
5fba62adec gmessages: Flush output stream after logging messages
When debug output is enabled then certain messages will be logged to
stdout. stdout however is block buffered by default when it isn't going
to a TTY meaning that debug logging will not be flushed out properly
when it is being redirected. One example of this happening may be tests
that rely on parsing g_debug messages.

Adding an explicit fflush ensures all log messages will reach the output.

https://bugzilla.gnome.org/show_bug.cgi?id=792432
2018-01-15 19:46:22 +01:00
Philip Withnall
d8fe926ba4 Fix various strict aliasing problems with sockaddr
Fix various strict aliasing problems caused by casting between (struct
sockaddr *) and (struct sockaddr_storage *): the correct code here is to
keep the two in a union.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=791622
2018-01-08 11:50:26 +00:00
Руслан Ижбулатов
69ea026fbc Fix a 32-bit time_t cast
Divide first, *then* cast. Otherwise a very long "now", which is
64-bit, gets truncated into a 32-bit time_t, which can't hold the
value, and turns negative more often than not.

https://bugzilla.gnome.org/show_bug.cgi?id=791128
2017-12-02 13:15:30 +00:00
Philip Withnall
63e9d109fd gmessages: Give examples of G_DEBUG with gdb in the documentation
Some of the documentation linked to information about G_DEBUG already,
but most of it didn’t, and there were no examples. People need obvious
examples.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=790157
2017-11-10 10:27:26 +00:00
Philip Withnall
a6fc4daeb9 gmessages: Add timestamp to g_log_writer_format_fields()
Since journald adds a timestamp, it would be useful to add one to the
stdout/stderr output too — we do not want it to miss out on the
timestamping fun.

Make it blue, because we can.

https://bugzilla.gnome.org/show_bug.cgi?id=769846
2017-11-03 13:01:19 +00:00
Emmanuele Bassi
dc39232ded Do not build journal integration on older Linux platforms
We need to add more checks for journal_sendv(), as we depend on the
presence of mkostemp() and O_CLOEXEC, which may not be available on
older Linux platforms, like RHEL 5.

https://bugzilla.gnome.org/show_bug.cgi?id=788705
2017-10-09 12:45:59 +01:00
Philip Withnall
493723b4d9 gmessages: Update advice for G_LOG_DOMAIN
Suggest defining it for all code — for applications as well as for
libraries. This allows G_MESSAGES_DEBUG=my-app to be used to filter out
all messages from libraries which it uses, for example.

https://bugzilla.gnome.org/show_bug.cgi?id=777956
2017-10-06 11:26:47 +01:00
Philip Withnall
8330b09d78 Bug 679467 — Mention translation in g_warning() documentation
Include a line in the documentation for g_warning(), g_error(), g_critical()
and g_debug() mentioning that the messages passed to them typically should not
be translated.

Closes: https://bugzilla.gnome.org/show_bug.cgi?id=679467
2017-10-06 11:23:01 +01:00
Philip Withnall
1304197ed0 gmessages: Fix a typo in the documentation for g_log_set_handler_full()
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-09-22 13:34:19 +01:00
Philip Withnall
015ab311f8 gmessages: Add IDs for documentation headers
This fixes some broken cross-references between various places in the
logging documentation.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-09-13 17:07:29 +01:00
INSUN PYO
15faf0ef62 glog: fix crash on Linux without stderr stream
0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:58
1  0xb67c43f0 in __GI_abort () at abort.c:89
2  0xb69ee9d8 in _g_log_abort (breakpoint=2, breakpoint@entry=1) at gmessages.c:548
3  0xb69ef692 in g_logv (log_domain=0xb6a1dfc8 "GLib", log_level=-1254563840, log_level@entry=G_LOG_LEVEL_CRITICAL, format=format@entry=0xb6a26a48 "%s: assertion '%s' failed", args=..., args@entry=...) at gmessages.c:1357
4  0xb69ef728 in g_log (log_domain=<optimized out>, log_level=log_level@entry=G_LOG_LEVEL_CRITICAL, format=0xb6a26a48 "%s: assertion '%s' failed") at gmessages.c:1398
5  0xb69efa5a in g_return_if_fail_warning (log_domain=<optimized out>, pretty_function=<optimized out>, expression=<optimized out>) at gmessages.c:2687
6  0xb69efe7c in g_log_writer_is_journald (output_fd=-1) at gmessages.c:2122
7  0xb69f02a2 in g_log_writer_default (log_level=G_LOG_LEVEL_CRITICAL, fields=0xbedc9d00, n_fields=4, user_data=0x0) at gmessages.c:2584
8  0xb69ef21a in g_log_structured_array (log_level=G_LOG_LEVEL_CRITICAL, fields=0xbedc9d00, n_fields=4) at gmessages.c:1933
9  0xb69ef47e in g_log_default_handler (log_domain=0xb6a1dfc8 "GLib", log_level=G_LOG_LEVEL_CRITICAL, message=<optimized out>, unused_data=<optimized out>) at gmessages.c:3036
10 0xb69ef5fc in g_logv (log_domain=0xb6a1dfc8 "GLib", log_level=log_level@entry=G_LOG_LEVEL_CRITICAL, format=format@entry=0xb6a26a48 "%s: assertion '%s' failed", args=..., args@entry=...) at gmessages.c:1336
11 0xb69ef728 in g_log (log_domain=<optimized out>, log_level=log_level@entry=G_LOG_LEVEL_CRITICAL, format=0xb6a26a48 "%s: assertion '%s' failed") at gmessages.c:1398
12 0xb69efa5a in g_return_if_fail_warning (log_domain=<optimized out>, pretty_function=<optimized out>, expression=<optimized out>) at gmessages.c:2687

If stderr is not associated with an output stream, the fileno(stderr) returned is -1.
So, g_return_if_fail_warning is recursively called and the abort occurs on the second call.

Modified by Philip Withnall to include mention this in the
documentation.

https://bugzilla.gnome.org/show_bug.cgi?id=786452
2017-08-18 09:26:12 +01:00
Debarshi Ray
8962736ba9 docs: Encourage applications to define G_LOG_DOMAIN
https://bugzilla.gnome.org/show_bug.cgi?id=785130
2017-07-20 19:45:53 +02:00
Philip Withnall
ac0b0c84f7 gmessages: Expand documentation on GLogWriterFunc chaining
As prompted by Marc-Andre Lureau.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=661926
2017-06-14 09:57:01 +01:00
Sébastien Wilmet
f9faac7661 glib/: LGPLv2+ -> LGPLv2.1+
All glib/*.{c,h} files have been processed, as well as gtester-report.

12 of those files are not licensed under LGPL:

	gbsearcharray.h
	gconstructor.h
	glibintl.h
	gmirroringtable.h
	gscripttable.h
	gtranslit-data.h
	gunibreak.h
	gunichartables.h
	gunicomp.h
	gunidecomp.h
	valgrind.h
	win_iconv.c

Some of them are generated files, some are licensed under a BSD-style
license and win_iconv.c is in the public domain.

Sub-directories inside glib/:

	deprecated/: processed in a previous commit
	glib-mirroring-tab/: already LGPLv2.1+
	gnulib/: not modified, the code is copied from gnulib
	libcharset/: a copy
	pcre/: a copy
	tests/: processed in a previous commit

https://bugzilla.gnome.org/show_bug.cgi?id=776504
2017-05-24 11:58:19 +02:00
Philip Withnall
e8487812b9 gmessages: Fix documentation formatting 2017-04-18 11:58:28 +01:00
Philip Withnall
21b6b1fba7 gmessages: Don’t check G_MESSAGES_DEBUG in old logging API
Now that the old logging API forwards through to the new structured
logging API, we don’t need to check the level or domain of a message
against INFO_LEVELS or G_MESSAGES_DEBUG — just pass it straight through
to the new structured logging API writer function.

https://bugzilla.gnome.org/show_bug.cgi?id=775879
2017-03-24 15:08:16 +00:00
Chun-wei Fan
9aaf7588fc glib/gmessages.c: Fix build when targeting Windows Vista+
In my previous attempt to improve colored console output on Windows, we
called GetFileInformationByHandleEx() directly if we target Vista or
later, but the check macro for doing so via LoadLibrary() had a typo.  Fix
this by correcting the check macro.

Pointed out by Ignacio Casal Quinteiro.
2017-03-02 08:22:49 +08:00
Chun-wei Fan
7518067cec gmessages.c: Windows: Improve g_log_writer_supports_colors()
... when tty terminal emulators (such as mintty) are used.  This support
is however for Vista and later, so use of such terminals should have
support for ANSI color codes in their outputs, even on Windows 7/8/8.1/

https://bugzilla.gnome.org/show_bug.cgi?id=775468
2016-12-08 22:15:47 +08:00
Sebastian Dröge
620b3c1e56 gmessages: Fix compilation on Android
There is no systemd on Android.

https://bugzilla.gnome.org/show_bug.cgi?id=775621
2016-12-05 12:12:50 +02:00
Chun-wei Fan
7a61a94b95 gmessages.c/Windows: Improve g_log_writer_supports_color()
Windows 10 begins to support color output on the console using ANSI
codes, but a few extra steps are required for this support, so we cannot
just use isatty() and expect colors to work in the outputs.
Instead, we use still use isatty() from the Windows CRT, and then enable
ENABLE_VIRTUAL_TERMINAL_PROCESSING if it returns true.

Also make the invalid parameter handler shared between gmessages.c and
gspawn-win32-helper.c, since it is basically intended to be a no-op stub.

https://bugzilla.gnome.org/show_bug.cgi?id=775468
2016-12-05 16:47:09 +08:00
Christian Hergert
18a33f72db introspection: use (nullable) or (optional) instead of (allow-none)
If we have an input parameter (or return value) we need to use (nullable).
However, if it is an (inout) or (out) parameter, (optional) is sufficient.

It looks like (nullable) could be used for everything according to the
Annotation documentation, but (optional) is more specific.
2016-11-22 14:14:37 -08:00
Marc-André Lureau
615b847d98 glog: fix crash on windows with --subsystem,windows app
A windows application compiled with -Wl,--subsystem,windows has no
console attached. When started from the start menu for ex, it will crash
when a function attempt to use g_log:

 #10 0x00a21b26 in g_logv (log_domain=0xa842e1 <__func__.7668+329> "GLib", log_level=G_LOG_LEVEL_CRITICAL, format=0xa845c6 <__func__.7668+1070> "%s: assertion '%s' failed", args=0x28f2bc "PG"")
 #11 0x00a21bb1 in g_log (log_domain=0xa842e1 <__func__.7668+329> "GLib", log_level=G_LOG_LEVEL_CRITICAL, format=0xa845c6 <__func__.7668+1070> "%s: assertion '%s' failed") at gmessages.c:1337
 #12 0x00a22bac in g_return_if_fail_warning (log_domain=0xa842e1 <__func__.7668+329> "GLib", pretty_function=0xa84750 <__func__.65002> "g_log_writer_supports_color",
     expression=0xa844de <__func__.7668+838> "output_fd >= 0") at gmessages.c:2453
 #13 0x00a2239e in g_log_writer_supports_color (output_fd=-2) at gmessages.c:1826
 #14 0x00a226ac in g_log_writer_standard_streams (log_level=G_LOG_LEVEL_WARNING, fields=0x28f3c8, n_fields=4, user_data=0x0) at gmessages.c:2254
 #15 0x00a2290e in g_log_writer_default (log_level=G_LOG_LEVEL_WARNING, fields=0x28f3c8, n_fields=4, user_data=0x0) at gmessages.c:2357

According to https://msdn.microsoft.com/en-us/library/zs6wbdhx.aspx:

If stdout or stderr is not associated with an output stream (for
example, in a Windows application without a console window), the file
descriptor returned is -2. In previous versions, the file descriptor
returned was -1. This change allows applications to distinguish this
condition from an error.

Check if the stream exists and has an associated descriptor and return
G_LOG_WRITER_UNHANDLED if it's not the case.

Fixes:
https://bugzilla.gnome.org/show_bug.cgi?id=772511

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2016-10-10 09:39:07 -04:00
Jonh Wendell
063e279a56 Fixed annotation for g_log_variant() 2016-09-17 16:25:31 -03:00
Jonh Wendell
e7bdd5d189 Add g_log_variant(): structured log that accepts a GVariant
This makes the structured logging available to other
languages via introspection.

https://bugzilla.gnome.org/show_bug.cgi?id=770971
2016-09-09 09:07:50 -03:00
Philip Withnall
a079de930e gmessages: Add doc cross-refs about enabling structured logging
Ensure that all the old log handler documentation includes
cross-references to a new section about how to enable structured logging
(tl;dr: #define G_LOG_USE_STRUCTURED).

https://bugzilla.gnome.org/show_bug.cgi?id=769785
2016-08-12 14:14:54 +02:00
Philip Withnall
f7825f98e7 gmessages: Drop one of the documentation sections
It was short, unhelpful, and easy to confuse with the longer and more
informative documentation sections.

https://bugzilla.gnome.org/show_bug.cgi?id=769785
2016-08-12 11:54:45 +02:00
Philip Withnall
371078b6a3 gmessages: Expand documentation on log domains and G_MESSAGES_DEBUG
Give some example log domains, and recommend that G_MESSAGES_DEBUG is
used universally as the way to enable debug output (rather than having a
separate environment variable per library).

https://bugzilla.gnome.org/show_bug.cgi?id=682794
2016-08-07 08:10:31 +01:00
Philip Withnall
0e132b8ac5 gmessages: Document g_test_expect_message() doesn’t like structured logs
It’s effectively deprecated if G_LOG_USE_STRUCTURED is defined, or if
the structured logging API is used directly. See the documentation for
rationale.

https://bugzilla.gnome.org/show_bug.cgi?id=769486
2016-08-07 08:07:42 +01:00
Philip Withnall
4e66036b82 Revert "Make g_test_expect_message work for structured logs"
This reverts commit df02e8f47c.

We are going to make g_test_expect_message() only work for the old
logging API.

https://bugzilla.gnome.org/show_bug.cgi?id=769486
2016-08-07 08:07:42 +01:00
Philip Withnall
5c3205f2fb gmessages: Don’t require is_journald() call before writer_journald()
g_log_writer_is_journald() works out whether the process’ stderr or
stdout are redirected to journald. While the default writer function
uses this in conjunction with g_log_writer_journald(), that does not
always have to be the case — other writer functions might want to always
write to the journal, and never write to stderr (for example).

Consequently, automatically open the journal socket in
writer_journald(), rather than is_journald().

https://bugzilla.gnome.org/show_bug.cgi?id=769507
2016-08-04 14:33:34 +01:00
Matthias Clasen
df02e8f47c Make g_test_expect_message work for structured logs
This is used in GTK+ tests, and GTK+ has switched to
G_LOG_USE_STRUCTURED, so we need this to keep working.
2016-08-02 23:16:04 -04:00
Chun-wei Fan
6d6b97aa59 gmessages.c: Fix build on non-Linux
journald is a part of systemd which is Linux-only at this time, so only
compile the journald items on Linux only, and just return FALSE for
g_log_writer_is_journald() and G_LOG_WRITER_UNHANDLED for
g_log_writer_journald() on non-Linux.

https://bugzilla.gnome.org/show_bug.cgi?id=744456
2016-07-27 10:45:00 +08:00
Matthias Clasen
496c52ec79 Avoid htole64 altogether
GLib ships its own api for this, lets use it.
2016-07-25 12:31:52 -04:00
Matthias Clasen
ad285d9bd2 Handle EINTR when sending logs to the journal
Ray pointed out that we might well get interrupted here,
and should not loose logs due to that.
2016-07-25 10:25:40 -04:00