-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnotice.go
47 lines (37 loc) · 846 Bytes
/
notice.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
package libpq
/*
#include <stdlib.h>
#include <libpq-fe.h>
extern void go_callback_log(char * message);
static void notice(void * arg, char * message)
{
go_callback_log(message);
}
static void setNotice(PGconn *conn)
{
// we need that cast to suppress issue with const qualifier
// that is not supported by cgo
PQsetNoticeProcessor(conn, (PQnoticeProcessor)notice,NULL);
}
*/
import "C"
func redirectOutput(db *C.PGconn) {
if logger != nil {
C.setNotice(db)
}
}
type Logger func(message string)
var logger Logger
// SetLogger redirects notifications for all new connections
// to a callback
func SetLogger(l Logger) {
if l == nil {
panic("can't set empty logger")
}
logger = l
}
//export go_callback_log
func go_callback_log(message *C.char) {
// this is possible only if logger is not null
logger(C.GoString(message))
}