Skip to content

Commit

Permalink
Address SonarCloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mackdk committed Jun 18, 2023
1 parent c4c956c commit cc79cc3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
65 changes: 32 additions & 33 deletions src/main/typescript/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { File } from '@gi-types/gio2';
import { get_user_data_dir } from '@gi-types/glib2';
import { getCurrentExtension } from '@gnome-shell/misc/extensionUtils';

import { FormatParameter, formatString, readonlyMap } from './utilities';
import { formatString, readonlyMap, removeAfter as stripAfter } from './utilities';

export enum LogLevel {
TRACE,
Expand All @@ -12,11 +12,11 @@ export enum LogLevel {
ERROR,
}

export type LoggableParameter = FormatParameter;

export class Logger {
private static readonly ROOT_SCOPE: string = 'root';

private static readonly SCOPE_DEFINITION_REGEX: RegExp = /^\s*(\w+(::\w+)*)\s=\s(TRACE|DEBUG|INFO|WARN|ERROR)\s*$/;

private static rootLogLevel: LogLevel = LogLevel.INFO;
private static scopeLevelsMap: Map<string, LogLevel> = new Map<string, LogLevel>();

Expand All @@ -28,33 +28,32 @@ export class Logger {
public static initialize(): void {
Logger.resetConfiguration();

const configuration = File.new_for_path(`${get_user_data_dir()}/gnome-github-manager/logging.properties`);
if (!configuration.query_exists(null)) {
// No configuration file, nothing to do
return;
}

try {
const configuration = File.new_for_path(`${get_user_data_dir()}/gnome-github-manager/logging.properties`);
if (configuration.query_exists(null)) {
const [success, bytes] = configuration.load_contents(null);
if (!success) {
throw new Error('Loading data was not successfull');
}

const lines = new TextDecoder('utf-8').decode(bytes.buffer).split('\n');
lines.forEach((line) => {
// Remove any comments
if (line.includes('#')) {
line = line.substring(0, line.indexOf('#'));
}
const [success, bytes] = configuration.load_contents(null);
if (!success) {
throw new Error('Loading data was not successfull');
}

if (line.match(/^\s*(\w+(::\w+)*)\s=\s(TRACE|DEBUG|INFO|WARN|ERROR)\s*$/) !== null) {
const [scope, level] = line.split('=').map((s) => s.trim());
if (scope === Logger.ROOT_SCOPE) {
this.rootLogLevel = LogLevel[level as keyof typeof LogLevel];
} else {
Logger.scopeLevelsMap.set(scope, LogLevel[level as keyof typeof LogLevel]);
}
const configurationFile = new TextDecoder('utf-8').decode(bytes.buffer);
configurationFile
.split('\n') // Divide into single lines
.map((line) => stripAfter(line, '#')) // Remove any comments
.filter((line) => Logger.SCOPE_DEFINITION_REGEX.exec(line) !== null) // Keep only lines that matches
.forEach((line) => {
// Line is now in the form scope = LEVEL. Split it to extract the components
const [scope, level] = line.split('=').map((s) => s.trim());
if (scope === Logger.ROOT_SCOPE) {
this.rootLogLevel = LogLevel[level as keyof typeof LogLevel];
} else {
Logger.scopeLevelsMap.set(scope, LogLevel[level as keyof typeof LogLevel]);
}
});
} else {
log(`[${Logger.EXT_PREFIX}] Logger initialization ERROR Unable to load configuration file`);
}
} catch (error) {
logError(error, `[${Logger.EXT_PREFIX}] Logger initialization ERROR Unable to load configuration file`);
}
Expand Down Expand Up @@ -107,52 +106,52 @@ export class Logger {
return this.isEnabled(LogLevel.TRACE);
}

public trace(format: string, ...args: LoggableParameter[]): void {
public trace(format: string, ...args: unknown[]): void {
this.addLog(LogLevel.TRACE, format, ...args);
}

public isDebugEnabled(): boolean {
return this.isEnabled(LogLevel.DEBUG);
}

public debug(format: string, ...args: LoggableParameter[]): void {
public debug(format: string, ...args: unknown[]): void {
this.addLog(LogLevel.DEBUG, format, ...args);
}

public isInfoEnabled(): boolean {
return this.isEnabled(LogLevel.INFO);
}

public info(format: string, ...args: LoggableParameter[]): void {
public info(format: string, ...args: unknown[]): void {
this.addLog(LogLevel.INFO, format, ...args);
}

public isWarnEnabled(): boolean {
return this.isEnabled(LogLevel.WARN);
}

public warn(format: string, ...args: LoggableParameter[]): void {
public warn(format: string, ...args: unknown[]): void {
this.addLog(LogLevel.WARN, format, ...args);
}

public isErrorEnabled(): boolean {
return this.isEnabled(LogLevel.ERROR);
}

public error(format: string, ...args: LoggableParameter[]): void {
public error(format: string, ...args: unknown[]): void {
this.addLog(LogLevel.ERROR, format, ...args);
}

private isEnabled(levelToCheck: LogLevel): boolean {
return levelToCheck >= this.logLevel;
}

private addLog(level: LogLevel, format: string, ...args: LoggableParameter[]): void {
private addLog(level: LogLevel, format: string, ...args: unknown[]): void {
if (!this.isEnabled(level)) {
return;
}

let err: unknown | undefined = undefined;
let err: unknown = undefined;
if (this.hasErrorParameter(format, args.length)) {
err = args.pop();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/typescript/utils/locale.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TranslationDomain, domain } from '@gettext';
import { initTranslations } from '@gnome-shell/misc/extensionUtils';

import { FormatParameter, formatString } from './utilities';
import { formatString } from './utilities';

let translationDomain: TranslationDomain | undefined;

Expand All @@ -15,7 +15,7 @@ export function disposeTranslationDomain(): void {
translationDomain = undefined;
}

export function _(msgId: string, ...args: FormatParameter[]): string {
export function _(msgId: string, ...args: unknown[]): string {
if (translationDomain === undefined) {
throw new Error('Translation domain is not initialized. Call initializeTranslations() first.');
}
Expand All @@ -24,7 +24,7 @@ export function _(msgId: string, ...args: FormatParameter[]): string {
return formatString(translated, ...args);
}

export function ngettext(singularMsgId: string, pluralMsgId: string, n: number, ...args: FormatParameter[]): string {
export function ngettext(singularMsgId: string, pluralMsgId: string, n: number, ...args: unknown[]): string {
if (translationDomain === undefined) {
throw new Error('Translation domain is not initialized. Call initializeTranslations() first.');
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/typescript/utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ export function lazy<T>(_target: T, _name: string): void {
// This is only used before compilation by a trasformer. Nothing to do.
}

export type FormatParameter = number | string | bigint | symbol | object | unknown;

export function formatString(template: string, ...args: FormatParameter[]): string {
export function formatString(template: string, ...args: unknown[]): string {
if (args.length === 0) {
return template;
}

return template.replace(/{(\d+)}/g, (match: string, number: number) => args[number]?.toString() ?? match);
}

export function removeAfter(text: string, gate: string): string {
if (!text.includes(gate)) {
return text;
}

return text.substring(0, text.indexOf(gate));
}

class ReadonlyMapWrapper<K, V> implements ReadonlyMap<K, V> {
private readonly wrappedMap: Map<K, V>;

Expand Down

0 comments on commit cc79cc3

Please sign in to comment.