/
tracer.go
92 lines (78 loc) · 2.44 KB
/
tracer.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
package tracer
/*
The tracer module contain a simple application trace facility whose purpose is to capture
error and execution messages to add detail to the reporting output.
The basic naive implementation consists of:-
- an ApplicationTrace channel which receives a string message
- a goroutine which listens to the channel and outputs anything received to console - initial implementation
- AppInfo which add a trace message
- AppEntry - which effectively indents the messages for easy trace reading in the initial implementation
- AppExit - which reduces an indentation level on function return
So the idea is to produce and initial trace which some structure as a starting point for a richer execution flow capture
to feed reporting, so a user can follow what executed, and knows exactly where an error occurred and why.
*/
import (
"fmt"
"github.com/sirupsen/logrus"
)
// Tracer info capture interface - capturing testcase execution info - expects to be extended/improved over time
type Tracer interface {
AppMsg(objType, msg, serialisedObj string) // general application flow information
AppErr(objType, msg, serialisedObj string) // application errors
AppEntry(objType, msg string) // function entry
AppExit(objType, msg string) // function exit
}
var (
// ApplicationTrace channel
applicationTrace chan string
// Silent running
Silent = true
// Current text indent
indents = 0
)
// Model Package Initialisation
// Core tracing channel
func init() {
applicationTrace = make(chan string)
go appTraceLisener()
}
// simple trace listener to dump out to console
func appTraceLisener() {
for {
msg := <-applicationTrace
//fmt.Println(msg)
_ = msg
}
}
// AppMsg - generic trace fuction
func AppMsg(objtype, msg, objdump string) {
if Silent {
return
}
appmsg := fmt.Sprintf("%s[%s] %s", "" /*indent()*/, objtype, msg)
logrus.StandardLogger().Debugln(appmsg)
applicationTrace <- appmsg
}
// AppErr - generic trace errpr fuction
func AppErr(objtype, msg, objdump string) {
if Silent {
return
}
errmsg := fmt.Sprintf("%s[%s] %s", "" /*indent()*/, objtype, msg)
logrus.StandardLogger().Error(errmsg)
applicationTrace <- errmsg
}
// AppEntry - application level trace
func AppEntry(objtype, msg string) {
AppMsg(objtype, msg, "")
addIndent(1)
}
// AppExit - application level trace
func AppExit(objtype, msg string) {
addIndent(-1)
AppMsg(objtype, msg, "")
}
// addIndent -
func addIndent(i int) {
indents += i
}