aboutsummaryrefslogtreecommitdiff
path: root/internal/log
diff options
context:
space:
mode:
authortsne <tsne.dev@outlook.com>2026-07-06 16:52:20 +0200
committertsne <tsne.dev@outlook.com>2026-07-06 19:21:15 +0200
commit2c41acab4c97f584e8e199b31dc456194b8d7e6c (patch)
tree42381deb6c4f6da0f32945432207a31b4d86664e /internal/log
downloadhopper-2c41acab4c97f584e8e199b31dc456194b8d7e6c.tar.gz
initial
Diffstat (limited to 'internal/log')
-rw-r--r--internal/log/log.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/internal/log/log.go b/internal/log/log.go
new file mode 100644
index 0000000..68635c3
--- /dev/null
+++ b/internal/log/log.go
@@ -0,0 +1,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...)
+}