Skip to content

Commit

Permalink
🔊 wrap console w/ logger + debug settings.
Browse files Browse the repository at this point in the history
Resolves #218
  • Loading branch information
ebullient committed Jan 9, 2025
1 parent e201280 commit aad78b6
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 86 deletions.
20 changes: 16 additions & 4 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getDailyNoteSettings,
} from "obsidian-daily-notes-interface";
import { DAY_PLANNER_DEFAULT_CONTENT, DAY_PLANNER_FILENAME } from "./constants";
import Logger from "./logger";
import MomentDateRegex from "./moment-date-regex";
import {
DayPlannerMode,
Expand Down Expand Up @@ -71,7 +72,10 @@ export default class DayPlannerFile {
await this.createFileIfNotExists(this.todayPlannerFilePath());
}
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"error updating file settings",
error,
);
}
}

Expand All @@ -86,7 +90,7 @@ export default class DayPlannerFile {
await this.vault.createFolder(normalizedPath);
}
} catch (error) {
console.log(error);
Logger.getInstance().logError("error creating folder", path, error);
}
}

Expand All @@ -100,7 +104,11 @@ export default class DayPlannerFile {
);
}
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"file does not exist",
fileName,
error,
);
}
}

Expand All @@ -111,7 +119,11 @@ export default class DayPlannerFile {
try {
return await this.vault.adapter.process(filename, handler);
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"error processing file",
filename,
error,
);
}
}
}
37 changes: 37 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { DayPlannerSettings } from "./settings";

export default class Logger {
private static instance: Logger;
settings: DayPlannerSettings;

public static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger();
}
return Logger.instance;
}

