/** * File-based logging for the native host. * Writes timestamped lines to holesail-browser.log (or BRIDGE_SWARM_LOG override) * and to process.stderr for native messaging host visibility. */ const path = require('bare-path'); const fs = require('bare-fs'); const { BASE_DIR } = require('./paths.js'); const DEBUG = process.env.HOLESAIL_DEBUG === '1' || process.env.HOLESAIL_DEBUG === 'true'; const LOG_FILE = path.join(BASE_DIR, 'holesail-browser.log'); let logStream = null; function getLogStream() { if (!logStream) { try { const logPath = process.env.BRIDGE_SWARM_LOG || LOG_FILE; logStream = fs.createWriteStream(logPath, { flags: 'a' }); } catch (e) { return null; } } return logStream; } function log(...args) { const stream = getLogStream(); if (stream) { const msg = '[' + new Date().toISOString() + '] ' + args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(' ') + '\n'; stream.write(msg); } if (process.stderr) { process.stderr.write('[' + new Date().toISOString() + '] ' + args.join(' ') + '\n'); } } function debugLog(...args) { if (!DEBUG) return; const msg = '[host:debug] ' + args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(' '); if (process.stderr) process.stderr.write(msg + '\n'); const stream = getLogStream(); if (stream) stream.write('[' + new Date().toISOString() + '] ' + msg + '\n'); } module.exports = { log, debugLog };