aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/sandbox/msg.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-03-17 02:31:46 +0900
committerOphestra <cat@gensokyo.uk>2025-03-17 02:44:07 +0900
commit9a1f8e129fe0f9919981b8a1d345a9b455bd76de (patch)
treee52f33a3908c256aebb30f059cabd40f371a886f /internal/sandbox/msg.go
parentee108603572101ad3c285830e04ee248916b6c5d (diff)
sandbox: wrap fmsg interface
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/sandbox/msg.go')
-rw-r--r--internal/sandbox/msg.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/sandbox/msg.go b/internal/sandbox/msg.go
new file mode 100644
index 00000000..5ee18509
--- /dev/null
+++ b/internal/sandbox/msg.go
@@ -0,0 +1,43 @@
+package sandbox
+
+import (
+ "log"
+ "sync/atomic"
+)
+
+type Msg interface {
+ IsVerbose() bool
+ Verbose(v ...any)
+ Verbosef(format string, v ...any)
+ WrapErr(err error, a ...any) error
+ PrintBaseErr(err error, fallback string)
+
+ Suspend()
+ Resume() bool
+
+ BeforeExit()
+}
+
+type DefaultMsg struct{ inactive atomic.Bool }
+
+func (msg *DefaultMsg) IsVerbose() bool { return true }
+func (msg *DefaultMsg) Verbose(v ...any) {
+ if !msg.inactive.Load() {
+ log.Println(v...)
+ }
+}
+func (msg *DefaultMsg) Verbosef(format string, v ...any) {
+ if !msg.inactive.Load() {
+ log.Printf(format, v...)
+ }
+}
+
+func (msg *DefaultMsg) WrapErr(err error, a ...any) error {
+ log.Println(a...)
+ return err
+}
+func (msg *DefaultMsg) PrintBaseErr(err error, fallback string) { log.Println(fallback, err) }
+
+func (msg *DefaultMsg) Suspend() { msg.inactive.Store(true) }
+func (msg *DefaultMsg) Resume() bool { return msg.inactive.CompareAndSwap(true, false) }
+func (msg *DefaultMsg) BeforeExit() {}