updateSettings(settings: DayPlannerSettings) {
this.settings = settings;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
logDebug(message: string, ...optionalParams: any[]): void {
if (!this.settings || this.settings.debug) {
console.debug("(DP)", message, ...optionalParams);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
logInfo(message: string, ...optionalParams: any[]): void {
console.info("(DP)", message, ...optionalParams);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
logError(message: string, ...optionalParams: any[]): void {
console.error("(DP)", message, ...optionalParams);
}
}
120 changes: 66 additions & 54 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { appHasDailyNotesPluginLoaded } from "obsidian-daily-notes-interface";
import { VIEW_TYPE_TIMELINE } from "./constants";
import DayPlannerFile from "./file";
import Logger from "./logger";
import Parser from "./parser";
import { PlanSummaryData } from "./plan-data";
import PlannerMarkdown from "./planner-md";
Expand Down Expand Up @@ -75,54 +76,17 @@ export default class DayPlanner extends Plugin {
}

async onload() {
console.log(`Loading Day Planner plugin v${this.manifest.version}`);
Logger.getInstance().logInfo(
`Loading Day Planner plugin v${this.manifest.version}`,
);
this.vault = this.app.vault;
this.settings = Object.assign(
new DayPlannerSettings(),
await this.loadData(),
);
Logger.getInstance().updateSettings(this.settings);
this.notesForDatesQuery = new NoteForDateQuery();
this.file = new DayPlannerFile(this.vault, this.settings);

const syncEnabled = this.syncPlugin?.instance !== undefined;
this.device = syncEnabled
? this.syncPlugin.instance.deviceName.length > 0
? this.syncPlugin.instance.deviceName
: this.syncPlugin.instance.getDefaultDeviceName()
: "Unknown";
console.debug(
"DPOG: Device/Writer:",
this.settings.mode,
this.device,
this.settings.writer,
this.isWriter(),
);

const progress = new Progress();
this.parser = new Parser(this.settings);
this.plannerMD = new PlannerMarkdown(
this.app.workspace,
this.settings,
this.file,
this.parser,
progress,
);

this.statusBar = new StatusBar(
this.settings,
this.addStatusBarItem(),
this.app.workspace,
progress,
new PlannerMarkdown(
this.app.workspace,
this.settings,
this.file,
this.parser,
progress,
),
this.file,
);
this.statusBar.initStatusBar();
this.registerEvent(this.app.vault.on("modify", this.codeMirror, ""));

this.addCommand({
Expand Down Expand Up @@ -194,16 +158,61 @@ export default class DayPlanner extends Plugin {
});

this.addSettingTab(new DayPlannerSettingsTab(this.app, this));
this.setTicker();
window.dayPlanner = { resolvePath: this.resolvePath.bind(this) };
this.register(() => {
window.dayPlanner = undefined;
});
this.app.workspace.onLayoutReady(this.layoutReady);
}

layoutReady = async () => {
this.file = new DayPlannerFile(this.vault, this.settings);
this.setTicker();

const syncEnabled = this.syncPlugin?.instance !== undefined;
this.device = syncEnabled
? this.syncPlugin.instance.deviceName.length > 0
? this.syncPlugin.instance.deviceName
: this.syncPlugin.instance.getDefaultDeviceName()
: "Unknown";
Logger.getInstance().logInfo(
"Device/Writer:",
this.settings.mode,
this.device,
this.settings.writer,
this.isWriter(),
);

const progress = new Progress();
this.parser = new Parser(this.settings);
this.plannerMD = new PlannerMarkdown(
this.app.workspace,
this.settings,
this.file,
this.parser,
progress,
);

this.statusBar = new StatusBar(
this.settings,
this.addStatusBarItem(),
this.app.workspace,
progress,
new PlannerMarkdown(
this.app.workspace,
this.settings,
this.file,
this.parser,
progress,
),
this.file,
);
this.statusBar.initStatusBar();
window.dayPlanner = { resolvePath: this.resolvePath.bind(this) };
};

async onunload() {
if (this.interval) {
console.debug("DPOG: Clearing ticker");
Logger.getInstance().logDebug("Clearing ticker");
window.clearInterval(this.interval);
}
}
Expand All @@ -212,9 +221,10 @@ export default class DayPlanner extends Plugin {
await super.handleConfigFileChange();
this.settings = Object.assign(this.settings, await this.loadData());
this.parser.updateSettings(this.settings);
Logger.getInstance().updateSettings(this.settings);
const hasNote = await this.file.hasTodayNote();
console.log(
"DPOG: update Device/Writer:",
Logger.getInstance().logInfo(
"update Device/Writer:",
this.settings.mode,
this.device,
this.settings.writer,
Expand All @@ -233,12 +243,12 @@ export default class DayPlanner extends Plugin {

setTicker() {
if (this.interval) {
console.debug("DPOG: Clearing ticker");
Logger.getInstance().logDebug("Clearing ticker");
window.clearInterval(this.interval);
this.interval = undefined;
}
console.log(
"DPOG: set Ticker with Device/Writer:",
Logger.getInstance().logDebug(
"set Ticker with Device/Writer:",
this.device,
this.settings.writer,
this.isWriter(),
Expand All @@ -249,7 +259,7 @@ export default class DayPlanner extends Plugin {
async tick() {
try {
if (await this.file.hasTodayNote()) {
// console.debug('DPOG: Active note found, starting file processing')
// Logger.getInstance().logDebug('Active note found, starting file processing')
const planSummary = await this.plannerMD.processDayPlanner(
this.isWriter(),
);
Expand All @@ -259,15 +269,18 @@ export default class DayPlanner extends Plugin {
this.settings.mode === DayPlannerMode.Daily &&
appHasDailyNotesPluginLoaded()
) {
// console.debug('DPOG: Clearing status bar & timeline in case daily note was deleted')
// Logger.getInstance().logDebug('Clearing status bar & timeline in case daily note was deleted')
const planSummary = new PlanSummaryData([], this.isWriter());
await this.statusBar.refreshStatusBar(planSummary);
this.timelineView?.update(planSummary);
} else {
// console.debug('DPOG: No active note, skipping file processing')
// Logger.getInstance().logDebug('No active note, skipping file processing')
}
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"error refreshing plan (tick)",
error,
);
}
}

Expand Down Expand Up @@ -360,7 +373,6 @@ export default class DayPlanner extends Plugin {

codeMirror = (file: TAbstractFile) => {
if (this.file.hasTodayNote()) {
// console.log('Active note found, starting CodeMirror monitoring')
this.plannerMD.checkIsDayPlannerEditing();
} else {
// console.log('No active note, skipping CodeMirror monitoring')
Expand Down
3 changes: 2 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from "./logger";
import {
type PlanItem,
PlanItemFactory,
Expand Down Expand Up @@ -102,7 +103,7 @@ export default class Parser {
);
}
} catch (error) {
console.log(error);
Logger.getInstance().logError("error parsing line", line, error);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/plan-data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from "./logger";
import type { DayPlannerSettings } from "./settings";

export class PlanSummaryData {
Expand Down Expand Up @@ -54,7 +55,7 @@ export class PlanSummaryData {
}
});
} catch (error) {
console.log(error);
Logger.getInstance().logError("error updating item status", error);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/planner-md.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MarkdownView, type Workspace } from "obsidian";
import { DAY_PLANNER_DEFAULT_CONTENT } from "./constants";
import type DayPlannerFile from "./file";
import Logger from "./logger";
import type Parser from "./parser";
import { PlanSummaryData } from "./plan-data";
import type Progress from "./progress";
Expand Down Expand Up @@ -60,7 +61,12 @@ export default class PlannerMarkdown {

return summary;
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"error processing file",
iAmWriter,
this.file,
error,
);
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/progress.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from "./logger";
import type { PlanItem } from "./plan-data";
const moment = window.moment;

Expand All @@ -16,7 +17,12 @@ export default class Progress {
const minsUntilNext = untilNext.asMinutes().toFixed(0);
return { percentageComplete, minsUntilNext };
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"error updating progress",
current,
next,
error,
);
}
}

Expand All @@ -29,7 +35,12 @@ export default class Progress {
new Array(20 - completeCount).join("_ ")
);
} catch (error) {
console.log(error);
Logger.getInstance().logError(
"error updating markdown",
current,
next,
error,
);
}
}
}
Loading

0 comments on commit aad78b6

Please sign in to comment.