forked from SAP/cgo-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
91 lines (78 loc) · 2.57 KB
/
bridge.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
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
// SPDX-FileCopyrightText: 2022 SAP SE
// SPDX-FileCopyrightText: 2023 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package ase
// #include "ctlib.h"
import "C"
import (
"fmt"
"os"
"sync"
)
var (
cbTarget = os.Stderr
cbRW = &sync.RWMutex{}
)
// SetCallbackTarget sets the os.File target the callback functions for
// client and server messages will write to.
func SetCallbackTarget(target *os.File) {
cbRW.Lock()
defer cbRW.Unlock()
cbTarget = target
}
// srvMsg is a callback function which will be called from C when the
// server sends a message. The message is then passed to the
// GlobalServerMessageBroker.
// Don't change the following line. It is the directive for cgo to make
// the function available from C.
//export srvMsg
func srvMsg(msg *C.CS_SERVERMSG) C.CS_RETCODE {
GlobalServerMessageBroker.recvServerMessage(msg)
return C.CS_SUCCEED
}
func logSrvMsg(msg Message) {
cbRW.RLock()
defer cbRW.RUnlock()
srvMsg, ok := msg.(ServerMessage)
if !ok {
return
}
fmt.Fprintln(cbTarget, "Server message:")
fmt.Fprintf(cbTarget, "\tmsgnumber: %d\n", srvMsg.MsgNumber)
fmt.Fprintf(cbTarget, "\tstate: %d\n", srvMsg.State)
fmt.Fprintf(cbTarget, "\tseverity: %d\n", srvMsg.Severity)
fmt.Fprintf(cbTarget, "\ttext: %s\n", srvMsg.Text)
fmt.Fprintf(cbTarget, "\tserver: %s\n", srvMsg.Server)
fmt.Fprintf(cbTarget, "\tproc: %s\n", srvMsg.Proc)
fmt.Fprintf(cbTarget, "\tline: %d\n", srvMsg.Line)
fmt.Fprintf(cbTarget, "\tsqlstate: %s\n", srvMsg.SQLState)
}
// cltMsg is a callback function which will be called from C when the
// client sends a message. The message is then passed to the
// GlobalClientMessageBroker.
// Don't change the following line. It is the directive for cgo to make
// the function available from C.
//export cltMsg
func cltMsg(msg *C.CS_CLIENTMSG) C.CS_RETCODE {
GlobalClientMessageBroker.recvClientMessage(msg)
return C.CS_SUCCEED
}
func logCltMsg(msg Message) {
cbRW.RLock()
defer cbRW.RUnlock()
cltMsg, ok := msg.(ClientMessage)
if !ok {
return
}
fmt.Fprintln(cbTarget, "Client message:")
fmt.Fprintf(cbTarget, "\tseverity: %d\n", cltMsg.Severity)
fmt.Fprintf(cbTarget, "\tmsgnumber: %d\n", cltMsg.MsgNumber)
fmt.Fprintf(cbTarget, "\tmsgstring: %s\n", cltMsg.Text)
fmt.Fprintf(cbTarget, "\tosnumber: %d\n", cltMsg.OSNumber)
fmt.Fprintf(cbTarget, "\tosstring: %s\n", cltMsg.OSString)
fmt.Fprintf(cbTarget, "\tstatus: %d\n", cltMsg.Status)
fmt.Fprintf(cbTarget, "\tsqlstate: %s\n", cltMsg.SQLState)
}