fmsg: produce all output through fmsg
All checks were successful
test / test (push) Successful in 17s
All checks were successful
test / test (push) Successful in 17s
The behaviour of print functions from package fmt is not thread safe. Functions provided by fmsg wrap around Logger methods. This makes prefix much cleaner and makes it easy to deal with future changes to logging. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
@@ -1,2 +1,41 @@
|
||||
// Package fmsg provides various functions for output messages.
|
||||
package fmsg
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
std = log.New(os.Stdout, "fortify: ", 0)
|
||||
warn = log.New(os.Stderr, "fortify: ", 0)
|
||||
|
||||
verbose = new(atomic.Bool)
|
||||
)
|
||||
|
||||
func SetPrefix(prefix string) {
|
||||
prefix += ": "
|
||||
std.SetPrefix(prefix)
|
||||
warn.SetPrefix(prefix)
|
||||
}
|
||||
|
||||
func Print(v ...any) {
|
||||
warn.Print(v...)
|
||||
}
|
||||
|
||||
func Printf(format string, v ...any) {
|
||||
warn.Printf(format, v...)
|
||||
}
|
||||
|
||||
func Println(v ...any) {
|
||||
warn.Println(v...)
|
||||
}
|
||||
|
||||
func Fatal(v ...any) {
|
||||
warn.Fatal(v...)
|
||||
}
|
||||
|
||||
func Fatalf(format string, v ...any) {
|
||||
warn.Fatalf(format, v...)
|
||||
}
|
||||
|
||||
21
internal/fmsg/verbose.go
Normal file
21
internal/fmsg/verbose.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package fmsg
|
||||
|
||||
func Verbose() bool {
|
||||
return verbose.Load()
|
||||
}
|
||||
|
||||
func SetVerbose(v bool) {
|
||||
verbose.Store(v)
|
||||
}
|
||||
|
||||
func VPrintf(format string, v ...any) {
|
||||
if verbose.Load() {
|
||||
std.Printf(format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func VPrintln(v ...any) {
|
||||
if verbose.Load() {
|
||||
std.Println(v...)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user