Skip to content

Commit

Permalink
Add a couple debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Feb 4, 2025
1 parent 9f1edd7 commit b9be9f7
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions internal/collector/sysinfo/hardware/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func New(args ...Options) Collector {

// Collect aggregates the data from all the other hardware collect functions.
func (s Collector) Collect() (info Info, err error) {
s.opts.log.Debug("collecting hardware info")

info.Product, err = s.collectProduct()
if err != nil {
s.opts.log.Warn("failed to collect Product info", "error", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/collector/sysinfo/hardware/hardware_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestCollectLinux(t *testing.T) {
require.NoError(t, err, "setup: failed to remove file %s: ", f)
}

l := testutils.NewMockHandler()
l := testutils.NewMockHandler(slog.LevelDebug)

options := []hardware.Options{
hardware.WithRoot(root),
Expand Down
2 changes: 2 additions & 0 deletions internal/collector/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func New(args ...Options) Collector {
// Collect gathers system information and returns it.
// Will only return an error if both hardware and software collection fail.
func (s Collector) Collect() (Info, error) {
s.log.Debug("collecting sysinfo")

hwInfo, hwErr := s.hw.Collect()
swInfo, swErr := s.sw.Collect()

Expand Down
2 changes: 1 addition & 1 deletion internal/collector/sysinfo/sysinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCollect(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

l := testutils.NewMockHandler()
l := testutils.NewMockHandler(slog.LevelDebug)

s := sysinfo.New(
sysinfo.WithHardwareCollector(makeFakeCollector(tc.hw, tc.hwErr)),
Expand Down
1 change: 1 addition & 0 deletions internal/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func AtomicWrite(path string, data []byte) error {

// ReadFileLogError returns the data in the file path, trimming whitespace, or "" on error.
func ReadFileLogError(path string, log *slog.Logger) string {
log.Debug("reading file", "file", path)
f, err := os.ReadFile(path)
if err != nil {
log.Warn("failed to read file", "file", path, "error", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/fileutils/fileutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestReadFileLogError(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

l := testutils.NewMockHandler()
l := testutils.NewMockHandler(slog.LevelDebug)

got := fileutils.ReadFileLogError(tc.file, slog.New(&l))

Expand Down
7 changes: 5 additions & 2 deletions internal/testutils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import (

// MockHandler tracks calls to logging functions and implements slog.Handler.
type MockHandler struct {
IgnoreBelow slog.Level
EnabledCalls []slog.Level
HandleCalls []slog.Record
WithAttrsCalls [][]slog.Attr
WithGroupCalls []string
}

// NewMockHandler returns a new MockHandler.
func NewMockHandler() MockHandler {
// levels <= ignoreBelow will not call handle.
func NewMockHandler(ignoreBelow slog.Level) MockHandler {
return MockHandler{
IgnoreBelow: ignoreBelow,
EnabledCalls: make([]slog.Level, 0),
HandleCalls: make([]slog.Record, 0),
WithAttrsCalls: make([][]slog.Attr, 0),
Expand Down Expand Up @@ -58,7 +61,7 @@ func (h MockHandler) OutputLogs(t *testing.T) {
// Enabled implements Handler.Enabled.
func (h *MockHandler) Enabled(ctx context.Context, level slog.Level) bool {
h.EnabledCalls = append(h.EnabledCalls, level)
return true
return level > h.IgnoreBelow
}

// Handle implements Handler.Handle.
Expand Down

0 comments on commit b9be9f7

Please sign in to comment.