Files

35 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

// web/log.mjs -- diagnostics: a rolling event log that survives
// refreshes.
//
// The api() choke point records every server call (status + duration),
// uncaught errors land here, and so do navigation ejections -- the
// "why did the app throw me out" history the ten-second banner loses.
// Kept in sessionStorage; the header's "log" button shows it.
import { $ } from './dom.mjs';
/** @type {string[]} */ const log = [];
try { log.push(...JSON.parse(sessionStorage.getItem('diag') ?? '[]')); } catch { /* fresh or corrupt -- start empty */ }
/** @param {string} kind @param {string} text */
export function logEvent(kind, text) {
log.push(`${new Date().toISOString().slice(11, 19)} ${kind} ${text}`);
if (log.length > 300) log.splice(0, log.length - 300);
try { sessionStorage.setItem('diag', JSON.stringify(log)); } catch { /* storage full or blocked -- the in-memory copy lives on */ }
}
function renderDiag() {
const d = $('diag');
d.textContent = log.join('\n');
d.scrollTop = d.scrollHeight;
}
$('diag-btn').addEventListener('click', () => {
const d = $('diag');
d.hidden = !d.hidden;
if (!d.hidden) renderDiag();
});
window.addEventListener('error', (e) => logEvent('error', e.message));
window.addEventListener('unhandledrejection', (e) => logEvent('error', `unhandled: ${e.reason?.message ?? e.reason}`));