/
misc.go
122 lines (100 loc) · 2.54 KB
/
misc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package goutil
import (
"fmt"
"go/ast"
"go/printer"
"go/token"
"io"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"github.com/jmigpin/editor/v2/util/osutil"
)
func GoPath() []string {
// TODO: use go/build defaultgopath if it becomes public
a := []string{}
gopath := os.Getenv("GOPATH")
if gopath != "" {
a = append(a, filepath.SplitList(gopath)...)
} else {
// from go/build/build.go:274
a = append(a, filepath.Join(osutil.HomeEnvVar(), "go"))
}
return a
}
func JoinPathLists(w ...string) string {
return strings.Join(w, string(os.PathListSeparator))
}
//----------
//func ExtractSrcDir(filename string) (string, string) {
// srcDir := ""
// for _, d := range build.Default.SrcDirs() {
// d += string(filepath.Separator)
// if strings.HasPrefix(filename, d) {
// srcDir = filename[:len(d)]
// filename = filename[len(d):]
// return srcDir, filename
// }
// }
// return srcDir, filename
//}
//----------
func AstFileFilename(astFile *ast.File, fset *token.FileSet) (string, error) {
if astFile == nil {
panic("!")
}
tfile := fset.File(astFile.Package)
if tfile == nil {
return "", fmt.Errorf("not found")
}
return tfile.Name(), nil
}
//----------
func PrintAstFile(w io.Writer, fset *token.FileSet, astFile *ast.File) error {
// TODO: without tabwidth set, it won't output the source correctly
// print with source positions from original file
// Fail: has struct fields without spaces "field int"->"fieldint"
//cfg := &printer.Config{Mode: printer.SourcePos | printer.TabIndent}
// Fail: has stmts split with comments in the middle
//cfg := &printer.Config{Mode: printer.SourcePos | printer.TabIndent | printer.UseSpaces}
cfg := &printer.Config{Mode: printer.SourcePos, Tabwidth: 4}
return cfg.Fprint(w, fset, astFile)
}
//----------
func Printfc(skip int, f string, args ...interface{}) {
pc, _, _, ok := runtime.Caller(1 + skip)
details := runtime.FuncForPC(pc)
if ok && details != nil {
u := details.Name()
i := strings.Index(u, "(")
if i > 0 {
u = u[i:]
}
fmt.Printf(u+": "+f, args...)
return
}
fmt.Printf(f, args...)
}
//----------
// go test -cpuprofile cpu.prof -memprofile mem.prof
// go tool pprof cpu.prof
// view with a browser:
// go tool pprof -http=:8000 cpu.prof
var profFile *os.File
func StartCPUProfile() error {
filename := "cpu.prof"
f, err := os.Create(filename)
if err != nil {
return err
}
profFile = f
log.Printf("profile cpu: %v\n", filename)
return pprof.StartCPUProfile(f)
}
func StopCPUProfile() error {
pprof.StopCPUProfile()
return profFile.Close()
}