1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00
node-feature-discovery/pkg/utils/dump.go
Markus Lehtonen bf670de68d pkg/utils: migrate KlogDump to structured logging
Drop the KlogDump helper in favor of klog.InfoS. However, that patch
introduces a new DelayedDumper() helper to avoid processing
(marshalling) of object unless really evaluated by the logging function.
2023-05-31 14:43:08 +03:00

48 lines
1.2 KiB
Go

/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"sigs.k8s.io/yaml"
)
type dumper struct {
obj interface{}
}
// String implements the fmt.Stringer interface
func (d *dumper) String() string {
return Dump(d.obj)
}
// DelayedDumper delays the dumping of an object. Useful in logging to delay
// the processing (JSON marshalling) until (or if) the object is actually
// evaluated.
func DelayedDumper(obj interface{}) fmt.Stringer {
return &dumper{obj: obj}
}
// Dump dumps an object into YAML textual format
func Dump(obj interface{}) string {
out, err := yaml.Marshal(obj)
if err != nil {
return fmt.Sprintf("<!!! FAILED TO MARSHAL %T (%v) !!!>\n", obj, err)
}
return string(out)
}