Skip to content

Commit

Permalink
debug option independent from log level
Browse files Browse the repository at this point in the history
  • Loading branch information
laaqxdze1k committed Aug 31, 2023
1 parent f115b22 commit d424579
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cmdupload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ assetLoop:
}
err := app.DeleteServerAssets(ctx, ids)
if err != nil {
return fmt.Errorf("Can't delete server's assets: %w", err)
return fmt.Errorf("an't delete server's assets: %w", err)
}
}

if len(app.deleteLocalList) > 0 {
err = app.DeleteLocalAssets()
}
app.logger.OK("%d media scanned, %d uploaded.", app.mediaCount, app.mediaUploaded)
return err
}

func (app *UpCmd) handleAsset(ctx context.Context, a *assets.LocalAssetFile) error {
app.logger.DebugObject("handleAsset: LocalAssetFile=", a)
showCount := true
defer func() {
a.Close()
Expand Down Expand Up @@ -199,6 +199,7 @@ func (app *UpCmd) handleAsset(ctx context.Context, a *assets.LocalAssetFile) err
return nil
}
}
app.logger.DebugObject("handleAsset: LocalAssetFile=", a)

advice, err := app.AssetIndex.ShouldUpload(a)
if err != nil {
Expand Down
18 changes: 15 additions & 3 deletions immich/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ type Logger struct {
displayLevel Level
noColors bool
colorStrings map[Level]string
debug bool
}

func NewLogger(DisplayLevel Level, noColors bool) *Logger {
func NewLogger(DisplayLevel Level, noColors bool, debug bool) *Logger {
l := Logger{
displayLevel: DisplayLevel,
noColors: noColors,
colorStrings: map[Level]string{},
debug: debug,
}
if !noColors {
l.colorStrings = colorLevel
Expand All @@ -88,7 +90,7 @@ type DebugObject interface {
}

func (l *Logger) DebugObject(name string, v any) {
if Debug > l.displayLevel {
if !l.debug {
return
}
if d, ok := v.(DebugObject); ok {
Expand All @@ -102,7 +104,17 @@ func (l *Logger) DebugObject(name string, v any) {
l.Error("can't display object %s: %s", name, err)
return
}
l.Message(Debug, "%s:\n%s", name, b.String())
if l.needCR {
fmt.Println()
l.needCR = false
}
l.needSpace = false
fmt.Print(l.colorStrings[Debug])
fmt.Printf("%s:\n%s", name, b.String())
if !l.noColors {
fmt.Print(chalk.ResetColor)
}
fmt.Println()
}
func (l *Logger) Info(f string, v ...any) {
l.Message(Info, f, v...)
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
func main() {
fmt.Printf("immich-go %s, commit %s, built at %s\n", version, commit, date)
var err error
var log = logger.NewLogger(logger.OK, true)
var log = logger.NewLogger(logger.OK, true, false)
// Create a context with cancel function to gracefully handle Ctrl+C events
ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -60,6 +60,7 @@ type Application struct {

NoLogColors bool // Disable log colors
LogLevel string
Debug bool
}

func Run(ctx context.Context, log *logger.Logger) (*logger.Logger, error) {
Expand All @@ -74,8 +75,9 @@ func Run(ctx context.Context, log *logger.Logger) (*logger.Logger, error) {
flag.StringVar(&app.Key, "key", "", "API Key")
flag.StringVar(&app.DeviceUUID, "device-uuid", deviceID, "Set a device UUID")
flag.BoolVar(&app.NoLogColors, "no-colors-log", false, "Disable colors on logs")
flag.StringVar(&app.LogLevel, "log-level", "ok", "Log level (Error|Warning|OK|Info|Debug), default OK")
flag.StringVar(&app.LogLevel, "log-level", "ok", "Log level (Error|Warning|OK|Info), default OK")
flag.BoolVar(&app.ApiTrace, "api-trace", false, "enable api call traces")
flag.BoolVar(&app.Debug, "debug", false, "enable debug messages")
flag.Parse()
if len(app.EndPoint) == 0 {
err = errors.Join(err, errors.New("missing -server"))
Expand All @@ -93,7 +95,7 @@ func Run(ctx context.Context, log *logger.Logger) (*logger.Logger, error) {
err = errors.Join(err, errors.New("missing command"))
}

app.Logger = logger.NewLogger(logLevel, app.NoLogColors)
app.Logger = logger.NewLogger(logLevel, app.NoLogColors, app.Debug)

if err != nil {
return app.Logger, err
Expand Down

0 comments on commit d424579

Please sign in to comment.