-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also add g_debug logging to listmodel.c for #154
- Loading branch information
1 parent
cd18cea
commit 20a52bd
Showing
5 changed files
with
163 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,61 @@ | ||
package gdebug | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"log/slog" | ||
"os" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
var debug = strings.Split(os.Getenv("GOTK4_DEBUG"), ",") | ||
|
||
func HasKey(key string) bool { | ||
for _, k := range debug { | ||
if k == key { | ||
return true | ||
} | ||
} | ||
return false | ||
return slices.Contains(debug, key) || slices.Contains(debug, "all") | ||
} | ||
|
||
func NewDebugLogger(key string) *log.Logger { | ||
func NewDebugLogger(key string) *slog.Logger { | ||
if !HasKey(key) { | ||
return log.New(io.Discard, "", 0) | ||
return slog.New(noopLogHandler{}) | ||
} | ||
return mustDebugLogger(key) | ||
} | ||
|
||
func NewDebugLoggerNullable(key string) *log.Logger { | ||
func NewDebugLoggerNullable(key string) *slog.Logger { | ||
if !HasKey(key) { | ||
return nil | ||
} | ||
return mustDebugLogger(key) | ||
} | ||
|
||
func mustDebugLogger(name string) *log.Logger { | ||
func mustDebugLogger(name string) *slog.Logger { | ||
if HasKey("to-console") { | ||
return log.Default() | ||
return slog.With("gotk4_module", name) | ||
} | ||
|
||
f, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("gotk4-%s-%d-*", name, os.Getpid())) | ||
if err != nil { | ||
log.Panicln("cannot create temp", name, "file:", err) | ||
} | ||
|
||
log.Println("gotk4: intern: enabled debug file at", f.Name()) | ||
return log.New(f, "", log.LstdFlags) | ||
slog.Info( | ||
"gotk4: intern: enabled debug file", | ||
"file", f.Name()) | ||
|
||
return slog.New( | ||
slog.NewTextHandler(f, &slog.HandlerOptions{ | ||
Level: slog.LevelDebug, | ||
}), | ||
) | ||
} | ||
|
||
type noopLogHandler struct{} | ||
|
||
var _ slog.Handler = noopLogHandler{} | ||
|
||
func (noopLogHandler) Enabled(context.Context, slog.Level) bool { return false } | ||
func (noopLogHandler) Handle(context.Context, slog.Record) error { return nil } | ||
func (noopLogHandler) WithAttrs([]slog.Attr) slog.Handler { return noopLogHandler{} } | ||
func (noopLogHandler) WithGroup(string) slog.Handler { return noopLogHandler{} } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.