Skip to content

Commit

Permalink
Now logger interface can take multiple messages.
Browse files Browse the repository at this point in the history
Fixed an linting issue in the watcher.
Moved watcher initialization to init method instead of constructor.
  • Loading branch information
rzvxa committed Oct 4, 2023
1 parent 80b51c2 commit 47e4296
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
13 changes: 7 additions & 6 deletions src/main/services/loggingService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,33 @@ class LoggingService implements Logger, IService {
}
}

trace(message: string, tags: string[] = []) {
trace(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.trace, tags, message);
}

debug(message: string, tags: string[] = []) {
debug(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.debug, tags, message);
}

info(message: string, tags: string[] = []) {
info(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.info, tags, message);
}

warning(message: string, tags: string[] = []) {
warning(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.warning, tags, message);
}

error(message: string, tags: string[] = []) {
error(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.error, tags, message);
}

fatal(message: string, tags: string[] = []) {
fatal(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.fatal, tags, message);
}

log(logLevel: LogLevel, tags: string[], ...args: unknown[]) {
const message = args.reduce((a, c) => `${a} ${c}`, '') as string;
console.log(message);
const logMessage = {
message,
level: logLevel,
Expand Down
23 changes: 15 additions & 8 deletions src/main/services/projectWatchService/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import path from 'path';
import { watch, FSWatcher } from 'chokidar';
import { sanitizePath } from 'shared/utils';
import { ChannelsRenderer, ProjectTree, ProjectTreeNode } from 'shared/types';
import {
LogLevel,
ChannelsRenderer,
ProjectTree,
ProjectTreeNode,
} from 'shared/types';

import project from 'main/project';

import IService from '../IService';

Expand All @@ -13,15 +20,15 @@ export type ProjectWatchServiceMessageBroker = (
const FlushInterval = 300;

class ProjectWatchService implements IService {
#watcher: FSWatcher;

#messageBroker: ProjectWatchServiceMessageBroker;

#projectPath: string;

#projectTree: ProjectTree;

#workerIdentifier: ReturnType<typeof setInterval>;
#watcher: FSWatcher | null = null;

#workerIdentifier: ReturnType<typeof setInterval> | null = null;

#isDirty: boolean = false;

Expand All @@ -38,6 +45,9 @@ class ProjectWatchService implements IService {
isDir: true,
children: undefined,
};
}

async init(): Promise<boolean> {
this.#watcher = watch(this.#projectPath, {
ignored: /^\./,
persistent: true,
Expand All @@ -51,13 +61,10 @@ class ProjectWatchService implements IService {
})
.on('unlink', (_path) => this.#onUnlink(_path))
.on('error', (error) => {
console.error('Error happened', error);
project.logger.log(LogLevel.error, ['ProjectWatchService'], error);
});

this.#workerIdentifier = setInterval(() => this.#worker(), FlushInterval);
}

async init(): Promise<boolean> {
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/utils/fileLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ export default class FileLogger implements Logger {
this.fileHandle = await open(this.filePath, 'a');
}

trace(message: string, tags: string[]) {
trace(message: unknown | unknown[], tags: string[]) {
this.log(LogLevel.trace, tags, message);
}

debug(message: string, tags: string[]) {
debug(message: unknown | unknown[], tags: string[]) {
this.log(LogLevel.debug, tags, message);
}

info(message: string, tags: string[]) {
info(message: unknown | unknown[], tags: string[]) {
this.log(LogLevel.info, tags, message);
}

warning(message: string, tags: string[]) {
warning(message: unknown | unknown[], tags: string[]) {
this.log(LogLevel.warning, tags, message);
}

error(message: string, tags: string[]) {
error(message: unknown | unknown[], tags: string[]) {
this.log(LogLevel.error, tags, message);
}

fatal(message: string, tags: string[]) {
fatal(message: unknown | unknown[], tags: string[]) {
this.log(LogLevel.fatal, tags, message);
}

Expand Down
12 changes: 6 additions & 6 deletions src/renderer/logger.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { ChannelsMain, Logger, LogLevel } from 'shared/types';

class RendererLogger implements Logger {
trace(message: string, tags: string[] = []) {
trace(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.trace, tags, message);
}

debug(message: string, tags: string[] = []) {
debug(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.debug, tags, message);
}

info(message: string, tags: string[] = []) {
info(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.info, tags, message);
}

warning(message: string, tags: string[] = []) {
warning(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.warning, tags, message);
}

error(message: string, tags: string[] = []) {
error(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.error, tags, message);
}

fatal(message: string, tags: string[] = []) {
fatal(message: unknown | unknown[], tags: string[] = []) {
this.log(LogLevel.fatal, tags, message);
}

Expand Down
12 changes: 6 additions & 6 deletions src/shared/types/logger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import LogLevel from './logLevel';

export default interface Logger {
trace: (message: string, tags: string[]) => void;
debug: (message: string, tags: string[]) => void;
info: (message: string, tags: string[]) => void;
warning: (message: string, tags: string[]) => void;
error: (message: string, tags: string[]) => void;
fatal: (message: string, tags: string[]) => void;
trace: (message: unknown | unknown[], tags: string[]) => void;
debug: (message: unknown | unknown[], tags: string[]) => void;
info: (message: unknown | unknown[], tags: string[]) => void;
warning: (message: unknown | unknown[], tags: string[]) => void;
error: (message: unknown | unknown[], tags: string[]) => void;
fatal: (message: unknown | unknown[], tags: string[]) => void;
log: (logLevel: LogLevel, tags: string[], ...args: unknown[]) => void;
}

0 comments on commit 47e4296

Please sign in to comment.