Files
wehub-resource-sync 6c9c7fe7f3
CI / integration tests (3.13) (push) Failing after 1s
Commit lint / pull request title (push) Has been skipped
Docs / links (push) Failing after 1s
CI / unit tests (3.13) (push) Failing after 1s
CI / lint (push) Failing after 1s
CI / integration tests (push) Failing after 1s
CI / package build (push) Failing after 1s
Commit lint / commit messages (push) Failing after 1s
CI / unit tests (push) Failing after 1s
chore: import upstream snapshot with attribution
2026-07-13 12:24:24 +08:00

62 lines
1.5 KiB
JavaScript

/**
* Shared debug utility for EverMem hooks
*
* Usage:
* import { debug, setDebugPrefix } from './utils/debug.js';
* setDebugPrefix('inject'); // Optional: add prefix to log lines
* debug('hookInput:', data);
*
* Enable by setting EVERMEM_DEBUG=1 in .env file or environment
* Logs are written to /tmp/evermem-debug.log
*/
import { appendFileSync } from 'fs';
import { isConfigured } from './config.js'; // This loads .env
const DEBUG_LOG_PATH = '/tmp/evermem-debug.log';
// Check debug flag (after config.js loads .env)
const DEBUG = process.env.EVERMEM_DEBUG === '1';
// Optional prefix for log lines (e.g., 'inject' or 'store')
let debugPrefix = '';
/**
* Set a prefix for debug log lines
* @param {string} prefix - Prefix to add (e.g., 'inject', 'store')
*/
export function setDebugPrefix(prefix) {
debugPrefix = prefix ? `[${prefix}] ` : '';
}
/**
* Write debug message to log file
* Only writes when EVERMEM_DEBUG=1
*
* @param {...any} args - Arguments to log (objects are JSON stringified)
*/
export function debug(...args) {
if (!DEBUG) return;
const msg = args.map(a =>
typeof a === 'object' ? JSON.stringify(a, null, 2) : a
).join(' ');
const timestamp = new Date().toISOString();
const line = `[${timestamp}] ${debugPrefix}${msg}\n`;
try {
appendFileSync(DEBUG_LOG_PATH, line);
} catch (e) {
// Silent on write errors
}
}
/**
* Check if debug mode is enabled
* @returns {boolean}
*/
export function isDebugEnabled() {
return DEBUG;
}