Skip to content

Commit 71c18cb

Browse files
committed
libworkbench: implement GLogWriterFunc and library initialization
Add `Workbench.init()` to initialize resources and other setup tasks. Currently, this only implements a `GLogWriterFunc` for silencing specific messages that Workbench users shouldn't see. This method has some limitations running on other threads in GJS, and should be more performant in C anyways.
1 parent 5a2840e commit 71c18cb

File tree

7 files changed

+146
-120
lines changed

7 files changed

+146
-120
lines changed

src/init.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import Adw from "gi://Adw";
1212
import Xdp from "gi://Xdp";
1313
import Source from "gi://GtkSource";
1414
import WebKit from "gi://WebKit";
15+
import Workbench from "gi://Workbench";
16+
17+
18+
Workbench.init();
1519

1620
Gio._promisify(Adw.MessageDialog.prototype, "choose", "choose_finish");
1721
Gio._promisify(Xdp.Portal.prototype, "trash_file", "trash_file_finish");

src/libworkbench/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ libworkbench_headers = files([
6161
'workbench.h',
6262
'workbench-completion-provider.h',
6363
'workbench-completion-request.h',
64+
'workbench-utils.h',
6465
])
6566

6667
libworkbench_sources = files([
6768
'workbench-completion-provider.c',
6869
'workbench-completion-request.c',
70+
'workbench-utils.c',
6971
])
7072

7173
install_headers(libworkbench_headers,

src/libworkbench/workbench-utils.c

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
// SPDX-FileCopyrightText: Workbench Contributors
3+
// SPDX-FileContributor: Andy Holmes <[email protected]>
4+
5+
#include <glib.h>
6+
7+
#include "workbench-utils.h"
8+
9+
10+
/*
11+
* Silenced log messages
12+
*/
13+
struct
14+
{
15+
const char *log_domain;
16+
GLogLevelFlags log_level;
17+
const char *message;
18+
} ignored_messages[] = {
19+
/* GDK4 */
20+
{
21+
"Gdk",
22+
G_LOG_LEVEL_CRITICAL,
23+
"gdk_scroll_event_get_direction: assertion 'GDK_IS_EVENT_TYPE (event, GDK_SCROLL)' failed",
24+
},
25+
{
26+
"Gdk",
27+
G_LOG_LEVEL_CRITICAL,
28+
"gdk_scroll_event_get_direction: assertion 'GDK_IS_EVENT (event)' failed",
29+
},
30+
31+
/* GTK4 */
32+
{
33+
"Gtk",
34+
G_LOG_LEVEL_CRITICAL,
35+
"Unable to connect to the accessibility bus at 'unix:path=/run/flatpak/at-spi-bus': Could not connect: No such file or directory",
36+
},
37+
38+
/* Adwaita */
39+
{
40+
"Adwaita",
41+
G_LOG_LEVEL_WARNING,
42+
"Using GtkSettings:gtk-application-prefer-dark-theme with libadwaita is unsupported. Please use AdwStyleManager:color-scheme instead.",
43+
},
44+
45+
/* GVFS */
46+
{
47+
"GVFS",
48+
G_LOG_LEVEL_WARNING,
49+
"The peer-to-peer connection failed: Error when getting information for file “/run/user/1000/gvfsd”: No such file or directory. Falling back to the session bus. Your application is probably missing --filesystem=xdg-run/gvfsd privileges.",
50+
},
51+
};
52+
53+
static inline gboolean
54+
workbench_log_ignore (const char *log_domain,
55+
GLogLevelFlags log_level,
56+
const char *message)
57+
{
58+
if G_UNLIKELY (log_domain == NULL || message == NULL)
59+
return FALSE;
60+
61+
for (unsigned int i = 0; i < G_N_ELEMENTS (ignored_messages); i++)
62+
{
63+
if (ignored_messages[i].log_level == log_level &&
64+
g_str_equal (ignored_messages[i].log_domain, log_domain) &&
65+
g_str_equal (ignored_messages[i].message, message))
66+
return TRUE;
67+
}
68+
69+
return FALSE;
70+
}
71+
72+
static GLogWriterOutput
73+
workbench_log_writer (GLogLevelFlags log_level,
74+
const GLogField *fields,
75+
gsize n_fields,
76+
gpointer user_data)
77+
{
78+
const char *log_domain = NULL;
79+
const char *message = NULL;
80+
81+
/* Respect default handling of log levels and domains */
82+
if (g_log_writer_default_would_drop (log_level, log_domain))
83+
return G_LOG_WRITER_HANDLED;
84+
85+
/* Silence specific messages */
86+
for (gsize i = 0; i < n_fields; i++)
87+
{
88+
GLogField field = fields[i];
89+
90+
if (g_str_equal (field.key, "GLIB_DOMAIN"))
91+
log_domain = field.value;
92+
else if (g_str_equal (field.key, "MESSAGE"))
93+
message = field.value;
94+
95+
if (log_domain != NULL && message != NULL)
96+
break;
97+
}
98+
99+
if (workbench_log_ignore (log_domain, log_level, message))
100+
return G_LOG_WRITER_HANDLED;
101+
102+
return g_log_writer_standard_streams (log_level, fields, n_fields, user_data);
103+
}
104+
105+
106+
/**
107+
* workbench_init:
108+
*
109+
* Initialize the internal library for Workbench.
110+
*/
111+
void
112+
workbench_init (void)
113+
{
114+
gsize initialized = 0;
115+
116+
if (g_once_init_enter (&initialized))
117+
{
118+
g_log_set_writer_func (workbench_log_writer, NULL, NULL);
119+
g_once_init_leave (&initialized, TRUE);
120+
}
121+
}

src/libworkbench/workbench-utils.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
// SPDX-FileCopyrightText: Workbench Contributors
3+
// SPDX-FileContributor: Andy Holmes <[email protected]>
4+
5+
#pragma once
6+
7+
#if !defined (WORKBENCH_INSIDE) && !defined (WORKBENCH_COMPILATION)
8+
# error "Only <workbench.h> can be included directly."
9+
#endif
10+
11+
#include <glib.h>
12+
13+
G_BEGIN_DECLS
14+
15+
WORKBENCH_EXPORT
16+
void workbench_init (void);
17+
18+
G_END_DECLS

src/libworkbench/workbench.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "workbench-completion-provider.h"
1212
#include "workbench-completion-request.h"
13+
#include "workbench-utils.h"
1314

1415
#undef WORKBENCH_INSIDE
1516

src/log_handler.js

-119
This file was deleted.

src/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import "./init.js";
2-
import "./log_handler.js";
32
import application from "./application.js";
43

54
pkg.initGettext();

0 commit comments

Comments
 (0)