Skip to content

Commit

Permalink
Selectable log level
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucki authored and ifl0w committed Feb 11, 2024
1 parent 43930e3 commit 59c159f
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 78 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ rules:
no-return-assign: error
no-return-await: error
no-self-compare: error
no-shadow: error
no-shadow: off
"@typescript-eslint/no-shadow": error
no-shadow-restricted-names: error
no-spaced-func: error
no-tabs: error
Expand All @@ -175,7 +176,8 @@ rules:
no-undef-init: error
no-unneeded-ternary: error
no-unused-expressions: error
no-unused-vars:
no-unused-vars: off
"@typescript-eslint/no-unused-vars":
- error
# Vars use a suffix _ instead of a prefix because of file-scope private vars
- varsIgnorePattern: (^unused|_$)
Expand Down
8 changes: 3 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let RandomWallpaperMenu: typeof RandomWallpaperMenuNamespace | null = null;
/**
*
*/
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function init() {
return new Extension();
}
Expand All @@ -36,12 +36,10 @@ class Extension {
this._logger.info('Enable extension.');
this._panelMenu.init();
}).catch(error => {
if (this._logger) {
if (this._logger)
this._logger.error(error);
else
logError(error);
} else {
logError(error);
}
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/historyMenuElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const HistoryElement = GObject.registerClass({
const authorItem = new PopupMenu.PopupMenuItem(`Image By: ${this.historyEntry.source.author}`);
authorItem.connect('activate', () => {
if (this.historyEntry.source.authorUrl)
Utils.execCheck(['xdg-open', this.historyEntry.source.authorUrl]).catch(logError);
Utils.execCheck(['xdg-open', this.historyEntry.source.authorUrl]).catch(this._logger.error);
});

this.menu.addMenuItem(authorItem);
Expand All @@ -100,7 +100,7 @@ const HistoryElement = GObject.registerClass({
const sourceItem = new PopupMenu.PopupMenuItem(`Image From: ${this.historyEntry.source.source}`);
sourceItem.connect('activate', () => {
if (this.historyEntry.source.sourceUrl)
Utils.execCheck(['xdg-open', this.historyEntry.source.sourceUrl]).catch(logError);
Utils.execCheck(['xdg-open', this.historyEntry.source.sourceUrl]).catch(this._logger.error);
});

this.menu.addMenuItem(sourceItem);
Expand All @@ -109,7 +109,7 @@ const HistoryElement = GObject.registerClass({
const imageUrlItem = new PopupMenu.PopupMenuItem('Open Image In Browser');
imageUrlItem.connect('activate', () => {
if (this.historyEntry.source.imageLinkUrl)
Utils.execCheck(['xdg-open', this.historyEntry.source.imageLinkUrl]).catch(logError);
Utils.execCheck(['xdg-open', this.historyEntry.source.imageLinkUrl]).catch(this._logger.error);
});

this.menu.addMenuItem(imageUrlItem);
Expand All @@ -132,7 +132,7 @@ const HistoryElement = GObject.registerClass({

const copyToFavorites = new PopupMenu.PopupMenuItem('Save For Later');
copyToFavorites.connect('activate', () => {
this._saveImage().catch(logError);
this._saveImage().catch(this._logger.error);
});
this.menu.addMenuItem(copyToFavorites);

Expand Down
59 changes: 38 additions & 21 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
// TODO: use an enum once moved to TS
const LOG_LEVEL = {
SILENT: 0,
ERROR: 1,
WARN: 2,
INFO: 3,
DEBUG: 4,
};

// TODO: add UI option or at least ENV variable (this is a quick workaround to conform to extension review requirements)
const CURRENT_LOG_LEVEL = LOG_LEVEL.WARN;
// https://gitlab.gnome.org/GNOME/gjs/-/blob/master/doc/Logging.md

import {Settings} from './settings.js';

const enum LogLevel {
SILENT,
ERROR,
WARNING,
INFO,
DEBUG,
}
type LogLevelStrings = keyof typeof LogLevel;

class Logger {
private _prefix: string;
private _callingClass: string;
private _settings = new Settings();

constructor(prefix: string, callingClass: string) {
this._prefix = prefix;
this._callingClass = callingClass;
}

private _log(level: string, message: unknown) {
log(`${this._prefix} [${level}] >> ${this._callingClass} :: ${message}`);
private _log(level: LogLevelStrings, message: unknown) {
let errorMessage = String(message);

if (message instanceof Error)
errorMessage = message.message;

// This logs messages with GLib.LogLevelFlags.LEVEL_MESSAGE
log(`${this._prefix} [${level}] >> ${this._callingClass} :: ${errorMessage}`);

// Log stack trace if available
if (message instanceof Error && message.stack)
// This logs messages with GLib.LogLevelFlags.LEVEL_WARNING
logError(message);
}

private _selectedLogLevel() {
return this._settings.getEnum('log-level');
}

debug(message: string) {
if (CURRENT_LOG_LEVEL < LOG_LEVEL.DEBUG)
debug(message: unknown) {
if (this._selectedLogLevel() < LogLevel.DEBUG)
return;

this._log('DEBUG', message);
}

info(message: string) {
if (CURRENT_LOG_LEVEL < LOG_LEVEL.INFO)
info(message: unknown) {
if (this._selectedLogLevel() < LogLevel.INFO)
return;

this._log('INFO', message);
}

warn(message: string) {
if (CURRENT_LOG_LEVEL < LOG_LEVEL.WARN)
warn(message: unknown) {
if (this._selectedLogLevel() < LogLevel.WARNING)
return;

this._log('WARNING', message);
}

error(message: string) {
if (CURRENT_LOG_LEVEL < LOG_LEVEL.ERROR)
error(message: unknown) {
if (this._selectedLogLevel() < LogLevel.ERROR)
return;

this._log('ERROR', message);
Expand Down
34 changes: 7 additions & 27 deletions src/prefs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Gio from 'gi://Gio';
import * as Gtk from 'gi://Gtk';

import * as Adw from '@gi/gtk4/adw/adw';
import * as ExtensionUtils from '@gi/misc/extensionUtils';

import * as Adw from '@gi/gtk4/adw/adw';
import type * as SettingsNamespace from './settings.js';
import type * as UtilsNamespace from './utils.js';
import type * as LoggerNamespace from './logger.js';
Expand All @@ -19,7 +19,7 @@ const Self = ExtensionUtils.getCurrentExtension();
/**
*
*/
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function init() {
// Convenience.initTranslations();
}
Expand All @@ -35,17 +35,16 @@ function init() {
*
* @param {Adw.PreferencesWindow} window Window the extension should fill
*/
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function fillPreferencesWindow(window: Adw.PreferencesWindow) {
window.set_default_size(-1, 720);
window.set_default_size(600, 720);
// temporary fill window to prevent error message until modules are loaded
const tmpPage = new Adw.PreferencesPage();
window.add(tmpPage);

new RandomWallpaperSettings(window, tmpPage);
}


// 40 < Gnome < 42
// function buildPrefsWidget() {
// let window = new Adw.PreferencesWindow();
Expand Down Expand Up @@ -82,7 +81,8 @@ class RandomWallpaperSettings {
this._builder.add_from_file(`${Self.path}/ui/pageGeneral.ui`);
this._builder.add_from_file(`${Self.path}/ui/pageSources.ui`);

this._fillTypeComboRow();
Utils.fillComboRowFromEnum(this._builder.get_object('combo_background_type'), this._settings, 'change-type');
Utils.fillComboRowFromEnum(this._builder.get_object('log_level'), this._settings, 'log-level');

this._settings.bind('minutes',
this._builder.get_object('duration_minutes'),
Expand Down Expand Up @@ -145,7 +145,7 @@ class RandomWallpaperSettings {
if (new module.HydraPaper().isAvailable())
// eslint-disable-next-line no-extra-parens
(this._builder.get_object('multiple_displays_row') as Adw.ActionRow).set_sensitive(true);
}).catch(logError);
}).catch(this._logger.error);
}).catch(error => {
logError(error);
throw error;
Expand Down Expand Up @@ -175,26 +175,6 @@ class RandomWallpaperSettings {
Settings = moduleSettings;
}

private _fillTypeComboRow() {
const comboRow: Adw.ComboRow = this._builder.get_object('combo_background_type');

// Fill combo from settings enum
const availableTypes = this._settings.getSchema().get_key('change-type').get_range(); // GLib.Variant (sv)
// (sv) = Tuple(%G_VARIANT_TYPE_STRING, %G_VARIANT_TYPE_VARIANT)
// s should be 'enum'
// v should be an array enumerating the possible values. Each item in the array is a possible valid value and no other values are valid.
// v is 'as'
const availableTypesNames = availableTypes.get_child_value(1).get_variant().get_strv();

const stringList = Gtk.StringList.new(availableTypesNames);
comboRow.model = stringList;
comboRow.selected = this._settings.getEnum('change-type');

comboRow.connect('notify::selected', _comboRow => {
this._settings.setEnum('change-type', _comboRow.selected);
});
}

private _bindButtons() {
const newWallpaperButton: Adw.ActionRow = this._builder.get_object('request_new_wallpaper');
const newWallpaperButtonLabel = newWallpaperButton.get_child() as Gtk.Label | null;
Expand Down
11 changes: 5 additions & 6 deletions src/randomWallpaperMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class RandomWallpaperMenu {
this._wallpaperController.prohibitNewWallpaper = false;
}).catch(error => {
this._wallpaperController.prohibitNewWallpaper = false;
logError(error);
this._logger.error(error);
});
});

Expand All @@ -111,7 +111,7 @@ class RandomWallpaperMenu {
// Open Wallpaper Folder
openFolder.connect('activate', () => {
const uri = GLib.filename_to_uri(this._wallpaperController.wallpaperLocation, '');
Utils.execCheck(['xdg-open', uri]).catch(logError);
Utils.execCheck(['xdg-open', uri]).catch(this._logger.error);
});

openSettings.connect('activate', () => {
Expand Down Expand Up @@ -197,10 +197,9 @@ class RandomWallpaperMenu {

/**
* @this {RandomWallpaperMenu} RandomWallpaperMenu
* @param {CustomElements.HistoryElement} actor The activating panel item
* @param {CustomElements.HistoryElement} unusedActor The activating panel item
*/
// eslint-disable-next-line no-unused-vars
function onLeave(this: RandomWallpaperMenu, actor: typeof CustomElements.HistoryElement) {
function onLeave(this: RandomWallpaperMenu, unusedActor: typeof CustomElements.HistoryElement) {
if (!this._wallpaperController.prohibitNewWallpaper)
this._wallpaperController.resetWallpaper();
}
Expand All @@ -227,7 +226,7 @@ class RandomWallpaperMenu {
this._wallpaperController.prohibitNewWallpaper = false;
}).catch(error => {
this._wallpaperController.prohibitNewWallpaper = false;
logError(error);
this._logger.error(error);
});
}

Expand Down
1 change: 0 additions & 1 deletion src/schemas/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
<!-- <value value='3' nick='Background and Lock Screen independently' /> -->
</enum>

<enum id='org.gnome.shell.extensions.space.iflow.randomwallpaper.log-levels'>
<value value='0' nick='Silent' />
<value value='1' nick='Error' />
<value value='2' nick='Warning' />
<value value='3' nick='Info' />
<value value='4' nick='Debug' />
</enum>

<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper'>

Expand Down Expand Up @@ -62,6 +70,12 @@
<description>Allows to choose what backgrounds will be changed.</description>
</key>

<key name='log-level' enum='org.gnome.shell.extensions.space.iflow.randomwallpaper.log-levels'>
<default>"Warning"</default>
<summary>Tier of logs</summary>
<description>Choose what minimal tier of logs should appear in the journal.</description>
</key>

<key type='b' name='disable-hover-preview'>
<default>false</default>
<summary>Disable hover preview</summary>
Expand Down
2 changes: 0 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class Settings {
if (!schemaId)
schemaId = Self.metadata['settings-schema'];


// Expect USER extensions to have a schemas/ subfolder, otherwise assume a
// SYSTEM extension that has been installed in the same prefix as the shell
const schemaDir = Self.dir.get_child('schemas');
Expand All @@ -146,7 +145,6 @@ class Settings {
if (!schemaObj)
throw new Error(`Schema ${schemaId} could not be found for extension ${Self.metadata.uuid}. Please check your installation`);


return schemaObj;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/ui/.gitignore

This file was deleted.

5 changes: 5 additions & 0 deletions src/ui/pageGeneral.blp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Adw.PreferencesPage page_general {
valign: center;
}
}

Adw.ComboRow log_level {
title: _("Log level");
subtitle: _("Set the tier of warnings appearing in the journal");
}
}

Adw.PreferencesGroup {
Expand Down
Loading

0 comments on commit 59c159f

Please sign in to comment.