github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/internal/logger/debug.go (about)

     1  // Copyright 2024 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logger
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/google/yamlfmt/internal/collections"
    21  )
    22  
    23  type DebugCode int
    24  
    25  const (
    26  	DebugCodeAny DebugCode = iota
    27  	DebugCodeConfig
    28  	DebugCodePaths
    29  )
    30  
    31  var (
    32  	supportedDebugCodes = map[string][]DebugCode{
    33  		"config": {DebugCodeConfig},
    34  		"paths":  {DebugCodePaths},
    35  		"all":    {DebugCodeConfig, DebugCodePaths},
    36  	}
    37  	activeDebugCodes = collections.Set[DebugCode]{}
    38  )
    39  
    40  func ActivateDebugCode(code string) {
    41  	if debugCodes, ok := supportedDebugCodes[code]; ok {
    42  		activeDebugCodes.Add(debugCodes...)
    43  	}
    44  }
    45  
    46  // Debug prints a message if the given debug code is active.
    47  func Debug(code DebugCode, msg string, args ...any) {
    48  	if activeDebugCodes.Contains(code) {
    49  		fmt.Printf("[DEBUG]: %s\n", fmt.Sprintf(msg, args...))
    50  	}
    51  }