mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 01:02:01 +03:00
Refactor console logs to use centralized logger (#72)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
parent
f84de44bc2
commit
d89ed69c63
9 changed files with 55 additions and 40 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "../logger";
|
||||
// Public facade: evaluates whole documents line by line and exposes the
|
||||
// extension API (numi.addUnit / addFunction / setVariable).
|
||||
import { DateOrder, Decimal, EngineSettings, EvalError, Quantity, Unit, Value, XRefError, defaultSettings, qty } from "./types";
|
||||
|
|
@ -147,7 +148,7 @@ export class SumEngine {
|
|||
} else {
|
||||
// Capture unexpected execution anomalies like BigInt or Range errors
|
||||
lineError = e instanceof Error ? e.message : String(e);
|
||||
console.warn("evaluate failed:", e);
|
||||
logger.warn("evaluate failed:", e);
|
||||
}
|
||||
// a line without a result beats a dead sheet.
|
||||
value = null;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { makeApi, runExtensions } from "./extensions";
|
||||
import { SumEngine } from "./engine";
|
||||
|
|
@ -41,8 +42,8 @@ describe("extensions", () => {
|
|||
let consoleWarnSpy: any;
|
||||
|
||||
beforeEach(() => {
|
||||
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
consoleErrorSpy = vi.spyOn(logger, "error").mockImplementation(() => {});
|
||||
consoleWarnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
|
@ -185,6 +186,8 @@ describe("extensions", () => {
|
|||
newQuickJSWASMModuleFromVariant: () => Promise.reject(new Error("no WebAssembly")),
|
||||
}));
|
||||
const { runExtensions: run } = await import("./extensions");
|
||||
const { logger } = await import("./logger");
|
||||
consoleWarnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
||||
|
||||
await run(mockEngine as SumEngine, [{ name: "s", code: "numi.setVariable('x', 1);" }]);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
// JS extensions runtime: numi.setVariable, numi.addUnit, numi.addFunction.
|
||||
// Scripts run inside a QuickJS WASM sandbox — no DOM, no Tauri IPC, no host globals.
|
||||
import {
|
||||
|
|
@ -75,7 +76,7 @@ export async function runExtensions(
|
|||
try {
|
||||
await load(engine, scripts);
|
||||
} catch (e) {
|
||||
console.warn("extensions disabled: QuickJS sandbox unavailable:", e);
|
||||
logger.warn("extensions disabled: QuickJS sandbox unavailable:", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -200,7 +201,7 @@ function evaluateScripts(
|
|||
}
|
||||
res.value.dispose();
|
||||
} catch (e) {
|
||||
console.error(`extension ${s.name} failed:`, e);
|
||||
logger.error(`extension ${s.name} failed:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
5
src/logger.ts
Normal file
5
src/logger.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
export const logger = {
|
||||
info: (...args: any[]) => console.info(...args),
|
||||
warn: (...args: any[]) => console.warn(...args),
|
||||
error: (...args: any[]) => console.error(...args),
|
||||
};
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
/** App bootstrap: engine + editor + documents + settings + Tauri glue. */
|
||||
import "./ui/app.css";
|
||||
import { SumEngine } from "./engine";
|
||||
|
|
@ -436,7 +437,7 @@ function bindDataDirSettings(): void {
|
|||
await flushAppData();
|
||||
await migrateDataDir(settings.dataDir, picked, strategy);
|
||||
} catch (e) {
|
||||
console.warn("migrate failed", e);
|
||||
logger.warn("migrate failed", e);
|
||||
toast(t("folderError"));
|
||||
return;
|
||||
}
|
||||
|
|
@ -537,7 +538,7 @@ async function registerHotkey(old: string | null, combo: string): Promise<boolea
|
|||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.warn("hotkey registration failed", e);
|
||||
logger.warn("hotkey registration failed", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -549,14 +550,14 @@ async function applyAutostart(enabled: boolean): Promise<void> {
|
|||
if (enabled) await auto.enable();
|
||||
else await auto.disable();
|
||||
} catch (e) {
|
||||
console.warn("autostart failed", e);
|
||||
logger.warn("autostart failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyAlwaysOnTop(enabled: boolean): Promise<void> {
|
||||
if (!isTauri()) return;
|
||||
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
||||
await getCurrentWindow().setAlwaysOnTop(enabled).catch((e) => console.warn("always-on-top failed", e));
|
||||
await getCurrentWindow().setAlwaysOnTop(enabled).catch((e) => logger.warn("always-on-top failed", e));
|
||||
}
|
||||
|
||||
async function refreshRates(force = false): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { isTauri, loadSettings, saveSettings, defaultSettingsData, loadAppData, saveAppData, flushAppData, setDataDir, runBackups, backupDeletedSheet, openBackupsFolder, chooseFolder, dataDirHasDocuments, migrateDataDir, fetchRates, fetchHistoricalRates } from './storage';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
|
@ -14,7 +15,7 @@ describe('storage', () => {
|
|||
getItem: vi.fn(),
|
||||
setItem: vi.fn(),
|
||||
});
|
||||
vi.stubGlobal('console', { warn: vi.fn() });
|
||||
vi.spyOn(logger, 'warn').mockImplementation(() => {});
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
|
|
@ -54,7 +55,7 @@ describe('storage', () => {
|
|||
it('falls back to defaultSettingsData if localStorage throws', async () => {
|
||||
vi.mocked(localStorage.getItem).mockImplementation(() => { throw new Error('localStorage error'); });
|
||||
const settings = await loadSettings();
|
||||
expect(console.warn).toHaveBeenCalledWith('loadSettings failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('loadSettings failed', expect.any(Error));
|
||||
expect(settings).toEqual(defaultSettingsData);
|
||||
});
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ describe('storage', () => {
|
|||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
vi.mocked(invoke).mockRejectedValue(new Error('invoke error'));
|
||||
const settings = await loadSettings();
|
||||
expect(console.warn).toHaveBeenCalledWith('loadSettings failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('loadSettings failed', expect.any(Error));
|
||||
expect(settings).toEqual(defaultSettingsData);
|
||||
});
|
||||
|
||||
|
|
@ -75,7 +76,7 @@ describe('storage', () => {
|
|||
it('falls back to defaultSettingsData if JSON is invalid', async () => {
|
||||
vi.mocked(localStorage.getItem).mockReturnValue("invalid json");
|
||||
const settings = await loadSettings();
|
||||
expect(console.warn).toHaveBeenCalledWith('loadSettings failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('loadSettings failed', expect.any(Error));
|
||||
expect(settings).toEqual(defaultSettingsData);
|
||||
});
|
||||
});
|
||||
|
|
@ -124,7 +125,7 @@ describe('storage', () => {
|
|||
it('returns null if localStorage throws', async () => {
|
||||
vi.mocked(localStorage.getItem).mockImplementation(() => { throw new Error('localStorage error'); });
|
||||
const data = await loadAppData('');
|
||||
expect(console.warn).toHaveBeenCalledWith('loadAppData failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('loadAppData failed', expect.any(Error));
|
||||
expect(data).toBeNull();
|
||||
});
|
||||
|
||||
|
|
@ -132,7 +133,7 @@ describe('storage', () => {
|
|||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
vi.mocked(invoke).mockRejectedValue(new Error('invoke error'));
|
||||
const data = await loadAppData('');
|
||||
expect(console.warn).toHaveBeenCalledWith('loadAppData failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('loadAppData failed', expect.any(Error));
|
||||
expect(data).toBeNull();
|
||||
});
|
||||
|
||||
|
|
@ -145,7 +146,7 @@ describe('storage', () => {
|
|||
it('returns null if JSON is invalid', async () => {
|
||||
vi.mocked(localStorage.getItem).mockReturnValue("invalid json");
|
||||
const data = await loadAppData('');
|
||||
expect(console.warn).toHaveBeenCalledWith('loadAppData failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('loadAppData failed', expect.any(Error));
|
||||
expect(data).toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
@ -223,7 +224,7 @@ describe('storage', () => {
|
|||
|
||||
await flushAppData();
|
||||
|
||||
expect(console.warn).toHaveBeenCalledWith('saveAppData failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('saveAppData failed', expect.any(Error));
|
||||
});
|
||||
|
||||
it('handles invoke throw gracefully in flushAppData', async () => {
|
||||
|
|
@ -234,7 +235,7 @@ describe('storage', () => {
|
|||
|
||||
await flushAppData();
|
||||
|
||||
expect(console.warn).toHaveBeenCalledWith('saveAppData failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('saveAppData failed', expect.any(Error));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -255,7 +256,7 @@ describe('storage', () => {
|
|||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
vi.mocked(invoke).mockRejectedValue(new Error('invoke error'));
|
||||
await runBackups('dir', 10);
|
||||
expect(console.warn).toHaveBeenCalledWith('runBackups failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('runBackups failed', expect.any(Error));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -281,7 +282,7 @@ describe('storage', () => {
|
|||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
vi.mocked(invoke).mockRejectedValue(new Error('invoke error'));
|
||||
await backupDeletedSheet('dir', 'title', 'contents');
|
||||
expect(console.warn).toHaveBeenCalledWith('backupDeletedSheet failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('backupDeletedSheet failed', expect.any(Error));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -369,7 +370,7 @@ describe('storage', () => {
|
|||
it('returns null if fetch fails', async () => {
|
||||
vi.mocked(fetch).mockRejectedValue(new Error('Network error'));
|
||||
const result = await fetchRates();
|
||||
expect(console.warn).toHaveBeenCalledWith('fetchRates failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('fetchRates failed', expect.any(Error));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
|
|
@ -404,7 +405,7 @@ describe('storage', () => {
|
|||
it('returns null if fetch fails', async () => {
|
||||
vi.mocked(fetch).mockRejectedValue(new Error('Network error'));
|
||||
const result = await fetchHistoricalRates('2024-01-01');
|
||||
expect(console.warn).toHaveBeenCalledWith('fetchHistoricalRates failed', '2024-01-01', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('fetchHistoricalRates failed', '2024-01-01', expect.any(Error));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
// Storage: Tauri commands in the app, localStorage fallback for `vite dev`.
|
||||
|
||||
export interface DocMeta {
|
||||
|
|
@ -79,7 +80,7 @@ export async function loadSettings(): Promise<SettingsData> {
|
|||
: localStorage.getItem("summarum.settings");
|
||||
if (raw) return { ...defaultSettingsData, ...JSON.parse(raw) };
|
||||
} catch (e) {
|
||||
console.warn("loadSettings failed", e);
|
||||
logger.warn("loadSettings failed", e);
|
||||
}
|
||||
return { ...defaultSettingsData };
|
||||
}
|
||||
|
|
@ -99,7 +100,7 @@ export async function loadAppData(dataDir: string): Promise<AppData | null> {
|
|||
: localStorage.getItem("summarum.documents");
|
||||
if (raw) return JSON.parse(raw);
|
||||
} catch (e) {
|
||||
console.warn("loadAppData failed", e);
|
||||
logger.warn("loadAppData failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -132,7 +133,7 @@ export async function flushAppData(): Promise<void> {
|
|||
if (isTauri()) await invoke("save_documents", { dir: dirArg(currentDataDir), contents: raw });
|
||||
else localStorage.setItem("summarum.documents", raw);
|
||||
} catch (e) {
|
||||
console.warn("saveAppData failed", e);
|
||||
logger.warn("saveAppData failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -150,7 +151,7 @@ export async function runBackups(dataDir: string, retentionDays: number): Promis
|
|||
try {
|
||||
await invoke("run_backups", { dir: dirArg(dataDir), retentionDays });
|
||||
} catch (e) {
|
||||
console.warn("runBackups failed", e);
|
||||
logger.warn("runBackups failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -159,7 +160,7 @@ export async function backupDeletedSheet(dataDir: string, title: string, content
|
|||
try {
|
||||
await invoke("backup_deleted_sheet", { dir: dirArg(dataDir), title, contents });
|
||||
} catch (e) {
|
||||
console.warn("backupDeletedSheet failed", e);
|
||||
logger.warn("backupDeletedSheet failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -205,7 +206,7 @@ export async function fetchRates(force = false): Promise<RatesPayload | null> {
|
|||
return { date: json.time_last_update_utc, rates: json.rates, fetchedAt: Math.floor(Date.now() / 1000) };
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("fetchRates failed", e);
|
||||
logger.warn("fetchRates failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -227,7 +228,7 @@ export async function fetchHistoricalRatesBatch(dates: string[]): Promise<Record
|
|||
);
|
||||
return results;
|
||||
} catch (e) {
|
||||
console.warn("fetchHistoricalRatesBatch failed", e);
|
||||
logger.warn("fetchHistoricalRatesBatch failed", e);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
@ -246,7 +247,7 @@ export async function fetchHistoricalRates(date: string): Promise<Record<string,
|
|||
}
|
||||
return out;
|
||||
} catch (e) {
|
||||
console.warn("fetchHistoricalRates failed", date, e);
|
||||
logger.warn("fetchHistoricalRates failed", date, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -265,7 +266,7 @@ export async function fetchMarketData(symbols: string[]): Promise<Record<string,
|
|||
try {
|
||||
return await invoke<Record<string, number>>("fetch_market_data", { symbols });
|
||||
} catch (e) {
|
||||
console.warn("fetchMarketData failed", e);
|
||||
logger.warn("fetchMarketData failed", e);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
@ -277,7 +278,7 @@ export async function loadExtensionScripts(): Promise<{ name: string; code: stri
|
|||
try {
|
||||
return await invoke<{ name: string; code: string }[]>("load_extensions");
|
||||
} catch (e) {
|
||||
console.warn("loadExtensions failed", e);
|
||||
logger.warn("loadExtensions failed", e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -314,7 +315,7 @@ export async function onFileDrop(cb: (content: string) => void): Promise<void> {
|
|||
const content = await invoke<string>("read_text_file", { path });
|
||||
cb(content);
|
||||
} catch (e) {
|
||||
console.warn("file drop rejected", path, e);
|
||||
logger.warn("file drop rejected", path, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { checkForUpdate } from './updater';
|
||||
import * as storage from './storage';
|
||||
|
|
@ -22,8 +23,8 @@ vi.mock('@tauri-apps/plugin-process', () => ({
|
|||
describe('checkForUpdate', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
vi.spyOn(console, 'info').mockImplementation(() => {});
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
vi.spyOn(logger, 'info').mockImplementation(() => {});
|
||||
vi.spyOn(logger, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it('should return null if not in Tauri', async () => {
|
||||
|
|
@ -39,7 +40,7 @@ describe('checkForUpdate', () => {
|
|||
|
||||
const result = await checkForUpdate();
|
||||
expect(result).toBe('error');
|
||||
expect(console.warn).toHaveBeenCalledWith('update check failed', expect.any(Error));
|
||||
expect(logger.warn).toHaveBeenCalledWith('update check failed', expect.any(Error));
|
||||
});
|
||||
|
||||
it('should return null if no update is available', async () => {
|
||||
|
|
@ -52,7 +53,7 @@ describe('checkForUpdate', () => {
|
|||
|
||||
const result = await checkForUpdate();
|
||||
expect(result).toBeNull();
|
||||
expect(console.info).toHaveBeenCalledWith("no update: running 1.0.0, that's the latest published release");
|
||||
expect(logger.info).toHaveBeenCalledWith("no update: running 1.0.0, that's the latest published release");
|
||||
});
|
||||
|
||||
it('should return an update object if an update is available', async () => {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { logger } from "./logger";
|
||||
import { isTauri } from "./storage";
|
||||
|
||||
interface AvailableUpdate {
|
||||
|
|
@ -19,10 +20,10 @@ export async function checkForUpdate(): Promise<AvailableUpdate | null | "error"
|
|||
// visible in devtools console — the fastest way to tell "already on
|
||||
// the latest published release" apart from "the check silently failed"
|
||||
const { getVersion } = await import("@tauri-apps/api/app");
|
||||
console.info(`no update: running ${await getVersion()}, that's the latest published release`);
|
||||
logger.info(`no update: running ${await getVersion()}, that's the latest published release`);
|
||||
return null;
|
||||
}
|
||||
console.info(`update available: ${update.currentVersion} -> ${update.version}`);
|
||||
logger.info(`update available: ${update.currentVersion} -> ${update.version}`);
|
||||
return {
|
||||
version: update.version,
|
||||
async install() {
|
||||
|
|
@ -32,7 +33,7 @@ export async function checkForUpdate(): Promise<AvailableUpdate | null | "error"
|
|||
},
|
||||
};
|
||||
} catch (e) {
|
||||
console.warn("update check failed", e);
|
||||
logger.warn("update check failed", e);
|
||||
return "error";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue