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

@@ -8,6 +8,7 @@ import (
"strings"
"github.com/mattrtaylor/go-rtmidi"
"github.com/rs/zerolog/log"
)
// MIDIDriver represents how the protocol is defined
@@ -17,6 +18,7 @@ type MIDIDriver struct {
// NewMIDIDriver creates a new DMXUSB protocol
func NewMIDIDriver() *MIDIDriver {
log.Trace().Str("file", "MIDIDriver").Msg("MIDI driver created")
return &MIDIDriver{
peripherals: make(map[string]Peripheral),
}
@@ -24,7 +26,7 @@ func NewMIDIDriver() *MIDIDriver {
// Initialize initializes the MIDI driver
func (d *MIDIDriver) Initialize() error {
fmt.Println("MIDI driver initialized")
log.Trace().Str("file", "MIDIDriver").Msg("MIDI driver initialized")
return nil
}
@@ -36,10 +38,12 @@ func (d *MIDIDriver) GetName() string {
// GetPeripheral gets the peripheral that correspond to the specified ID
func (d *MIDIDriver) 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", "MIDIDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the MIDI driver")
return nil, false
}
log.Trace().Str("file", "MIDIDriver").Str("peripheralID", peripheralID).Msg("MIDI peripheral found in the driver")
return peripheral, true
}
@@ -67,28 +71,35 @@ func splitStringAndNumber(input string) (string, int, error) {
// Scan scans the interfaces compatible with the MIDI protocol
func (d *MIDIDriver) Scan(ctx context.Context) error {
midiPeripherals := make(map[string]Peripheral)
fmt.Println("Opening MIDI scanner port...")
log.Trace().Str("file", "MIDIDriver").Msg("opening MIDI scanner port...")
midiScanner, err := rtmidi.NewMIDIInDefault()
if err != nil {
return fmt.Errorf("Error when opening the MIDI scanner: %s", err)
log.Err(err).Str("file", "MIDIDriver").Msg("unable to open the MIDI scanner port...")
return fmt.Errorf("unable to open the MIDI scanner: %s", err)
}
defer midiScanner.Close()
fmt.Println("Scanning MIDI devices...")
midiScanner.SetCallback(func(m rtmidi.MIDIIn, b []byte, f float64) {
})
log.Trace().Str("file", "MIDIDriver").Msg("scanning MIDI peripherals...")
devicesCount, err := midiScanner.PortCount()
if err != nil {
return fmt.Errorf("Error when scanning MIDI devices: %s", err)
log.Err(err).Str("file", "MIDIDriver").Msg("unable to scan MIDI peripherals...")
return fmt.Errorf("unable to scan MIDI peripherals: %s", err)
}
for i := 0; i < devicesCount; i++ {
portName, err := midiScanner.PortName(i)
if err != nil {
log.Warn().Str("file", "MIDIPeripheral").Msg("found peripheral without a correct name, set it to unknown")
portName = "Unknown device 0"
}
// Separate data
name, location, err := splitStringAndNumber(portName)
if err != nil {
return fmt.Errorf("Unable to get information from the string: %s", err)
log.Err(err).Str("file", "MIDIDriver").Str("description", portName).Msg("invalid peripheral description")
return fmt.Errorf("invalid pripheral description: %s", err)
}
fmt.Printf("New MIDI device found: %s on %i\n", name, location)
log.Info().Str("file", "MIDIDriver").Str("name", name).Int("location", location).Msg("MIDI peripheral found")
// Add the peripheral to the temporary list
sn := strings.ToLower(strings.Replace(name, " ", "_", -1))
midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn)
@@ -97,7 +108,9 @@ func (d *MIDIDriver) Scan(ctx context.Context) error {
removedList, addedList := comparePeripherals(d.peripherals, midiPeripherals)
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
log.Info().Str("file", "MIDIDriver").Msg("MIDI remove list emitted to the front")
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
log.Info().Str("file", "MIDIDriver").Msg("MIDI add list emitted to the front")
// Store the new peripherals list
d.peripherals = midiPeripherals
return nil