Skip to content

Commit 11c001b

Browse files
authored
set log function (#3)
* gstreamer set log function * missing parameters * wrong object type * spacing * transfer * nil check object * add lock * unset -> reset * prevent data race
1 parent 8fc9f59 commit 11c001b

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ _bin/
2222
_rust/
2323
*.DS_Store*
2424
*.supp
25-
zzgenerated*
25+
zzgenerated*
26+
.idea/

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
22
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
3-
github.com/tinyzimmer/go-glib v0.0.24 h1:ktZZC22/9t88kGRgNEFV/SESgIWhGHE+q7Z7Qj++luw=
4-
github.com/tinyzimmer/go-glib v0.0.24/go.mod h1:ltV0gO6xNFzZhsIRbFXv8RTq9NGoNT2dmAER4YmZfaM=
3+
github.com/tinyzimmer/go-glib v0.0.25 h1:2GpumtkxA0wpXhCXP6D3ksb5pGMfo9WbhgLvEw8njK4=
4+
github.com/tinyzimmer/go-glib v0.0.25/go.mod h1:ltV0gO6xNFzZhsIRbFXv8RTq9NGoNT2dmAER4YmZfaM=

gst/cgo_exports.go

+31
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,34 @@ func goPluginInit(plugin *C.GstPlugin, userData C.gpointer) C.gboolean {
243243
func goGlobalPluginInit(plugin *C.GstPlugin) C.gboolean {
244244
return gboolean(globalPluginInit(wrapPlugin(&glib.Object{GObject: glib.ToGObject(unsafe.Pointer(plugin))})))
245245
}
246+
247+
//export goLogFunction
248+
func goLogFunction(
249+
_ *C.GstDebugCategory,
250+
level C.GstDebugLevel,
251+
file *C.gchar,
252+
function *C.gchar,
253+
line C.gint,
254+
object *C.GObject,
255+
message *C.GstDebugMessage,
256+
_ C.gpointer,
257+
) {
258+
logFnMu.RLock()
259+
f := customLogFunction
260+
logFnMu.RUnlock()
261+
262+
if f != nil {
263+
var obj *glib.Object
264+
if object != nil {
265+
obj = glib.TransferNone(unsafe.Pointer(object))
266+
}
267+
f(
268+
DebugLevel(level),
269+
C.GoString(file),
270+
C.GoString(function),
271+
int(line),
272+
obj,
273+
C.GoString(C.gst_debug_message_get(message)),
274+
)
275+
}
276+
}

gst/gst_debug.go

+50
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ package gst
33
/*
44
#include "gst.go.h"
55
6+
extern void goLogFunction(GstDebugCategory * category,
7+
GstDebugLevel level,
8+
const gchar * file,
9+
const gchar * function,
10+
gint line,
11+
GObject * object,
12+
GstDebugMessage * message,
13+
gpointer user_data) G_GNUC_NO_INSTRUMENT;
14+
615
void cgoDebugLog (GstDebugCategory * category,
716
GstDebugLevel level,
817
const gchar * file,
@@ -14,12 +23,27 @@ void cgoDebugLog (GstDebugCategory * category,
1423
gst_debug_log(category, level, file, function, line, object, msg);
1524
}
1625
26+
void cgoSetLogFunction()
27+
{
28+
gst_debug_remove_log_function(gst_debug_log_default);
29+
gst_debug_add_log_function(goLogFunction, NULL, NULL);
30+
}
31+
32+
void cgoResetLogFunction()
33+
{
34+
gst_debug_remove_log_function(goLogFunction);
35+
gst_debug_add_log_function(gst_debug_log_default, NULL, NULL);
36+
}
37+
1738
*/
1839
import "C"
1940
import (
2041
"path"
2142
"runtime"
43+
"sync"
2244
"unsafe"
45+
46+
"github.com/tinyzimmer/go-glib/glib"
2347
)
2448

2549
// DebugColorFlags are terminal style flags you can use when creating your debugging
@@ -164,3 +188,29 @@ func (d *DebugCategory) LogTrace(message string, obj ...*Object) {
164188
func (d *DebugCategory) LogMemDump(message string, obj ...*Object) {
165189
d.logDepth(LevelMemDump, message, 2, getLogObj(obj...))
166190
}
191+
192+
type LogFunction func(
193+
level DebugLevel,
194+
file string,
195+
function string,
196+
line int,
197+
object *glib.Object,
198+
message string,
199+
)
200+
201+
var (
202+
logFnMu sync.RWMutex
203+
customLogFunction LogFunction
204+
)
205+
206+
func SetLogFunction(f LogFunction) {
207+
logFnMu.Lock()
208+
defer logFnMu.Unlock()
209+
210+
if f == nil {
211+
C.cgoResetLogFunction()
212+
} else if customLogFunction == nil {
213+
C.cgoSetLogFunction()
214+
}
215+
customLogFunction = f
216+
}

0 commit comments

Comments
 (0)