diff --git a/.gitignore b/.gitignore index 3a14734..b24787d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ _bin/ _rust/ *.DS_Store* *.supp -zzgenerated* \ No newline at end of file +zzgenerated* +.idea/ diff --git a/gst/cgo_exports.go b/gst/cgo_exports.go index c9c7cbf..3b4b9fe 100644 --- a/gst/cgo_exports.go +++ b/gst/cgo_exports.go @@ -242,3 +242,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( + category *C.GstDebugCategory, + level C.GstDebugLevel, + file *C.gchar, + function *C.gchar, + line C.gint, + object *C.GObject, + message *C.GstDebugMessage, + userData 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)), + ) + } +} diff --git a/gst/gst_debug.go b/gst/gst_debug.go index 8c27aae..d07fda7 100644 --- a/gst/gst_debug.go +++ b/gst/gst_debug.go @@ -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, @@ -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/go-gst/go-glib/glib" ) // DebugColorFlags are terminal style flags you can use when creating your debugging @@ -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 +}