|
root / src / log.reef
log.reef Reef 79 lines 2.7 KB
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/******************************************************************************
                __               ____                __   
               / /   ___  ____ _/ __/_____________ _/ /__ 
              / /   / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \
             / /___/  __/ /_/ / __(__  ) /__/ /_/ / /  __/
            /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/ 

    (C)opyright 2026, Leafscale, LLC -  https://www.leafscale.com

    Project: repoman
   Filename: src/log.reef
    Authors: Chris Tusa <chris.tusa@leafscale.com>
    License: <see LICENSE file included with this source code>
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: <logdir>/<project>-<verb>.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