go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/huectl/pkg/hue/verbose.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package hue
     9  
    10  import (
    11  	"context"
    12  	"log"
    13  	"os"
    14  )
    15  
    16  type verboseKey struct{}
    17  
    18  // WithVerbose sets the verbose flag on the context.
    19  func WithVerbose(ctx context.Context, verbose bool) context.Context {
    20  	return context.WithValue(ctx, verboseKey{}, verbose)
    21  }
    22  
    23  func getVerbose(ctx context.Context) bool {
    24  	if value := ctx.Value(verboseKey{}); value != nil {
    25  		typedValue, _ := value.(bool)
    26  		return typedValue
    27  	}
    28  	return false
    29  }
    30  
    31  var logger = log.New(os.Stdout, "huectl|", log.Ltime|log.Lmicroseconds)
    32  
    33  func verbosef(ctx context.Context, format string, args ...interface{}) {
    34  	if !getVerbose(ctx) {
    35  		return
    36  	}
    37  	logger.Printf(format, args...)
    38  }