-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrace.go
More file actions
197 lines (174 loc) · 5.12 KB
/
trace.go
File metadata and controls
197 lines (174 loc) · 5.12 KB
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
// Copyright 2014 Dizitart. All rights reserved.
// Use of this source code is governed by a Apache License V2
// that can be found in the LICENSE file.
//
// Author - Anindya Chatterjee (anindya@dizitart.com)
package trace
import (
"fmt"
"time"
"runtime"
"os"
"path/filepath"
"runtime/debug"
)
type MessageFormat byte
type OutputChannel byte
const (
// Switches off other information in trace message
PRINT_NONE MessageFormat = 0
// Switch to print time
PRINT_TIME MessageFormat = 1
// Switch to print function name
PRINT_PROC MessageFormat = 2
// Switch to print source file name
PRINT_FILE MessageFormat = 4
// Switch yo print current line number
PRINT_LINE MessageFormat = 8
// Switches on all formatting
PRINT_ALL MessageFormat = PRINT_TIME | PRINT_PROC | PRINT_FILE | PRINT_LINE
// Switches off printing
OUT_NONE OutputChannel = 0
// Switch to print to Stdout
OUT_STD OutputChannel = 1
// Switch to print to File
OUT_FILE OutputChannel = 2
// Switches on both File & Stdout printing
OUT_ALL OutputChannel = OUT_STD | OUT_FILE
)
// Use these configuration variables to set tracing parameters
var (
// Flag to enable or disable the Tracing
ENABLE_TRACE bool = false
// Trace log file path
TRACE_FILE_PATH string
// Trace message flag
traceMessageFlag MessageFormat
// Output flag
traceOutFlag OutputChannel
)
// Writes information about current source file, line number and input variable
func Write(variable interface {}) {
if ENABLE_TRACE {
msg := createTraceMessage(variable)
trace(msg, false)
}
}
// Writes information about current source file, line number and input variable after
// formatting it according to a format specifier
func Writef(format string, variables ...interface{}) {
if ENABLE_TRACE {
msg := createTraceMessagef(format, variables...)
trace(msg, false)
}
}
// Checks for a condition; if the condition is false, writes
// a trace message that shows the call stack.
func Assert (condition bool) {
if ENABLE_TRACE {
if !condition {
msg := createTraceMessage("Assertion Failed!")
trace(msg, true)
}
}
}
// Checks for a condition; if the condition is false, writes a
// specified message after formatting it according to a format specifier
// that shows the call stack.
func Assertf (condition bool, format string, variables ...interface{}) {
if ENABLE_TRACE {
if !condition {
msg := createTraceMessagef(format, variables...)
trace(msg, true)
}
}
}
// Sets output channels (Stdout, File)
func SetOut(out OutputChannel) {
traceOutFlag = out
}
// Sets trace message format
func SetMessageFormat(messageFormat MessageFormat) {
traceMessageFlag = messageFormat
}
// Prints the message and stack trace if set to true
func trace(msg string, printStack bool) {
if traceOutFlag & OUT_STD == OUT_STD {
fmt.Println(msg)
if printStack {
debug.PrintStack()
}
}
if traceOutFlag & OUT_FILE == OUT_FILE {
if TRACE_FILE_PATH == "" {
return
} else {
file, _ := os.OpenFile(TRACE_FILE_PATH, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
defer file.Close()
file.WriteString(msg + "\n")
if printStack {
file.WriteString(string(debug.Stack()))
}
}
}
}
// Creates the trace message
func createTraceMessage(variable interface {}) string {
pc, path, line, ok := runtime.Caller(2)
if ok {
fun := runtime.FuncForPC(pc)
name := fun.Name()
sourceFile := filepath.Base(path)
var msg string = "TRACE"
if traceMessageFlag & PRINT_NONE == PRINT_NONE {
msg = msg + ""
}
if traceMessageFlag & PRINT_TIME == PRINT_TIME {
msg = fmt.Sprintf(msg + " [%v]", time.Now())
}
if traceMessageFlag & PRINT_PROC == PRINT_PROC {
msg = fmt.Sprintf(msg + " <%s>", name)
}
if traceMessageFlag & PRINT_FILE == PRINT_FILE && traceMessageFlag & PRINT_LINE == PRINT_LINE {
msg = fmt.Sprintf(msg + " (%s:%d)", sourceFile, line)
} else if traceMessageFlag & PRINT_FILE == PRINT_FILE {
msg = fmt.Sprintf(msg + " (%s)", sourceFile)
} else if traceMessageFlag & PRINT_LINE == PRINT_LINE {
msg = fmt.Sprintf(msg + " (%d)", line)
}
msg = fmt.Sprintf(msg + " - %v", variable)
return msg
}
return ""
}
// Creates the trace message after
// formatting it according to a format specifier
func createTraceMessagef(format string, variables ...interface{}) string {
pc, path, line, ok := runtime.Caller(2)
if ok {
fun := runtime.FuncForPC(pc)
name := fun.Name()
sourceFile := filepath.Base(path)
umsg := fmt.Sprintf(format, variables...)
var msg string = "TRACE"
if traceMessageFlag & PRINT_NONE == PRINT_NONE {
msg = msg + ""
}
if traceMessageFlag & PRINT_TIME == PRINT_TIME {
msg = fmt.Sprintf(msg + " [%v]", time.Now())
}
if traceMessageFlag & PRINT_PROC == PRINT_PROC {
msg = fmt.Sprintf(msg + " <%s>", name)
}
if traceMessageFlag & PRINT_FILE == PRINT_FILE && traceMessageFlag & PRINT_LINE == PRINT_LINE {
msg = fmt.Sprintf(msg + " (%s:%d)", sourceFile, line)
} else if traceMessageFlag & PRINT_FILE == PRINT_FILE {
msg = fmt.Sprintf(msg + " (%s)", sourceFile)
} else if traceMessageFlag & PRINT_LINE == PRINT_LINE {
msg = fmt.Sprintf(msg + " (%d)", line)
}
msg = fmt.Sprintf(msg + " - %v", umsg)
return msg
}
return ""
}