import fs from "fs";
import moment from "moment";

export const addLogFunction = async (fileName, data, functionName) => {
  try {
    fs.exists(fileName, async (exists) => {
      if (exists) {
        let old_data = await fs.readFileSync(fileName, "utf8");
        fs.writeFileSync(
          fileName,
          old_data +
            "\n\n[" +
            moment(Date.now()).format("D/M/YY-HH:mm:ss") +
            "] - " +
            functionName +
            "\n" +
            data +
            "\n\n======================================================================"
        );
      } else {
        fs.writeFileSync(
          fileName,
          "\n\n[" +
            moment(Date.now()).format("D/M/YY-HH:mm:ss") +
            "] - System Logs" +
            data +
            "\n\n======================================================================"
        );
      }
    });
  } catch (error) {
    console.log(error);
  }
};
