Skip to content

Commit ec6e4fe

Browse files
author
Gijs Peskens
authored
Add API for setting loglevel & loghandler (#20)
1 parent 930958c commit ec6e4fe

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/haivision/srtgo
22

33
go 1.12
44

5-
require golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c
5+
require (
6+
github.com/mattn/go-pointer v0.0.1
7+
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c
8+
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
2+
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
13
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c h1:38q6VNPWR010vN82/SB121GujZNIfAUb4YttE2rhGuc=
24
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

logging.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package srtgo
2+
3+
/*
4+
#cgo LDFLAGS: -lsrt
5+
#include <srt/srt.h>
6+
extern void srtLogCB(void* opaque, int level, const char* file, int line, const char* area, const char* message);
7+
*/
8+
import "C"
9+
10+
import (
11+
"sync"
12+
"unsafe"
13+
14+
gopointer "github.com/mattn/go-pointer"
15+
)
16+
17+
type LogCallBackFunc func(level SrtLogLevel, file string, line int, area, message string)
18+
19+
type SrtLogLevel int
20+
21+
const (
22+
// SrtLogLevelEmerg = int(C.LOG_EMERG)
23+
// SrtLogLevelAlert = int(C.LOG_ALERT)
24+
SrtLogLevelCrit SrtLogLevel = SrtLogLevel(C.LOG_CRIT)
25+
SrtLogLevelErr SrtLogLevel = SrtLogLevel(C.LOG_ERR)
26+
SrtLogLevelWarning SrtLogLevel = SrtLogLevel(C.LOG_WARNING)
27+
SrtLogLevelNotice SrtLogLevel = SrtLogLevel(C.LOG_NOTICE)
28+
SrtLogLevelInfo SrtLogLevel = SrtLogLevel(C.LOG_INFO)
29+
SrtLogLevelDebug SrtLogLevel = SrtLogLevel(C.LOG_DEBUG)
30+
SrtLogLevelTrace SrtLogLevel = SrtLogLevel(8)
31+
)
32+
33+
var (
34+
logCBPtr unsafe.Pointer = nil
35+
logCBPtrLock sync.Mutex
36+
)
37+
38+
//export srtLogCBWrapper
39+
func srtLogCBWrapper(arg unsafe.Pointer, level C.int, file *C.char, line C.int, area, message *C.char) {
40+
userCB := gopointer.Restore(arg).(LogCallBackFunc)
41+
go userCB(SrtLogLevel(level), C.GoString(file), int(line), C.GoString(area), C.GoString(message))
42+
}
43+
44+
func SrtSetLogLevel(level SrtLogLevel) {
45+
C.srt_setloglevel(C.int(level))
46+
}
47+
48+
func SrtSetLogHandler(cb LogCallBackFunc) {
49+
ptr := gopointer.Save(cb)
50+
C.srt_setloghandler(ptr, (*C.SRT_LOG_HANDLER_FN)(C.srtLogCB))
51+
logCBPtrLock.Lock()
52+
storeLogCBPtr(ptr)
53+
}
54+
55+
func SrtUnsetLogHandler() {
56+
C.srt_setloghandler(nil, nil)
57+
storeLogCBPtr(nil)
58+
}
59+
60+
func storeLogCBPtr(ptr unsafe.Pointer) {
61+
logCBPtrLock.Lock()
62+
defer logCBPtrLock.Unlock()
63+
if logCBPtr != nil {
64+
gopointer.Unref(logCBPtr)
65+
}
66+
logCBPtr = nil
67+
}

logging_c.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package srtgo
2+
3+
/*
4+
#cgo LDFLAGS: -lsrt
5+
#include <srt/srt.h>
6+
7+
extern void srtLogCBWrapper (void* opaque, int level, char* file, int line, char* area, char* message);
8+
9+
void srtLogCB(void* opaque, int level, const char* file, int line, const char* area, const char* message)
10+
{
11+
srtLogCBWrapper(opaque, level, (char*)file, line, (char*)area,(char*) message);
12+
}
13+
*/
14+
import "C"

0 commit comments

Comments
 (0)