github.com/vmware/govmomi@v0.51.0/vim25/debug/debug.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package debug 6 7 import ( 8 "io" 9 "regexp" 10 ) 11 12 // Provider specified the interface types must implement to be used as a 13 // debugging sink. Having multiple such sink implementations allows it to be 14 // changed externally (for example when running tests). 15 type Provider interface { 16 NewFile(s string) io.WriteCloser 17 Flush() 18 } 19 20 // ReadCloser is a struct that satisfies the io.ReadCloser interface 21 type ReadCloser struct { 22 io.Reader 23 io.Closer 24 } 25 26 // NewTeeReader wraps io.TeeReader and patches through the Close() function. 27 func NewTeeReader(rc io.ReadCloser, w io.Writer) io.ReadCloser { 28 return ReadCloser{ 29 Reader: io.TeeReader(rc, w), 30 Closer: rc, 31 } 32 } 33 34 var currentProvider Provider = nil 35 var scrubPassword = regexp.MustCompile(`<password>(.*)</password>`) 36 37 func SetProvider(p Provider) { 38 if currentProvider != nil { 39 currentProvider.Flush() 40 } 41 currentProvider = p 42 } 43 44 // Enabled returns whether debugging is enabled or not. 45 func Enabled() bool { 46 return currentProvider != nil 47 } 48 49 // NewFile dispatches to the current provider's NewFile function. 50 func NewFile(s string) io.WriteCloser { 51 return currentProvider.NewFile(s) 52 } 53 54 // Flush dispatches to the current provider's Flush function. 55 func Flush() { 56 currentProvider.Flush() 57 } 58 59 func Scrub(in []byte) []byte { 60 return scrubPassword.ReplaceAll(in, []byte(`<password>********</password>`)) 61 }