aboutsummaryrefslogtreecommitdiff
path: root/internal/log/log.go
blob: 68635c365cf6c9eec453a63bf3ad075318bdb705 (plain) (blame)
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
// Package log centralizes all user-facing output behind package-level
// functions backed by a private global logger. Normal output (actions, summary)
// goes to stdout; warnings and errors go to stderr. For a single-process CLI a
// global sink avoids threading a logger through every call site.
package log

import (
	"fmt"
	"io"
	"os"
)

// global is the process-wide logger. It defaults to the real stdout/stderr so the
// package is usable without explicit setup (e.g. in tests).
var global = &logger{stdout: os.Stdout, stderr: os.Stderr}

type logger struct {
	stdout  io.Writer
	stderr  io.Writer
	verbose bool
	quiet   bool
}

// Init configures the global logger's destinations. Call once at startup.
func Init(out, err io.Writer) {
	global.stdout = out
	global.stderr = err
}

// SetVerbose toggles whether commands include extra detail in their output.
func SetVerbose(v bool) { global.verbose = v }

// SetQuiet toggles whether normal output is suppressed. Quiet wins over verbose
// (see DESIGN §13): the two are points on one scale, quiet > verbose > default.
func SetQuiet(q bool) { global.quiet = q }

// Verbose reports whether verbose output is enabled. It folds the quiet override
// in, so callers never re-check quiet: quiet suppresses verbose detail.
func Verbose() bool { return global.verbose && !global.quiet }

// Print writes a normal output line to stdout. It is suppressed under quiet.
func Print(format string, args ...any) {
	if !global.quiet {
		fmt.Fprintf(global.stdout, format+"\n", args...)
	}
}

// PrintVerbose writes an extra-detail line to stdout, emitted only when verbose
// output is enabled (and not suppressed by quiet, per Verbose()).
func PrintVerbose(format string, args ...any) {
	if Verbose() {
		fmt.Fprintf(global.stdout, format+"\n", args...)
	}
}

// PrintImportant writes a line to stdout that survives quiet (e.g. the final
// summary).
func PrintImportant(format string, args ...any) {
	fmt.Fprintf(global.stdout, format+"\n", args...)
}

// Warn prints a warning to stderr. Warnings are never suppressed.
func Warn(format string, args ...any) {
	fmt.Fprintf(global.stderr, "Warning: "+format+"\n", args...)
}

// Error prints an error to stderr. Errors are never suppressed.
func Error(format string, args ...any) {
	fmt.Fprintf(global.stderr, "Error: "+format+"\n", args...)
}