diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-06-08 14:19:11 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-06-08 14:58:24 +0900 |
| commit | f869ff95a12756de97827b4afd93763f9661f144 (patch) | |
| tree | 6f5445ae623f44ceb99c8f7ddb5bb4cd1fb04aa0 /message | |
| parent | 725f2e0ef3eaba1ad46b597604a684125ab93911 (diff) | |
all: apply modernisers
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'message')
| -rw-r--r-- | message/message.go | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/message/message.go b/message/message.go index aa4870f9..3bb223a1 100644 --- a/message/message.go +++ b/message/message.go @@ -1,4 +1,5 @@ -// Package message provides interfaces and a base implementation for extended reporting on top of [log.Logger] +// Package message provides interfaces and a base implementation for extended +// reporting on top of [log.Logger] package message import ( @@ -15,10 +16,11 @@ type Error interface { error } -// GetMessage returns whether an error implements [Error], and the message if it does. +// GetMessage returns whether an error implements [Error], and the message if +// it does. func GetMessage(err error) (string, bool) { - var e Error - if !errors.As(err, &e) || e == nil { + e, ok := errors.AsType[Error](err) + if !ok || e == nil { return "", false } return e.Message(), true @@ -29,30 +31,43 @@ type Msg interface { // GetLogger returns the address of the underlying [log.Logger]. GetLogger() *log.Logger - // IsVerbose atomically loads and returns whether [Msg] has verbose logging enabled. + // IsVerbose atomically loads and returns whether [Msg] has verbose logging + // enabled. IsVerbose() bool - // SwapVerbose atomically stores a new verbose state and returns the previous value held by [Msg]. + // SwapVerbose atomically stores a new verbose state and returns the + // previous value held by [Msg]. SwapVerbose(verbose bool) bool - // Verbose passes its argument to the Println method of the underlying [log.Logger] if IsVerbose returns true. + // Verbose passes its argument to the Println method of the underlying + // [log.Logger] if IsVerbose returns true. Verbose(v ...any) - // Verbosef passes its argument to the Printf method of the underlying [log.Logger] if IsVerbose returns true. + // Verbosef passes its argument to the Printf method of the underlying + // [log.Logger] if IsVerbose returns true. Verbosef(format string, v ...any) - // Suspend causes the embedded [Suspendable] to withhold writes to its downstream [io.Writer]. - // Suspend returns false and is a noop if called between calls to Suspend and Resume. + // Suspend causes the embedded [Suspendable] to withhold writes to its + // downstream [io.Writer]. + // + // Suspend returns false and is a noop if called between calls to Suspend + // and Resume. Suspend() bool - // Resume dumps the entire buffer held by the embedded [Suspendable] and stops withholding future writes. + // Resume dumps the entire buffer held by the embedded [Suspendable] and + // stops withholding future writes. + // // Resume returns false and is a noop if a call to Suspend does not precede it. Resume() bool - // BeforeExit runs implementation-specific cleanup code, and optionally prints warnings. + // BeforeExit runs implementation-specific cleanup code, and optionally + // prints warnings. + // // BeforeExit is called before [os.Exit]. BeforeExit() } // defaultMsg is the default implementation of the [Msg] interface. -// The zero value is not safe for use. Callers should use the [New] function instead. +// +// The zero value is not safe for use. Callers should use the [New] function +// instead. type defaultMsg struct { verbose atomic.Bool @@ -61,8 +76,9 @@ type defaultMsg struct { } // New initialises a downstream [log.Logger] for a new [Msg]. -// The [log.Logger] should no longer be configured after [New] returns. -// If downstream is nil, a new logger is initialised in its place. +// +// The [log.Logger] should no longer be configured after [New] returns. If +// downstream is nil, a new logger is initialised in its place. func New(downstream *log.Logger) Msg { if downstream == nil { downstream = log.New(log.Writer(), "container: ", 0) @@ -94,7 +110,8 @@ func (msg *defaultMsg) Verbosef(format string, v ...any) { func (msg *defaultMsg) Resume() bool { resumed, dropped, _, err := msg.Suspendable.Resume() if err != nil { - // probably going to result in an error as well, so this message is as good as unreachable + // probably going to result in an error as well, so this message is as + // good as unreachable msg.logger.Printf("cannot dump buffer on resume: %v", err) } if resumed && dropped > 0 { @@ -103,7 +120,8 @@ func (msg *defaultMsg) Resume() bool { return resumed } -// BeforeExit prints a message if called between calls to [Suspendable.Suspend] and Resume. +// BeforeExit prints a message if called between calls to [Suspendable.Suspend] +// and Resume. func (msg *defaultMsg) BeforeExit() { if msg.Resume() { msg.logger.Printf("beforeExit reached on suspended output") |
