~juju-qa/ubuntu/xenial/juju/2.0.2

« back to all changes in this revision

Viewing changes to src/github.com/vmware/govmomi/govc/flags/debug.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-09 22:31:29 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161209223129-h2wbknpc8ty29zwp
ImportĀ upstreamĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014-2016 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 flags
 
18
 
 
19
import (
 
20
        "flag"
 
21
        "fmt"
 
22
        "os"
 
23
        "path/filepath"
 
24
        "strings"
 
25
        "time"
 
26
 
 
27
        "golang.org/x/net/context"
 
28
 
 
29
        "github.com/vmware/govmomi/vim25/debug"
 
30
)
 
31
 
 
32
type DebugFlag struct {
 
33
        common
 
34
 
 
35
        enable bool
 
36
}
 
37
 
 
38
var debugFlagKey = flagKey("debug")
 
39
 
 
40
func NewDebugFlag(ctx context.Context) (*DebugFlag, context.Context) {
 
41
        if v := ctx.Value(debugFlagKey); v != nil {
 
42
                return v.(*DebugFlag), ctx
 
43
        }
 
44
 
 
45
        v := &DebugFlag{}
 
46
        ctx = context.WithValue(ctx, debugFlagKey, v)
 
47
        return v, ctx
 
48
}
 
49
 
 
50
func (flag *DebugFlag) Register(ctx context.Context, f *flag.FlagSet) {
 
51
        flag.RegisterOnce(func() {
 
52
                env := "GOVC_DEBUG"
 
53
                enable := false
 
54
                switch env := strings.ToLower(os.Getenv(env)); env {
 
55
                case "1", "true":
 
56
                        enable = true
 
57
                }
 
58
 
 
59
                usage := fmt.Sprintf("Store debug logs [%s]", env)
 
60
                f.BoolVar(&flag.enable, "debug", enable, usage)
 
61
        })
 
62
}
 
63
 
 
64
func (flag *DebugFlag) Process(ctx context.Context) error {
 
65
        if !flag.enable {
 
66
                return nil
 
67
        }
 
68
 
 
69
        return flag.ProcessOnce(func() error {
 
70
                // Base path for storing debug logs.
 
71
                r := os.Getenv("GOVC_DEBUG_PATH")
 
72
                if r == "" {
 
73
                        r = filepath.Join(os.Getenv("HOME"), ".govmomi")
 
74
                }
 
75
                r = filepath.Join(r, "debug")
 
76
 
 
77
                // Path for this particular run.
 
78
                run := os.Getenv("GOVC_DEBUG_PATH_RUN")
 
79
                if run == "" {
 
80
                        now := time.Now().Format("2006-01-02T15-04-05.999999999")
 
81
                        r = filepath.Join(r, now)
 
82
                } else {
 
83
                        // reuse the same path
 
84
                        r = filepath.Join(r, run)
 
85
                        _ = os.RemoveAll(r)
 
86
                }
 
87
 
 
88
                err := os.MkdirAll(r, 0700)
 
89
                if err != nil {
 
90
                        return err
 
91
                }
 
92
 
 
93
                p := debug.FileProvider{
 
94
                        Path: r,
 
95
                }
 
96
 
 
97
                debug.SetProvider(&p)
 
98
                return nil
 
99
        })
 
100
}