From 84da43998f7cc42cc72652333ac6e3d293abb23e Mon Sep 17 00:00:00 2001 From: tsne Date: Mon, 6 Jul 2026 16:52:20 +0200 Subject: initial --- internal/log/log.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 internal/log/log.go (limited to 'internal/log/log.go') 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...) +} -- cgit v1.3