import{join,relative,sep}from"node:path";import{chmod,mkdir,open}from"node:fs/promises";async function createDevDiagnosticSink(t,n={}){let o=join(t,`.eve`,`logs`);await mkdir(o,{recursive:!0,mode:448}),await chmod(o,448);let s=n.now?.()??new Date,c=n.pid??process.pid,l=join(o,`dev-${s.toISOString().replaceAll(`:`,`-`)}-${c}.log`);return createOpenSink(t,l,await open(l,`wx`,384),()=>n.now?.()??new Date)}function createOpenSink(e,r,i,a){let o=Promise.resolve(),s=!1,c=!1;return{path:r,displayPath:relative(e,r).split(sep).join(`/`),append(e){if(c||s)return;let t=formatDiagnosticEntry(e,a());o=o.then(()=>i.appendFile(t,`utf8`)).catch(()=>{s=!0})},async close(){c||(c=!0,await o,await i.close())}}}function formatDiagnosticEntry(e,t){return`${JSON.stringify({at:t.toISOString(),...e})}\n`}export{createDevDiagnosticSink};