2
Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved.
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
8
http://www.apache.org/licenses/LICENSE-2.0
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.
27
"golang.org/x/net/context"
29
"github.com/vmware/govmomi/vim25/debug"
32
type DebugFlag struct {
38
var debugFlagKey = flagKey("debug")
40
func NewDebugFlag(ctx context.Context) (*DebugFlag, context.Context) {
41
if v := ctx.Value(debugFlagKey); v != nil {
42
return v.(*DebugFlag), ctx
46
ctx = context.WithValue(ctx, debugFlagKey, v)
50
func (flag *DebugFlag) Register(ctx context.Context, f *flag.FlagSet) {
51
flag.RegisterOnce(func() {
54
switch env := strings.ToLower(os.Getenv(env)); env {
59
usage := fmt.Sprintf("Store debug logs [%s]", env)
60
f.BoolVar(&flag.enable, "debug", enable, usage)
64
func (flag *DebugFlag) Process(ctx context.Context) error {
69
return flag.ProcessOnce(func() error {
70
// Base path for storing debug logs.
71
r := os.Getenv("GOVC_DEBUG_PATH")
73
r = filepath.Join(os.Getenv("HOME"), ".govmomi")
75
r = filepath.Join(r, "debug")
77
// Path for this particular run.
78
run := os.Getenv("GOVC_DEBUG_PATH_RUN")
80
now := time.Now().Format("2006-01-02T15-04-05.999999999")
81
r = filepath.Join(r, now)
83
// reuse the same path
84
r = filepath.Join(r, run)
88
err := os.MkdirAll(r, 0700)
93
p := debug.FileProvider{