Skip to content

Commit

Permalink
libworkbench: implement GLogWriterFunc and library initialization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andyholmes committed Aug 20, 2023
1 parent 5a2840e commit eae9ff0
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 120 deletions.
4 changes: 4 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import Adw from "gi://Adw";
import Xdp from "gi://Xdp";
import Source from "gi://GtkSource";
import WebKit from "gi://WebKit";
import Workbench from "gi://Workbench";


Workbench.init();

Gio._promisify(Adw.MessageDialog.prototype, "choose", "choose_finish");
Gio._promisify(Xdp.Portal.prototype, "trash_file", "trash_file_finish");
Expand Down
2 changes: 2 additions & 0 deletions src/libworkbench/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ libworkbench_headers = files([
'workbench.h',
'workbench-completion-provider.h',
'workbench-completion-request.h',
'workbench-global.h',
])

libworkbench_sources = files([
'workbench-completion-provider.c',
'workbench-completion-request.c',
'workbench-global.c',
])

install_headers(libworkbench_headers,
Expand Down
121 changes: 121 additions & 0 deletions src/libworkbench/workbench-global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Workbench Contributors
// SPDX-FileContributor: Andy Holmes <[email protected]>

#include <glib.h>

#include "workbench-global.h"


/*
* Silenced log messages
*/
struct
{
const char *log_domain;
GLogLevelFlags log_level;
const char *message;
} ignored_messages[] = {
/* GDK4 */
{
"Gdk",
G_LOG_LEVEL_CRITICAL,
"gdk_scroll_event_get_direction: assertion 'GDK_IS_EVENT_TYPE (event, GDK_SCROLL)' failed",
},
{
"Gdk",
G_LOG_LEVEL_CRITICAL,
"gdk_scroll_event_get_direction: assertion 'GDK_IS_EVENT (event)' failed",
},

/* GTK4 */
{
"Gtk",
G_LOG_LEVEL_CRITICAL,
"Unable to connect to the accessibility bus at 'unix:path=/run/flatpak/at-spi-bus': Could not connect: No such file or directory",
},

/* Adwaita */
{
"Adwaita",
G_LOG_LEVEL_WARNING,
"Using GtkSettings:gtk-application-prefer-dark-theme with libadwaita is unsupported. Please use AdwStyleManager:color-scheme instead.",
},

/* GVFS */
{
"GVFS",
G_LOG_LEVEL_WARNING,
"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.",
},
};

static inline gboolean
workbench_log_ignore (const char *log_domain,
GLogLevelFlags log_level,
const char *message)
{
if G_UNLIKELY (log_domain == NULL || message == NULL)
return FALSE;

for (unsigned int i = 0; i < G_N_ELEMENTS (ignored_messages); i++)
{
if (ignored_messages[i].log_level == log_level &&
g_str_equal (ignored_messages[i].log_domain, log_domain) &&
g_str_equal (ignored_messages[i].message, message))
return TRUE;
}

return FALSE;
}

static GLogWriterOutput
workbench_log_writer (GLogLevelFlags log_level,
const GLogField *fields,
gsize n_fields,
gpointer user_data)
{
const char *log_domain = NULL;
const char *message = NULL;

/* Respect default handling of log levels and domains */
if (g_log_writer_default_would_drop (log_level, log_domain))
return G_LOG_WRITER_HANDLED;

/* Silence specific messages */
for (gsize i = 0; i < n_fields; i++)
{
GLogField field = fields[i];

if (g_str_equal (field.key, "GLIB_DOMAIN"))
log_domain = field.value;
else if (g_str_equal (field.key, "MESSAGE"))
message = field.value;

if (log_domain != NULL && message != NULL)
break;
}

if (workbench_log_ignore (log_domain, log_level, message))
return G_LOG_WRITER_HANDLED;

return g_log_writer_standard_streams (log_level, fields, n_fields, user_data);
}


/**
* workbench_init:
*
* Initialize the internal library for Workbench.
*/
void
workbench_init (void)
{
gsize initialized = 0;

if (g_once_init_enter (&initialized))
{
g_log_set_writer_func (workbench_log_writer, NULL, NULL);
g_once_init_leave (&initialized, TRUE);
}
}
18 changes: 18 additions & 0 deletions src/libworkbench/workbench-global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Workbench Contributors
// SPDX-FileContributor: Andy Holmes <[email protected]>

#pragma once

#if !defined (WORKBENCH_INSIDE) && !defined (WORKBENCH_COMPILATION)
# error "Only <workbench.h> can be included directly."
#endif

#include <glib.h>

G_BEGIN_DECLS

WORKBENCH_EXPORT
void workbench_init (void);

G_END_DECLS
1 change: 1 addition & 0 deletions src/libworkbench/workbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "workbench-completion-provider.h"
#include "workbench-completion-request.h"
#include "workbench-global.h"

#undef WORKBENCH_INSIDE

119 changes: 0 additions & 119 deletions src/log_handler.js

This file was deleted.

1 change: 0 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "./init.js";
import "./log_handler.js";
import application from "./application.js";

pkg.initGettext();
Expand Down

0 comments on commit eae9ff0

Please sign in to comment.