forked from waigani/xxx
/
xxx.go
152 lines (131 loc) · 3.02 KB
/
xxx.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
// xxx is a debuging library for golang.
package xxx
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"runtime"
"runtime/debug"
"gopkg.in/mgo.v2"
"github.com/davecgh/go-spew/spew"
"github.com/juju/juju/mongo"
)
// Print prints v along with filename and line number of call.
func Print(v interface{}, message ...string) {
called(2, fmt.Sprintf("%s \n%#v\n", m(message), v))
}
// Printf prints format along with filename and line number of call.
func Printf(format string, vars ...interface{}) {
called(2, fmt.Sprintf(format, vars...))
}
// Dump prints all information on x.
func Dump(x ...interface{}) {
if off {
return
}
called(2, "spew")
var a []interface{}
a = append(a, x...)
spew.Dump(a)
}
// Stack prints call stack to this point.
func Stack() {
if off {
return
}
debug.PrintStack()
}
// DumpColl dumps the contents of a mongo database collection.
func DumpColl(db *mgo.Database, collName string) {
if off {
return
}
coll, closer := mongo.CollectionFromName(db, collName)
defer closer()
var results []interface{}
err := coll.Find(nil).All(&results)
if err != nil {
panic(err)
}
called(2, collName)
if len(results) == 0 {
fmt.Println("no results found")
return
}
for i, r := range results {
output, err := json.MarshalIndent(r, "", " ")
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("Doc %d (%T): %s\n", i, r, output)
fmt.Println("")
}
fmt.Println("")
}
// CaptureStdOutAndErr captures all std out/err until the returned func is
// called. That func returns any captured output as a string. This is useful
// for capturing remote output (e.g. on a server) and writing it to a file or
// piping it to a log etc.
func CaptureStdOutAndErr() func() string {
old := os.Stdout // keep backup of the real stdout
oldErr := os.Stderr
r, w, _ := os.Pipe()
os.Stdout = w
rErr, wErr, _ := os.Pipe()
os.Stderr = wErr
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
outErrC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, rErr)
outErrC <- buf.String()
}()
return func() string {
w.Close()
wErr.Close()
os.Stdout = old // restoring the real stdout
os.Stderr = oldErr // restoring the real stdout
return <-outC + <-outErrC
}
}
func Called(message ...string) {
called(2, m(message))
}
func CalledX(x int, message ...string) {
c := 2 + x
called(c, m(message))
}
var off bool
// Off stops any output being printed from this package.
func Off() {
off = true
}
// On starts any output being printed from this package. When you have code
// peppered with debugging output, use On and Off to print just the debug
// outputs you are interested in.
func On() {
off = false
}
func called(scope int, message string) {
if off {
return
}
if _, file, line, ok := runtime.Caller(scope); ok {
fmt.Printf("%s:%d: %s \n", file, line, message)
}
}
func m(ms []string) string {
var m string
if ms != nil {
m = ms[0]
}
return m
}