/****************************************************************************** __ ____ __ / / ___ ____ _/ __/_____________ _/ /__ / / / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \ / /___/ __/ /_/ / __(__ ) /__/ /_/ / / __/ /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/ (C)opyright 2026, Leafscale, LLC - https://www.leafscale.com Project: repoman Filename: src/log.reef Authors: Chris Tusa License: Description: Per-invocation log file writer ******************************************************************************/ module log import core.str import io.console as console import io.file as iofile import io.dir as iodir import paths export fn open_log(logdir: string, project: string, verb: string): bool proc write(msg: string) end export // Module-level state: the path of the currently-open log file, and a flag // indicating whether file writes are enabled. Both are set by open_log; if // open fails (cannot create logdir, cannot truncate file), log_enabled stays // false and write() degrades to stderr-only. mut log_path: string = "" mut log_enabled: bool = false // Prepare a log file for the current invocation. Naming: /-.log. // Truncates any existing file. Returns false on setup failure (printing a // warning to stderr); the caller should still proceed — write() will fall // back to stderr-only output. fn open_log(logdir: string, project: string, verb: string): bool let expanded: string = paths.expand_home(logdir) if not iodir.dir_exists(expanded) if not iodir.create_dir_all(expanded) console.printErr("repoman: warning: cannot create logdir " + expanded + "; file logging disabled") log_enabled = false return false end if end if let filename: string = project + "-" + verb + ".log" let path: string = paths.join(expanded, filename) // Truncate (or create) the file by writing an empty string. if not iofile.writeFile(path, "") console.printErr("repoman: warning: cannot open " + path + " for writing; file logging disabled") log_enabled = false return false end if log_path = path log_enabled = true return true end open_log // Print a message to stderr (always) and append it to the open log file // (when logging is enabled). Mirrors what the user sees on stderr into // the log file for after-the-fact troubleshooting. proc write(msg: string) console.printErr(msg) if log_enabled let _w: bool = iofile.appendFile(log_path, msg + "\n") end if end write end module