-
Notifications
You must be signed in to change notification settings - Fork 61
/
trace_manager.go
225 lines (196 loc) · 4.83 KB
/
trace_manager.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
package firebirdsql
import (
"fmt"
"regexp"
"strconv"
)
type TraceManager struct {
connBuilder func() (*ServiceManager, error)
}
const (
SessionStopped = iota
SessionRunning
SessionPaused
)
type TraceSession struct {
connBuilder func() (*ServiceManager, error)
conn *ServiceManager
id int32
state int
}
func NewTraceManager(addr string, user string, password string, options ServiceManagerOptions) (*TraceManager, error) {
connBuilder := func() (*ServiceManager, error) {
return NewServiceManager(addr, user, password, options)
}
return &TraceManager{
connBuilder: connBuilder,
}, nil
}
func (t *TraceManager) Start(config string) (*TraceSession, error) {
return t.StartWithName("", config)
}
func (t *TraceManager) StartWithName(name string, config string) (*TraceSession, error) {
var (
conn *ServiceManager
id int64
err error
)
if conn, err = t.connBuilder(); err != nil {
return nil, err
}
var res string
var spb = NewXPBWriterFromTag(isc_action_svc_trace_start)
if len(name) > 0 {
spb.PutString(isc_spb_trc_name, name)
}
spb.PutString(isc_spb_trc_cfg, config)
if err = conn.ServiceStart(spb.Bytes()); err != nil {
return nil, err
}
if res, _, err = conn.GetString(); err != nil {
return nil, err
}
re := regexp.MustCompile(`Trace session ID (\d+) started`)
match := re.FindStringSubmatch(res)
if len(match) == 0 {
_ = conn.Close()
return nil, fmt.Errorf("unable to start trace session: %s", res)
}
if id, err = strconv.ParseInt(match[1], 10, 32); err != nil {
return nil, err
}
return &TraceSession{
connBuilder: t.connBuilder,
conn: conn,
id: int32(id),
state: SessionRunning,
}, nil
}
func (t *TraceManager) List() (string, error) {
var (
err error
line, res string
end = false
conn *ServiceManager
)
if conn, err = t.connBuilder(); err != nil {
return "", nil
}
defer func(conn *ServiceManager) {
_ = conn.Close()
}(conn)
if err = conn.ServiceStart([]byte{isc_action_svc_trace_list}); err != nil {
return "", err
}
for {
if line, end, err = conn.GetString(); err != nil {
return "", nil
}
if end {
return res, nil
}
res += line + "\n"
}
}
func (ts *TraceSession) Close() (err error) {
if ts.state != SessionStopped {
if err = ts.Stop(); err != nil {
return err
}
}
if err = ts.conn.Close(); err != nil {
return err
}
ts.conn = nil
return nil
}
func (ts *TraceSession) Stop() (err error) {
if ts.state == SessionStopped {
return fmt.Errorf("session already stopped")
}
var auxConn *ServiceManager
if auxConn, err = ts.connBuilder(); err != nil {
return
}
defer func(auxConn *ServiceManager) {
_ = auxConn.Close()
}(auxConn)
var res string
spb := NewXPBWriterFromTag(isc_action_svc_trace_stop)
spb.PutInt32(isc_spb_trc_id, ts.id)
if err = auxConn.ServiceStart(spb.Bytes()); err != nil {
return err
}
if res, _, err = auxConn.GetString(); err != nil {
return err
}
re := regexp.MustCompile(`Trace session ID (\d+) stopped`)
match := re.FindStringSubmatch(res)
if len(match) == 0 {
return fmt.Errorf("unable to stop trace session: %s", res)
}
ts.state = SessionStopped
return nil
}
func (ts *TraceSession) Pause() (err error) {
if ts.state != SessionRunning {
return fmt.Errorf("session not running")
}
var auxConn *ServiceManager
if auxConn, err = ts.connBuilder(); err != nil {
return
}
defer func(auxConn *ServiceManager) {
_ = auxConn.Close()
}(auxConn)
var res string
spb := NewXPBWriterFromTag(isc_action_svc_trace_suspend)
spb.PutInt32(isc_spb_trc_id, ts.id)
if err = auxConn.ServiceStart(spb.Bytes()); err != nil {
return err
}
if res, _, err = auxConn.GetString(); err != nil {
return err
}
re := regexp.MustCompile(`Trace session ID (\d+) paused`)
match := re.FindStringSubmatch(res)
if len(match) == 0 {
return fmt.Errorf("unable to pause trace session: %s", res)
}
ts.state = SessionPaused
return nil
}
func (ts *TraceSession) Resume() (err error) {
if ts.state == SessionPaused {
return fmt.Errorf("session not paused")
}
var auxConn *ServiceManager
if auxConn, err = ts.connBuilder(); err != nil {
return
}
defer func(auxConn *ServiceManager) {
_ = auxConn.Close()
}(auxConn)
var res string
spb := NewXPBWriterFromTag(isc_action_svc_trace_resume)
spb.PutInt32(isc_spb_trc_id, ts.id)
if err = auxConn.ServiceStart(spb.Bytes()); err != nil {
return err
}
if res, _, err = auxConn.GetString(); err != nil {
return err
}
re := regexp.MustCompile(`Trace session ID (\d+) resumed`)
match := re.FindStringSubmatch(res)
if len(match) == 0 {
return fmt.Errorf("unable to resume trace session: %s", res)
}
ts.state = SessionRunning
return nil
}
func (ts *TraceSession) Wait() (err error) {
return ts.conn.Wait()
}
func (ts *TraceSession) WaitStrings(result chan string) (err error) {
return ts.conn.WaitStrings(result)
}