-
Notifications
You must be signed in to change notification settings - Fork 15
/
trace.go
302 lines (248 loc) · 6.06 KB
/
trace.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"text/template"
)
var (
importName = "__log"
importPath = `"github.com/jbardin/gotrace/log"`
importStmt = `
import __log "github.com/jbardin/gotrace/log"
`
setup = `
var _ = __log.Setup("stderr", "%s", %d)
`
tmpl = `
__traceID := __log.ID()
__log.L.Printf("[%d] {{.fname}}(%s){{if .position}} [{{.position}}]{{ end }}\n", __traceID, __log.Format({{.args}}))
{{if .timing}}__start := __log.Now(){{end}}
{{if .return}}defer func() {
{{if .timing}}since := "in " + __log.Since(__start).String(){{else}}since := ""{{end}}
__log.L.Printf("[%d] {{.fname}}{{if .position}} [{{.position}}]{{ end }} returned %s\n", __traceID, since)
}(){{ end }}
`
ErrAlreadyImported = fmt.Errorf("%s already imported", importPath)
)
var (
fset *token.FileSet
funcTemplate *template.Template
showReturn bool
exportedOnly bool
prefix string
showPackage bool
writeFiles bool
filterFlag string
excludeFlag string
formatLength int
timing bool
filter *regexp.Regexp
exclude *regexp.Regexp
)
// convert function parameters to a list of names
func paramNames(params *ast.FieldList) []string {
var p []string
for _, f := range params.List {
for _, n := range f.Names {
// we can't use _ as a name, so ignore it
if n.Name != "_" {
p = append(p, n.Name)
}
}
}
return p
}
func debugCall(fName string, pos token.Pos, args ...string) []byte {
vals := make(map[string]string)
if len(args) > 0 {
vals["args"] = strings.Join(args, ", ")
} else {
vals["args"] = ""
}
if timing {
vals["timing"] = "true"
}
vals["fname"] = fName
if pos.IsValid() {
vals["position"] = fset.Position(pos).String()
}
if showReturn {
vals["return"] = "true"
}
var b bytes.Buffer
err := funcTemplate.Execute(&b, vals)
if err != nil {
log.Fatal(err)
}
return b.Bytes()
}
type edit struct {
pos int
val []byte
}
type editList struct {
edits []edit
packageName string
}
func (e *editList) Add(pos int, val []byte) {
e.edits = append(e.edits, edit{pos: pos, val: val})
}
func (e *editList) inspect(node ast.Node) bool {
if node == nil {
return false
}
var pos token.Pos
var funcType *ast.FuncType
var body *ast.BlockStmt
var funcName string
switch n := node.(type) {
case *ast.FuncDecl:
body = n.Body
if body == nil {
return true
}
funcType = n.Type
funcName = n.Name.Name
// prepend our receiver type
if n.Recv != nil && len(n.Recv.List) > 0 {
switch t := n.Recv.List[0].Type.(type) {
case *ast.StarExpr:
funcName = t.X.(*ast.Ident).Name + "." + funcName
case *ast.Ident:
funcName = t.Name + "." + funcName
}
}
case *ast.FuncLit:
body = n.Body
funcType = n.Type
funcName = "func"
pos = n.Pos()
default:
return true
}
if exportedOnly && !ast.IsExported(funcName) {
return true
}
if showPackage {
funcName = e.packageName + "." + funcName
}
if !filter.MatchString(funcName) {
return true
}
if exclude != nil && exclude.MatchString(funcName) {
return true
}
e.Add(int(body.Lbrace), debugCall(funcName, pos, paramNames(funcType.Params)...))
return true
}
func annotateFile(file string) {
orig, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
src, err := annotate(file, orig)
if err != nil {
log.Printf("%s: skipping %s", err, file)
return
}
if !writeFiles {
fmt.Println(string(src))
return
}
err = ioutil.WriteFile(file, src, 0)
if err != nil {
log.Fatal(err)
}
}
func annotate(filename string, orig []byte) ([]byte, error) {
// we need to make sure the source is formmated to insert the new code in
// the expected place
orig, err := format.Source(orig)
if err != nil {
return orig, err
}
fset = token.NewFileSet()
f, err := parser.ParseFile(fset, filename, orig, parser.ParseComments)
if err != nil {
return nil, err
}
for _, imp := range f.Imports {
if imp.Name != nil && imp.Name.Name == importName {
return nil, ErrAlreadyImported
}
if imp.Path.Value == importPath {
return nil, ErrAlreadyImported
}
}
edits := editList{packageName: f.Name.Name}
// insert our import directly after the package line
edits.Add(int(f.Name.End()), []byte(importStmt))
ast.Inspect(f, edits.inspect)
var buf bytes.Buffer
if err := format.Node(&buf, fset, f); err != nil {
return nil, fmt.Errorf("format.Node: %s", err)
}
data := buf.Bytes()
var pos int
var out []byte
for _, e := range edits.edits {
out = append(out, data[pos:e.pos]...)
out = append(out, []byte(e.val)...)
pos = e.pos
}
out = append(out, data[pos:]...)
// it's easier to append the setup code at the end
out = append(out, []byte(setup)...)
src, err := format.Source(out)
if err != nil {
return out, fmt.Errorf("format.Source: %s", err)
}
return src, nil
}
func init() {
funcTemplate = template.Must(template.New("debug").Parse(tmpl))
}
func main() {
flag.BoolVar(&showReturn, "returns", false, "show function return")
flag.BoolVar(&exportedOnly, "exported", false, "only annotate exported functions")
flag.StringVar(&prefix, "prefix", "", "log prefix")
flag.BoolVar(&showPackage, "package", false, "show package name prefix on function calls")
flag.BoolVar(&writeFiles, "w", false, "re-write files in place")
flag.StringVar(&filterFlag, "filter", ".", "only annotate functions matching the regular expression")
flag.StringVar(&excludeFlag, "exclude", "", "exclude any matching functions, takes precedence over filter")
flag.IntVar(&formatLength, "formatLength", 1024, "limit the formatted length of each argumnet to 'size'")
flag.BoolVar(&timing, "timing", false, "print function durations. Implies -returns")
flag.Parse()
if flag.NArg() < 1 {
flag.PrintDefaults()
os.Exit(1)
}
setup = fmt.Sprintf(setup, prefix, formatLength)
var err error
filter, err = regexp.Compile(filterFlag)
if err != nil {
log.Fatal(err)
}
if excludeFlag != "" {
exclude, err = regexp.Compile(excludeFlag)
if err != nil {
log.Fatal(err)
}
}
if timing {
showReturn = true
}
for _, file := range flag.Args() {
annotateFile(file)
}
}