improved log system

This commit is contained in:
2024-12-29 13:09:46 +01:00
parent c3c604d871
commit b69097e2a4
13 changed files with 247 additions and 111 deletions

View File

@@ -5,6 +5,8 @@ import (
"fmt"
"math/rand"
"strings"
"github.com/rs/zerolog/log"
)
// OS2LDriver represents how the protocol is defined
@@ -14,6 +16,7 @@ type OS2LDriver struct {
// NewOS2LDriver creates a new OS2L driver
func NewOS2LDriver() *OS2LDriver {
log.Trace().Str("file", "OS2LDriver").Msg("OS2L driver created")
return &OS2LDriver{
peripherals: make(map[string]Peripheral),
}
@@ -21,7 +24,7 @@ func NewOS2LDriver() *OS2LDriver {
// Initialize initializes the MIDI driver
func (d *OS2LDriver) Initialize() error {
fmt.Println("OS2L driver initialized")
log.Trace().Str("file", "OS2LDriver").Msg("OS2L driver initialized")
return nil
}
@@ -29,14 +32,17 @@ func (d *OS2LDriver) Initialize() error {
func (d *OS2LDriver) CreatePeripheral(ctx context.Context) (Peripheral, error) {
// Create a random serial number for this peripheral
randomSerialNumber := strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32)))
log.Trace().Str("file", "OS2LDriver").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created")
peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber)
d.peripherals[randomSerialNumber] = peripheral
log.Info().Str("file", "OS2LDriver").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created and registered")
return peripheral, nil
}
// RemovePeripheral removes an OS2L dev
func (d *OS2LDriver) RemovePeripheral(serialNumber string) error {
delete(d.peripherals, serialNumber)
log.Info().Str("file", "OS2LDriver").Str("serialNumber", serialNumber).Msg("OS2L peripheral removed")
return nil
}
@@ -48,10 +54,12 @@ func (d *OS2LDriver) GetName() string {
// GetPeripheral gets the peripheral that correspond to the specified ID
func (d *OS2LDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
// Return the specified peripheral
peripheral := d.peripherals[peripheralID]
if peripheral == nil {
peripheral, found := d.peripherals[peripheralID]
if !found {
log.Error().Str("file", "OS2LDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the OS2L driver")
return nil, false
}
log.Trace().Str("file", "OS2LDriver").Str("peripheralID", peripheralID).Msg("OS2L peripheral found in the driver")
return peripheral, true
}