import fs from 'fs';
import os from 'os';
import path from 'path';

const logPath = path.join(os.homedir(), 'Library', 'Application Support', 'mouse', 'openmouse.log');

const log = (x, y, fatal = false) => {
  const line = `[${new Date().toISOString()}] [${x}] ${y}`;
  console.log(line);
  try {
    fs.mkdirSync(path.dirname(logPath), { recursive: true });
    fs.appendFileSync(logPath, `${line}\n`);
  } catch (error) {
    // Console output still works if the file cannot be written.
  }
  if (fatal) {
    process.exit();
  }
};

export default log;
