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

@@ -7,6 +7,8 @@ import (
"time"
"unsafe"
"github.com/rs/zerolog/log"
"github.com/lxn/win"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/sys/windows"
@@ -37,6 +39,7 @@ type HardwareManager struct {
// NewHardwareManager creates a new HardwareManager
func NewHardwareManager() *HardwareManager {
log.Trace().Str("package", "hardware").Msg("Hardware instance created")
return &HardwareManager{
drivers: make(map[string]PeripheralDriver),
peripherals: make([]Peripheral, 0),
@@ -44,13 +47,17 @@ func NewHardwareManager() *HardwareManager {
}
}
// Start starts to finding new device events
// Start starts to find new peripheral events
func (h *HardwareManager) Start(ctx context.Context) error {
cb := windows.NewCallback(h.wndProc)
log.Trace().Str("file", "hardware").Msg("wndProc callback set")
inst := win.GetModuleHandle(nil)
cn, err := syscall.UTF16PtrFromString("DMXConnect device watcher")
log.Trace().Str("file", "hardware").Msg("got windows API instance")
cn, err := syscall.UTF16PtrFromString("DMXConnect peripheral watcher")
if err != nil {
log.Err(err).Str("file", "hardware").Msg("failed to convert window class name to UTF16")
return fmt.Errorf("failed to convert window class name to UTF16: %w", err)
}
wc := win.WNDCLASSEX{
@@ -58,12 +65,18 @@ func (h *HardwareManager) Start(ctx context.Context) error {
LpfnWndProc: cb,
LpszClassName: cn,
}
log.Trace().Str("file", "hardware").Msg("windows API class created")
wc.CbSize = uint32(unsafe.Sizeof(wc))
if win.RegisterClassEx(&wc) == 0 {
log.Err(syscall.GetLastError()).Str("file", "hardware").Msg("failed to register window class")
return fmt.Errorf("failed to register window class: %w", syscall.GetLastError())
}
log.Trace().Str("file", "hardware").Msg("window class registered")
wName, err := syscall.UTF16PtrFromString("usbevent.exe")
if err != nil {
log.Err(err).Str("file", "hardware").Msg("failed to convert window class name to UTF16")
return fmt.Errorf("failed to convert window name to UTF16: %w", err)
}
wdw := win.CreateWindowEx(
@@ -80,14 +93,18 @@ func (h *HardwareManager) Start(ctx context.Context) error {
wc.HInstance,
nil)
if wdw == 0 {
log.Err(syscall.GetLastError()).Str("file", "hardware").Msg("failed to create window")
return fmt.Errorf("failed to create window: %w", syscall.GetLastError())
}
log.Trace().Str("file", "hardware").Msg("window created successfully")
_ = win.ShowWindow(wdw, win.SW_HIDE)
win.UpdateWindow(wdw)
log.Trace().Str("file", "hardware").Msg("window shown and updated")
// To continuously get the devices events from Windows
go func() {
defer log.Debug().Str("file", "hardware").Msg("peripheral watcher goroutine exited")
for {
select {
case <-ctx.Done():
@@ -105,13 +122,17 @@ func (h *HardwareManager) Start(ctx context.Context) error {
// To handle the peripheral changed
go func() {
defer log.Debug().Str("file", "hardware").Msg("peripheral getter goroutine exited")
for {
select {
case <-ctx.Done():
return
case <-h.deviceChangedEvent:
fmt.Println("This is the list of devices")
h.Scan(ctx)
log.Debug().Str("file", "hardware").Msg("peripheral change event, triggering scan...")
err := h.Scan(ctx)
if err != nil {
log.Err(err).Str("file", "hardware").Msg("unable to scan peripherals")
}
}
}
}()
@@ -122,26 +143,29 @@ func (h *HardwareManager) Start(ctx context.Context) error {
func (h *HardwareManager) GetDriver(driverName string) (PeripheralDriver, error) {
driver, exists := h.drivers[driverName]
if !exists {
log.Error().Str("file", "hardware").Str("driverName", driverName).Msg("unable to get the driver")
return nil, fmt.Errorf("Unable to locate the '%s' driver", driverName)
}
log.Debug().Str("file", "hardware").Str("driverName", driverName).Msg("got driver")
return driver, nil
}
// RegisterDriver registers a new peripherals driver
func (h *HardwareManager) RegisterDriver(driver PeripheralDriver) {
h.drivers[driver.GetName()] = driver
fmt.Printf("Success registered the %s driver\n", driver.GetName())
log.Info().Str("file", "hardware").Str("driverName", driver.GetName()).Msg("driver registered")
}
// GetPeripheral gets the peripheral object from the parent driver
func (h *HardwareManager) GetPeripheral(driverName string, peripheralID string) (Peripheral, bool) {
// Get the driver
parentDriver := h.drivers[driverName]
parentDriver, found := h.drivers[driverName]
// If no driver found, return false
if parentDriver == nil {
fmt.Println("Unable to get the driver")
if !found {
log.Error().Str("file", "hardware").Str("driverName", driverName).Msg("unable to get the driver")
return nil, false
}
log.Trace().Str("file", "hardware").Str("driverName", parentDriver.GetName()).Msg("driver got")
// Contact the driver to get the device
return parentDriver.GetPeripheral(peripheralID)
}
@@ -149,14 +173,15 @@ func (h *HardwareManager) GetPeripheral(driverName string, peripheralID string)
// Scan scans all the peripherals for the registered finders
func (h *HardwareManager) Scan(ctx context.Context) error {
if len(h.drivers) == 0 {
return fmt.Errorf("No peripherals driver registered")
log.Warn().Str("file", "hardware").Msg("no driver registered")
return fmt.Errorf("no driver registered")
}
for _, driver := range h.drivers {
driverCopy := driver
go func() {
err := driverCopy.Scan(ctx)
if err != nil {
fmt.Printf("Unable to scan peripherals with the %s driver: %s\n", driverCopy.GetName(), err)
log.Err(err).Str("file", "hardware").Str("driverName", driverCopy.GetName()).Msg("unable to scan peripheral")
return
}
}()
@@ -170,9 +195,10 @@ func (h *HardwareManager) wndProc(hwnd windows.HWND, msg uint32, wParam, lParam
// Trigger the devices scan when the last DEVICE_CHANGE event is received
if debounceTimer != nil {
debounceTimer.Stop()
log.Trace().Str("file", "hardware").Msg("scan debounce timer stopped")
}
debounceTimer = time.AfterFunc(debounceDuration, func() {
fmt.Printf("Devices list has changed, refresh the devices list\n")
log.Debug().Str("file", "hardware").Msg("peripheral changed")
h.deviceChangedEvent <- struct{}{}
})
}
@@ -183,6 +209,7 @@ func (h *HardwareManager) wndProc(hwnd windows.HWND, msg uint32, wParam, lParam
func emitPeripheralsEvents(ctx context.Context, peripheralsList map[string]Peripheral, peripheralEvent PeripheralEvent) {
for _, peripheral := range peripheralsList {
runtime.EventsEmit(ctx, string(peripheralEvent), peripheral.GetInfo())
log.Trace().Str("file", "hardware").Str("event", string(peripheralEvent)).Msg("emit peripheral event")
}
}
@@ -195,9 +222,11 @@ func comparePeripherals(oldPeripherals map[string]Peripheral, newPeripherals map
for key, value := range oldPeripherals {
oldList[key] = value
}
log.Trace().Str("file", "hardware").Any("oldList", oldList).Msg("peripheral oldList comparison")
for key, value := range newPeripherals {
newList[key] = value
}
log.Trace().Str("file", "hardware").Any("newList", newList).Msg("peripheral newList comparison")
// Remove in these lists all the commons peripherals
for key := range newList {
if _, exists := oldList[key]; exists {
@@ -206,7 +235,7 @@ func comparePeripherals(oldPeripherals map[string]Peripheral, newPeripherals map
}
}
// Now the old list contains the removed peripherals, and the new list contains the added peripherals
fmt.Printf("%s\n", oldList)
fmt.Printf("%s\n", newList)
log.Trace().Str("file", "hardware").Any("oldList", oldList).Msg("peripheral oldList computed")
log.Trace().Str("file", "hardware").Any("newList", newList).Msg("peripheral newList computed")
return oldList, newList
}