Skip to content

Commit

Permalink
Implemented Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasSchatz committed Dec 14, 2020
1 parent d747a68 commit 23611ca
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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();


Expand Down
1 change: 1 addition & 0 deletions src/features/create-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/features/search-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions src/services/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,6 +11,8 @@ export const bootstrap = async(): Promise<NoteGraph> => {
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 => {
Expand Down
8 changes: 8 additions & 0 deletions src/services/filesystemSyncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
10 changes: 10 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CodekastenLogger, Logger } from './logger';
import { FilesystemSyncher } from './filesystemSyncher';
import { bootstrap} from './bootstrap';

export {
bootstrap,
CodekastenLogger,
FilesystemSyncher,
Logger
};
95 changes: 95 additions & 0 deletions src/services/logger.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

1 change: 1 addition & 0 deletions src/vscode/NoteActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

}
Expand Down

0 comments on commit 23611ca

Please sign in to comment.