github.com/vmware/govmomi@v0.37.2/vim25/debug/debug.go (about)

     1  /*
     2  Copyright (c) 2014 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package debug
    18  
    19  import (
    20  	"io"
    21  	"regexp"
    22  )
    23  
    24  // Provider specified the interface types must implement to be used as a
    25  // debugging sink. Having multiple such sink implementations allows it to be
    26  // changed externally (for example when running tests).
    27  type Provider interface {
    28  	NewFile(s string) io.WriteCloser
    29  	Flush()
    30  }
    31  
    32  // ReadCloser is a struct that satisfies the io.ReadCloser interface
    33  type ReadCloser struct {
    34  	io.Reader
    35  	io.Closer
    36  }
    37  
    38  // NewTeeReader wraps io.TeeReader and patches through the Close() function.
    39  func NewTeeReader(rc io.ReadCloser, w io.Writer) io.ReadCloser {
    40  	return ReadCloser{
    41  		Reader: io.TeeReader(rc, w),
    42  		Closer: rc,
    43  	}
    44  }
    45  
    46  var currentProvider Provider = nil
    47  var scrubPassword = regexp.MustCompile(`<password>(.*)</password>`)
    48  
    49  func SetProvider(p Provider) {
    50  	if currentProvider != nil {
    51  		currentProvider.Flush()
    52  	}
    53  	currentProvider = p
    54  }
    55  
    56  // Enabled returns whether debugging is enabled or not.
    57  func Enabled() bool {
    58  	return currentProvider != nil
    59  }
    60  
    61  // NewFile dispatches to the current provider's NewFile function.
    62  func NewFile(s string) io.WriteCloser {
    63  	return currentProvider.NewFile(s)
    64  }
    65  
    66  // Flush dispatches to the current provider's Flush function.
    67  func Flush() {
    68  	currentProvider.Flush()
    69  }
    70  
    71  func Scrub(in []byte) []byte {
    72  	return scrubPassword.ReplaceAll(in, []byte(`<password>********</password>`))
    73  }