Skip to content

Commit

Permalink
better logging API
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Dec 8, 2023
1 parent 4511b1b commit 749819e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 210 deletions.
127 changes: 0 additions & 127 deletions cyrw/bus_pico.go

This file was deleted.

4 changes: 2 additions & 2 deletions cyrw/bus_pico_pio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
var _busOrder = binary.LittleEndian

type spibus struct {
cs OutputPin
cs outputPin
spi piolib.SPI3w
}

func DefaultNew() *Device {
func NewPicoWDevice() *Device {
// Raspberry Pi Pico W pin definitions for the CY43439.
const (
// IRQ = machine.GPIO24 // AKA WL_HOST_WAKE
Expand Down
74 changes: 12 additions & 62 deletions cyrw/debug.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cyrw

import (
"context"
"encoding/hex"
"fmt"
"reflect"
"unsafe"

"log/slog"
)
Expand All @@ -16,7 +14,7 @@ const (
// A higher currentLevel means less logs (less verbose).
defaultLevel slog.Level = levelTrace + 1
levelTrace slog.Level = slog.LevelDebug - 1
deviceLevel slog.Level = slog.LevelError - 1
deviceLevel slog.Level = slog.LevelDebug - 1
// dblogattrs decides whether to print key-value log attributes.
dblogattrs = true
// print out raw bus transactions.
Expand Down Expand Up @@ -44,43 +42,20 @@ func (d *Device) trace(msg string, attrs ...slog.Attr) {
}

func (d *Device) logattrs(level slog.Level, msg string, attrs ...slog.Attr) {
canPrintDeviceLog := enableDeviceLog && level == deviceLevel
if level < d.level && !canPrintDeviceLog {
return
if d.logger != nil {
d.logger.LogAttrs(context.Background(), level, msg, attrs...)
}

print(_levelStr(level))
print(" ")
print(msg)
if dblogattrs {
for _, a := range attrs {
print(" ")
print(a.Key)
print("=")
if a.Value.Kind() == slog.KindAny {
fmt.Printf("%+v", a.Value.Any())
} else {
print(a.Value.String())
}
}
}
println()
}
func _levelStr(level slog.Level) string {
var levelStr string
switch level {
case deviceLevel:
levelStr = "CY43"
case levelTrace:
levelStr = "TRACE"
default:
levelStr = level.String()
}
return levelStr

// SetLogger sets the logger for the device. If nil logging is disabled.
func (d *Device) SetLogger(l *slog.Logger) {
d.mu.Lock()
defer d.mu.Unlock()
d.logger = l
}

func (d *Device) log_init() error {
if !enableDeviceLog {
if d.logger == nil || !d.logger.Handler().Enabled(context.Background(), deviceLevel) {
return nil
}
d.trace("log_init")
Expand Down Expand Up @@ -112,7 +87,7 @@ func (d *Device) log_init() error {
// log_read reads the CY43439's internal logs and prints them to the structured logger
// under the CY43 level.
func (d *Device) log_read() error {
if !enableDeviceLog {
if d.logger == nil || !d.logger.Handler().Enabled(context.Background(), deviceLevel) {
return nil
}
d.trace("log_read")
Expand Down Expand Up @@ -152,31 +127,6 @@ func (d *Device) log_read() error {
return nil
}

func printLowLevelTx(cmd uint32, data []uint32) {
if !printLowestLevelBusTransactions {
return
}
var buf [8]byte
ptr := unsafe.Pointer(&buf[0])
strhdr := reflect.StringHeader{
Data: uintptr(ptr),
Len: uintptr(len(buf)),
}
h := *(*string)(unsafe.Pointer(&strhdr))
const hextable = "0123456789abcdef"

hex.Encode(buf[:], u32PtrTo4U8(&cmd)[:])
print("tx: ")
print(h)

for _, d := range data {
hex.Encode(buf[:], u32PtrTo4U8(&d)[:])
print(" ")
print(h)
}
println()
}

func hex32(u uint32) string {
return hex.EncodeToString([]byte{byte(u >> 24), byte(u >> 16), byte(u >> 8), byte(u)})
}
17 changes: 8 additions & 9 deletions cyrw/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ import (
"github.com/soypat/cyw43439/whd"
)

type OutputPin func(bool)
type outputPin func(bool)

func DefaultConfig() Config {
func DefaultWifiConfig() Config {
return Config{
Firmware: wifiFW2,
CLM: clmFW,
Level: defaultLevel,
}
}

// type OutputPin func(bool)
type Device struct {
mu sync.Mutex
pwr OutputPin
pwr outputPin
lastStatusGet time.Time
spi spibus
log logstate
Expand All @@ -44,10 +43,10 @@ type Device struct {
auxCDCHeader whd.CDCHeader
auxBDCHeader whd.BDCHeader
rcvEth func([]byte) error
level slog.Level
logger *slog.Logger
}

func New(pwr, cs OutputPin, spi spibus) *Device {
func New(pwr, cs outputPin, spi spibus) *Device {
d := &Device{
pwr: pwr,
spi: spi,
Expand All @@ -59,14 +58,14 @@ func New(pwr, cs OutputPin, spi spibus) *Device {
type Config struct {
Firmware string
CLM string
Level slog.Level // Logging level.
Logger *slog.Logger
}

func (d *Device) Init(cfg Config) (err error) {
d.lock()
defer d.unlock()
d.level = cfg.Level
d.info("Init:start", slog.String("slog.Level", _levelStr(d.level)))
d.logger = cfg.Logger
d.info("Init:start")

// Reference: https://github.com/embassy-rs/embassy/blob/6babd5752e439b234151104d8d20bae32e41d714/cyw43/src/runner.rs#L76
err = d.initBus()
Expand Down
4 changes: 2 additions & 2 deletions examples/dhcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func main() {
time.Sleep(2 * time.Second)
println("starting program")
slog.Debug("starting program")
dev := cyrw.DefaultNew()
dev := cyrw.NewPicoWDevice()
// cfg.Level = slog.LevelInfo // Logging level.
err := dev.Init(cyrw.DefaultConfig())
err := dev.Init(cyrw.DefaultWifiConfig())
if err != nil {
panic(err)
}
Expand Down
18 changes: 10 additions & 8 deletions examples/tcpserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ func main() {
println("panic:", a)
}
}()
logger := slog.New(slog.NewTextHandler(machine.Serial, &slog.HandlerOptions{
Level: slog.LevelDebug, // Go lower (Debug-1) to see more verbosity on wifi device.
}))

time.Sleep(2 * time.Second)
println("starting program")
slog.Debug("starting program")
dev := cyrw.DefaultNew()
cfg := cyrw.DefaultConfig()
cfg.Level = slog.LevelError // Logging level.
logger.Debug("starting program")
dev := cyrw.NewPicoWDevice()
cfg := cyrw.DefaultWifiConfig()
// cfg.Logger = logger // Uncomment to see in depth info on wifi device functioning.
err := dev.Init(cfg)
if err != nil {
panic(err)
Expand All @@ -57,10 +61,8 @@ func main() {
lastRx = time.Now()
return nil
},
MTU: MTU,
Logger: slog.New(slog.NewTextHandler(machine.Serial, &slog.HandlerOptions{
Level: slog.LevelDebug,
})),
MTU: MTU,
Logger: logger,
})

dev.RecvEthHandle(stack.RecvEth)
Expand Down

0 comments on commit 749819e

Please sign in to comment.