Skip to content

Commit

Permalink
add the log output
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed May 13, 2024
1 parent 582b01f commit b9f33ef
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
7 changes: 2 additions & 5 deletions defaults_onexit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

package defaults

import (
"log/slog"
"os"
)
import "os"

var (
// ExitFunc is used to exit the program.
Expand All @@ -36,5 +33,5 @@ func Exit(code int) { ExitFunc.Get()(code) }
func OnExit(f func()) { OnExitFunc.Get()(f) }

func onexit(f func()) {
slog.Warn("system does not set the exit function register", "caller", caller(1))
logwarn("system does not set the exit function register", "caller", caller(2))
}
4 changes: 1 addition & 3 deletions defaults_oninit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package defaults

import "log/slog"

var (
// OnInitFunc is used to register the init function.
OnInitFunc = NewValueWithValidation(oninit, fA1Validation[func()]("OnInit"))
Expand All @@ -25,5 +23,5 @@ var (
func OnInit(f func()) { OnInitFunc.Get()(f) }

func oninit(f func()) {
slog.Warn("system does not set the init function register", "caller", caller(1))
logwarn("system does not set the init function register", "caller", caller(2))
}
2 changes: 1 addition & 1 deletion defaults_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func trimPkgFile(file string) string {

func caller(skip int) (caller string) {
pcs := make([]uintptr, 1)
if n := runtime.Callers(skip+3, pcs); n > 0 {
if n := runtime.Callers(skip+2, pcs); n > 0 {
frame, _ := runtime.CallersFrames(pcs).Next()
if frame.PC != 0 {
return fmtframe(frame)
Expand Down
43 changes: 43 additions & 0 deletions defaults_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package defaults

import (
"log/slog"
"os"
"strconv"
)

var isdebug bool

func init() {
isdebug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
}

func logwarn(msg string, kvs ...any) {
slog.Warn(msg, kvs...)
}

func logset() {
if isdebug {
slog.Info("set the default", "caller", caller(2))
}
}

func logswap() {
if isdebug {
slog.Info("swap the default", "caller", caller(2))
}
}
2 changes: 2 additions & 0 deletions defaults_value_atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (v *Value[T]) Set(new T) {
panic(err)
}
v.value.Store(valuer[T]{Value: new})
logset()
}

// Swap sets the value to new thread-safely and returns the old value.
Expand All @@ -68,6 +69,7 @@ func (v *Value[T]) Swap(new T) (old T) {
if value := v.value.Swap(valuer[T]{Value: new}); value != nil {
old = value.(valuer[T]).Value
}
logswap()
return
}

Expand Down
2 changes: 2 additions & 0 deletions defaults_value_notatomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (v *Value[T]) Set(new T) {
panic(err)
}
v.value = new
logset()
}

// Swap sets the value to new and returns the old value.
Expand All @@ -56,6 +57,7 @@ func (v *Value[T]) Swap(new T) (old T) {
}
old = v.value
v.value = new
logswap()
return
}

Expand Down

0 comments on commit b9f33ef

Please sign in to comment.