From 23611ca2f07fadff193d1421ee994859e42ee2db Mon Sep 17 00:00:00 2001 From: Jonas Schatz Date: Mon, 14 Dec 2020 23:31:07 +0100 Subject: [PATCH] Implemented Logger --- src/extension.ts | 10 ++-- src/features/create-note.ts | 1 + src/features/search-note.ts | 2 +- src/services/bootstrap.ts | 4 ++ src/services/filesystemSyncher.ts | 8 +++ src/services/index.ts | 10 ++++ src/services/logger.ts | 95 +++++++++++++++++++++++++++++++ src/vscode/NoteActions.ts | 1 + 8 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/services/index.ts create mode 100644 src/services/logger.ts diff --git a/src/extension.ts b/src/extension.ts index ee185ff..d42a7f1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,14 +1,14 @@ import * as vscode from 'vscode'; + +import { NoteGraph } from "./core"; import { features } from "./features"; -import { Config, createConfigFromVsCode, NoteGraph } from "./core"; -import { CodekastenParser } from "./vscode"; +import { bootstrap, CodekastenLogger, Logger } from './services'; import { checkCodekastenSetup } from './vscode/Verification'; -import { bootstrap } from './services/bootstrap'; -export async function activate(context: vscode.ExtensionContext) { - const config: Config = createConfigFromVsCode(); +export async function activate(context: vscode.ExtensionContext) { + Logger.setDefaultLogger(new CodekastenLogger); const codekastenGraph: NoteGraph = await bootstrap(); diff --git a/src/features/create-note.ts b/src/features/create-note.ts index 4e27419..837037e 100644 --- a/src/features/create-note.ts +++ b/src/features/create-note.ts @@ -4,6 +4,7 @@ import { NoteGraph } from "../core"; import { letUserChooseTemplate, letUserChooseText } from "../vscode/Inputs"; import { createNote, openNoteInWorkspace } from "../vscode/NoteActions"; import { MarkdownLink } from "../core/Link"; +import { Logger } from "../services"; const feature: Feature = { activate: (context: vscode.ExtensionContext, graph: NoteGraph) => { diff --git a/src/features/search-note.ts b/src/features/search-note.ts index e5e0e2d..a81ef3f 100644 --- a/src/features/search-note.ts +++ b/src/features/search-note.ts @@ -4,7 +4,7 @@ import { NoteGraph } from "../core"; import { Note } from '../core/Note'; import { letUserSearchNoteByTitle, letUserChooseNoteAction } from '../vscode/Inputs'; import * as noteActions from "../vscode/NoteActions"; - +import { Logger } from "../services"; const feature: Feature = { activate: (context: vscode.ExtensionContext, graph: NoteGraph) => { diff --git a/src/services/bootstrap.ts b/src/services/bootstrap.ts index 64a9957..98ca871 100644 --- a/src/services/bootstrap.ts +++ b/src/services/bootstrap.ts @@ -1,6 +1,8 @@ import * as md5 from 'md5'; import * as vscode from 'vscode'; + import { NoteGraph } from "../core"; +import { Logger } from '../services'; import { CodekastenParser } from "../vscode"; import { FilesystemSyncher } from './filesystemSyncher'; @@ -9,6 +11,8 @@ export const bootstrap = async(): Promise => { const parser: CodekastenParser = new CodekastenParser(); const codekastenGraph: NoteGraph = new NoteGraph(); await codekastenGraph.populateGraph(vscode.workspace.findFiles('**/*.md', '{.codekasten, ./index.md}'), parser); + Logger.info(`Loaded ${codekastenGraph.graph.nodeCount()} Notes and ${codekastenGraph.graph.edgeCount()} Links`); + const filesystemSyncher: FilesystemSyncher = new FilesystemSyncher(codekastenGraph); watcher.onDidCreate(async uri => { diff --git a/src/services/filesystemSyncher.ts b/src/services/filesystemSyncher.ts index 05e7f58..772bae2 100644 --- a/src/services/filesystemSyncher.ts +++ b/src/services/filesystemSyncher.ts @@ -19,6 +19,14 @@ export class FilesystemSyncher{ this.graph.updateId(oldId, newId); + for(const id of this.graph.graph.nodes()){ + const node = this.graph.getNote(id); + if (node.path.includes('python')){ + console.log(node.path); + } + console.log(node.path); + } + // Adjust forward links: They have a new source const forwardLinkTargets: string[] = this.graph.getForwardLinksAsString(newId); for(const forwardLinkTarget of forwardLinkTargets){ diff --git a/src/services/index.ts b/src/services/index.ts new file mode 100644 index 0000000..7c35453 --- /dev/null +++ b/src/services/index.ts @@ -0,0 +1,10 @@ +import { CodekastenLogger, Logger } from './logger'; +import { FilesystemSyncher } from './filesystemSyncher'; +import { bootstrap} from './bootstrap'; + +export { + bootstrap, + CodekastenLogger, + FilesystemSyncher, + Logger +}; \ No newline at end of file diff --git a/src/services/logger.ts b/src/services/logger.ts new file mode 100644 index 0000000..6817981 --- /dev/null +++ b/src/services/logger.ts @@ -0,0 +1,95 @@ +import * as vscode from 'vscode'; + +export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; + + +abstract class BaseLogger { + constructor(private level: LogLevel = 'info') {} + + private static severity = { + debug: 1, + info: 2, + warn: 3, + error: 4, + }; + + abstract log(lvl: LogLevel, msg?: any, ...extra: any[]): void; + + doLog(msgLevel: LogLevel, message?: any, ...params: any[]): void { + if (BaseLogger.severity[msgLevel] >= BaseLogger.severity[this.level]) { + this.log(msgLevel, message, ...params); + } + } + + debug(message?: any, ...params: any[]): void { + this.doLog('debug', message, ...params); + } + info(message?: any, ...params: any[]): void { + this.doLog('info', message, ...params); + } + warn(message?: any, ...params: any[]): void { + this.doLog('warn', message, ...params); + } + error(message?: any, ...params: any[]): void { + this.doLog('error', message, ...params); + } + + getLevel(): LogLevel { + return this.level; + } + setLevel(level: LogLevel): void { + this.level = level; + } + +} + +export class ConsoleLogger extends BaseLogger { + log(level: LogLevel, msg?: string, ...params: any[]): void { + console[level](`[${level}] ${msg}`, ...params); + } + } + + + +export class CodekastenLogger extends BaseLogger { + private channel = vscode.window.createOutputChannel("CodekastenLog"); + + log(lvl: LogLevel, msg?: any, ...extra: any[]): void { + if (msg) { + this.channel.appendLine(`[${lvl} - ${new Date().toLocaleTimeString()}] ${msg}`); + } + } + + show() { + this.channel.show(); + } +} + + +export class Logger { + static debug(message?: any, ...params: any[]): void { + Logger.defaultLogger.debug(message, ...params); + } + static info(message?: any, ...params: any[]): void { + Logger.defaultLogger.info(message, ...params); + } + static warn(message?: any, ...params: any[]): void { + Logger.defaultLogger.warn(message, ...params); + } + static error(message?: any, ...params: any[]): void { + Logger.defaultLogger.error(message, ...params); + } + static getLevel(): LogLevel { + return Logger.defaultLogger.getLevel(); + } + static setLevel(level: LogLevel): void { + Logger.defaultLogger.setLevel(level); + } + + private static defaultLogger: BaseLogger = new CodekastenLogger(); + + static setDefaultLogger(logger: BaseLogger) { + Logger.defaultLogger = logger; + } + } + \ No newline at end of file diff --git a/src/vscode/NoteActions.ts b/src/vscode/NoteActions.ts index dbbc260..1dbb240 100644 --- a/src/vscode/NoteActions.ts +++ b/src/vscode/NoteActions.ts @@ -73,6 +73,7 @@ export async function replaceTextInFile(filePath: string, oldText: string, newTe const newContent: string = content.replace(oldText, newText); if(content !== newContent) { await createNote(filePath, newContent, true); + console.log(`Replaced text in file ${filePath}: oldText "${oldText}", newText "${newText}`); } }