manager.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package status
  2. import (
  3. "log/slog"
  4. "sync"
  5. )
  6. // Manager handles status message management
  7. type Manager struct {
  8. service Service
  9. mu sync.RWMutex
  10. }
  11. // Global instance of the status manager
  12. var globalManager *Manager
  13. // InitManager initializes the global status manager with the provided service
  14. func InitManager(service Service) {
  15. globalManager = &Manager{
  16. service: service,
  17. }
  18. // Subscribe to status events for any global handling if needed
  19. // go func() {
  20. // ctx := context.Background()
  21. // _ = service.Subscribe(ctx)
  22. // }()
  23. slog.Debug("Status manager initialized")
  24. }
  25. // GetService returns the status service from the global manager
  26. func GetService() Service {
  27. if globalManager == nil {
  28. slog.Warn("Status manager not initialized, initializing with default service")
  29. InitManager(NewService())
  30. }
  31. globalManager.mu.RLock()
  32. defer globalManager.mu.RUnlock()
  33. return globalManager.service
  34. }
  35. // Info publishes an info level status message using the global manager
  36. func Info(message string) {
  37. GetService().Info(message)
  38. }
  39. // Warn publishes a warning level status message using the global manager
  40. func Warn(message string) {
  41. GetService().Warn(message)
  42. }
  43. // Error publishes an error level status message using the global manager
  44. func Error(message string) {
  45. GetService().Error(message)
  46. }
  47. // Debug publishes a debug level status message using the global manager
  48. func Debug(message string) {
  49. GetService().Debug(message)
  50. }