2026-01-30 10:55:30 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/host-uk/core/pkg/framework"
|
2026-01-30 22:02:40 +00:00
|
|
|
"github.com/host-uk/core/pkg/log"
|
2026-01-30 10:55:30 +00:00
|
|
|
)
|
|
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// LogLevel aliases for backwards compatibility.
|
|
|
|
|
type LogLevel = log.Level
|
2026-01-30 10:55:30 +00:00
|
|
|
|
|
|
|
|
const (
|
2026-01-30 22:02:40 +00:00
|
|
|
LogLevelQuiet = log.LevelQuiet
|
|
|
|
|
LogLevelError = log.LevelError
|
|
|
|
|
LogLevelWarn = log.LevelWarn
|
|
|
|
|
LogLevelInfo = log.LevelInfo
|
|
|
|
|
LogLevelDebug = log.LevelDebug
|
2026-01-30 10:55:30 +00:00
|
|
|
)
|
|
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// LogService wraps log.Service with CLI styling.
|
2026-01-30 10:55:30 +00:00
|
|
|
type LogService struct {
|
2026-01-30 22:02:40 +00:00
|
|
|
*log.Service
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogOptions configures the log service.
|
2026-01-30 22:02:40 +00:00
|
|
|
type LogOptions = log.Options
|
2026-01-30 10:55:30 +00:00
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// NewLogService creates a log service factory with CLI styling.
|
2026-01-30 10:55:30 +00:00
|
|
|
func NewLogService(opts LogOptions) func(*framework.Core) (any, error) {
|
|
|
|
|
return func(c *framework.Core) (any, error) {
|
2026-01-30 22:02:40 +00:00
|
|
|
// Create the underlying service
|
|
|
|
|
factory := log.NewService(opts)
|
|
|
|
|
svc, err := factory(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
logSvc := svc.(*log.Service)
|
2026-01-30 10:55:30 +00:00
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// Apply CLI styles
|
|
|
|
|
logSvc.StyleTimestamp = func(s string) string { return DimStyle.Render(s) }
|
|
|
|
|
logSvc.StyleDebug = func(s string) string { return DimStyle.Render(s) }
|
|
|
|
|
logSvc.StyleInfo = func(s string) string { return InfoStyle.Render(s) }
|
|
|
|
|
logSvc.StyleWarn = func(s string) string { return WarningStyle.Render(s) }
|
|
|
|
|
logSvc.StyleError = func(s string) string { return ErrorStyle.Render(s) }
|
2026-01-30 10:55:30 +00:00
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
return &LogService{Service: logSvc}, nil
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Package-level convenience ---
|
|
|
|
|
|
|
|
|
|
// Log returns the CLI's log service, or nil if not available.
|
|
|
|
|
func Log() *LogService {
|
|
|
|
|
if instance == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
svc, err := framework.ServiceFor[*LogService](instance.core, "log")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return svc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogDebug logs a debug message if log service is available.
|
|
|
|
|
func LogDebug(msg string) {
|
|
|
|
|
if l := Log(); l != nil {
|
|
|
|
|
l.Debug(msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogInfo logs an info message if log service is available.
|
|
|
|
|
func LogInfo(msg string) {
|
|
|
|
|
if l := Log(); l != nil {
|
2026-01-30 22:02:40 +00:00
|
|
|
l.Info(msg)
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogWarn logs a warning message if log service is available.
|
|
|
|
|
func LogWarn(msg string) {
|
|
|
|
|
if l := Log(); l != nil {
|
2026-01-30 22:02:40 +00:00
|
|
|
l.Warn(msg)
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogError logs an error message if log service is available.
|
|
|
|
|
func LogError(msg string) {
|
|
|
|
|
if l := Log(); l != nil {
|
2026-01-30 22:02:40 +00:00
|
|
|
l.Error(msg)
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
}
|