TextIO.openIn:"No such file or directory" in Poly/ML

27 Views Asked by At

I hope the code to output log information to log.txt in the current folder.
But it says the folder cannot be found.
Could you please help me find out what the current problem is, how I can modify it, and output the relevant information to this folder.
Thanks

Exception- Io {cause = SysErr ("No such file or directory", SOME ENOENT), function = "TextIO.openIn", name = "TextIO"} raised
Exception- Io {cause = SysErr ("No such file or directory", SOME ENOENT), function = "TextIO.openIn", name = "TextIO"} raised
(* Function to open a log file *)
    fun openLogFile () =
      let
        val outStream = TextIO.openOut "log.txt" (* Opens or creates "log.txt" in the current directory *)
      in
        logFile := SOME outStream;
        outStream
      end

    (* Function to log debug messages. Now supports file logging *)
    fun logDebugMessage msg =
      case !logFile of
          NONE => if !debugMode then print ("[DEBUG] " ^ msg ^ "\n") else ()
        | SOME outStream => (TextIO.output (outStream, "[DEBUG] " ^ msg ^ "\n"); TextIO.flush outStream)

    (* Function to process input arguments *)
    fun processInput input =
      if input = "-debug" then (
        let
          val outStream = openLogFile ()
          (* Define a function to write to the log and flush *)
          fun logAndFlush msg = (
            TextIO.output (outStream, msg ^ "\n");
            TextIO.flush outStream
          )
        in
          debugMode := true;
          logAndFlush "Debug mode enabled."
          (* Remember to close the stream when done, or do it in cleanup *)
          (* TextIO.closeOut outStream; *)
        end
      ) else (
        textLabel := input;
        (* Assuming you have a way to check if debug mode is on and log accordingly *)
        if !debugMode then logDebugMessage ("Setting text label to: " ^ input)
        else ()
      );

    (* Cleanup function to close the log file if it's open *)
    fun cleanup () =
      case !logFile of
          NONE => ()
        | SOME outStream => TextIO.closeOut outStream

    in
      case args of
          [] => print "No command line arguments provided.\n"
        | "--debug" :: _ => (debugMode := true; ignore (openLogFile ()); logDebugMessage "Debug mode enabled.")
        | arg :: _ => processInput arg;

      (* Ensure the cleanup function is called before the program exits *)
      cleanup ()
    end
  handle e => (cleanup (); Giraffe.error 1 ["Uncaught exception\n", exnMessage e, "\n"])
0

There are 0 best solutions below