Skip to content

Commit

Permalink
set log function (#3)
Browse files Browse the repository at this point in the history
* gstreamer set log function

* missing parameters

* wrong object type

* spacing

* transfer

* nil check object

* add lock

* unset -> reset

* prevent data race
  • Loading branch information
frostbyte73 authored Jun 23, 2023
1 parent 8fc9f59 commit 11c001b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ _bin/
_rust/
*.DS_Store*
*.supp
zzgenerated*
zzgenerated*
.idea/
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
github.com/tinyzimmer/go-glib v0.0.24 h1:ktZZC22/9t88kGRgNEFV/SESgIWhGHE+q7Z7Qj++luw=
github.com/tinyzimmer/go-glib v0.0.24/go.mod h1:ltV0gO6xNFzZhsIRbFXv8RTq9NGoNT2dmAER4YmZfaM=
github.com/tinyzimmer/go-glib v0.0.25 h1:2GpumtkxA0wpXhCXP6D3ksb5pGMfo9WbhgLvEw8njK4=
github.com/tinyzimmer/go-glib v0.0.25/go.mod h1:ltV0gO6xNFzZhsIRbFXv8RTq9NGoNT2dmAER4YmZfaM=
31 changes: 31 additions & 0 deletions gst/cgo_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,34 @@ func goPluginInit(plugin *C.GstPlugin, userData C.gpointer) C.gboolean {
func goGlobalPluginInit(plugin *C.GstPlugin) C.gboolean {
return gboolean(globalPluginInit(wrapPlugin(&glib.Object{GObject: glib.ToGObject(unsafe.Pointer(plugin))})))
}

//export goLogFunction
func goLogFunction(
_ *C.GstDebugCategory,
level C.GstDebugLevel,
file *C.gchar,
function *C.gchar,
line C.gint,
object *C.GObject,
message *C.GstDebugMessage,
_ C.gpointer,
) {
logFnMu.RLock()
f := customLogFunction
logFnMu.RUnlock()

if f != nil {
var obj *glib.Object
if object != nil {
obj = glib.TransferNone(unsafe.Pointer(object))
}
f(
DebugLevel(level),
C.GoString(file),
C.GoString(function),
int(line),
obj,
C.GoString(C.gst_debug_message_get(message)),
)
}
}
50 changes: 50 additions & 0 deletions gst/gst_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ package gst
/*
#include "gst.go.h"
extern void goLogFunction(GstDebugCategory * category,
GstDebugLevel level,
const gchar * file,
const gchar * function,
gint line,
GObject * object,
GstDebugMessage * message,
gpointer user_data) G_GNUC_NO_INSTRUMENT;
void cgoDebugLog (GstDebugCategory * category,
GstDebugLevel level,
const gchar * file,
Expand All @@ -14,12 +23,27 @@ void cgoDebugLog (GstDebugCategory * category,
gst_debug_log(category, level, file, function, line, object, msg);
}
void cgoSetLogFunction()
{
gst_debug_remove_log_function(gst_debug_log_default);
gst_debug_add_log_function(goLogFunction, NULL, NULL);
}
void cgoResetLogFunction()
{
gst_debug_remove_log_function(goLogFunction);
gst_debug_add_log_function(gst_debug_log_default, NULL, NULL);
}
*/
import "C"
import (
"path"
"runtime"
"sync"
"unsafe"

"github.com/tinyzimmer/go-glib/glib"
)

// DebugColorFlags are terminal style flags you can use when creating your debugging
Expand Down Expand Up @@ -164,3 +188,29 @@ func (d *DebugCategory) LogTrace(message string, obj ...*Object) {
func (d *DebugCategory) LogMemDump(message string, obj ...*Object) {
d.logDepth(LevelMemDump, message, 2, getLogObj(obj...))
}

type LogFunction func(
level DebugLevel,
file string,
function string,
line int,
object *glib.Object,
message string,
)

var (
logFnMu sync.RWMutex
customLogFunction LogFunction
)

func SetLogFunction(f LogFunction) {
logFnMu.Lock()
defer logFnMu.Unlock()

if f == nil {
C.cgoResetLogFunction()
} else if customLogFunction == nil {
C.cgoSetLogFunction()
}
customLogFunction = f
}

0 comments on commit 11c001b

Please sign in to comment.