Skip to content

Commit fd8aa94

Browse files
committed
Fix verbose/debug logging, add G_LOG_DOMAIN for easier tracking
in journalctl. Verbose mode was broken for gtk-based daemons, as post-gtk_init is too late to set G_MESSAGES_DEBUG.
1 parent 8c5b745 commit fd8aa94

18 files changed

Lines changed: 53 additions & 5 deletions

File tree

plugins/a11y-settings/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ executable(
1717
include_directories: [include_dirs, common_inc],
1818
dependencies: a11y_settings_deps,
1919
c_args: [
20+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
2021
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
2122
],
2223
install: true,

plugins/automount/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ executable(
1818
include_directories: [include_dirs, common_inc],
1919
dependencies: automount_deps,
2020
c_args: [
21+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
2122
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
2223
],
2324
install_rpath: join_paths(prefix, apilibdir),

plugins/background/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ executable(
1919
include_directories: [include_dirs, common_inc],
2020
dependencies: background_deps,
2121
c_args: [
22+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
2223
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
2324
],
2425
install: true,

plugins/clipboard/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ executable(
1919
include_directories: [include_dirs, common_inc],
2020
dependencies: clipboard_deps,
2121
c_args: [
22+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
2223
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
2324
],
2425
install: true,

plugins/color/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ executable(
3737
dependencies: color_deps,
3838
c_args: [
3939
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
40+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
4041
'-DBINDIR="@0@"'.format(bindir),
4142
],
4243
install: true,

plugins/common/daemon-skeleton-gtk.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ handle_signal (gpointer user_data)
174174
return G_SOURCE_REMOVE;
175175
}
176176

177+
static void
178+
message_handler (const gchar *log_domain,
179+
GLogLevelFlags log_level,
180+
const gchar *message,
181+
gpointer user_data)
182+
{
183+
/* Make this look like normal console output */
184+
if (log_level & G_LOG_LEVEL_DEBUG) {
185+
g_autoptr(GDateTime) dt = g_date_time_new_now_local ();
186+
g_autofree gchar *iso8601 = g_date_time_format (dt, "%F:%T");
187+
printf ("csd-%s:%s: %s\n", PLUGIN_NAME, iso8601, message);
188+
}
189+
else {
190+
printf ("%s: %s\n", g_get_prgname (), message);
191+
}
192+
}
193+
177194
int
178195
main (int argc, char **argv)
179196
{
@@ -221,7 +238,7 @@ main (int argc, char **argv)
221238
g_unix_signal_add (SIGTERM, (GSourceFunc) handle_signal, NULL);
222239

223240
if (verbose)
224-
g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
241+
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, message_handler, NULL);
225242

226243
if (timeout > 0) {
227244
guint id;

plugins/common/daemon-skeleton.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ handle_signal (gpointer user_data)
171171
return G_SOURCE_REMOVE;
172172
}
173173

174+
static void
175+
message_handler (const gchar *log_domain,
176+
GLogLevelFlags log_level,
177+
const gchar *message,
178+
gpointer user_data)
179+
{
180+
/* Make this look like normal console output */
181+
if (log_level & G_LOG_LEVEL_DEBUG) {
182+
g_autoptr(GDateTime) dt = g_date_time_new_now_local ();
183+
g_autofree gchar *iso8601 = g_date_time_format (dt, "%F:%T");
184+
printf ("csd-%s:%s: %s\n", PLUGIN_NAME, iso8601, message);
185+
}
186+
else {
187+
printf ("%s: %s\n", g_get_prgname (), message);
188+
}
189+
}
190+
174191
int
175192
main (int argc, char **argv)
176193
{
@@ -208,10 +225,8 @@ main (int argc, char **argv)
208225

209226
g_unix_signal_add (SIGTERM, (GSourceFunc) handle_signal, loop);
210227

211-
if (verbose) {
212-
g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
213-
setlinebuf (stdout);
214-
}
228+
if (verbose)
229+
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, message_handler, NULL);
215230

216231
if (timeout > 0) {
217232
guint id;

plugins/datetime/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ if polkit.found()
4747
include_directories: [include_dirs, common_inc],
4848
dependencies: datetime_deps,
4949
c_args: [
50+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
5051
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
5152
],
5253
install: true,

plugins/housekeeping/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ executable(
2525
include_directories: [include_dirs, common_inc],
2626
dependencies: housekeeping_deps,
2727
c_args: [
28+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
2829
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
2930
],
3031
install: true,

plugins/keyboard/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ executable(
2323
include_directories: [include_dirs, common_inc, include_enums],
2424
dependencies: keyboard_deps,
2525
c_args: [
26+
'-DG_LOG_DOMAIN="csd-@0@"'.format(plugin_name),
2627
'-DPLUGIN_NAME="@0@"'.format(plugin_name),
2728
'-DDATADIR="@0@"'.format(datadir),
2829
],

0 commit comments

Comments
 (0)