Added fmt to log callback

This commit is contained in:
Quentin Renard
2022-03-24 15:35:37 +01:00
parent 12f9a2e067
commit fe4242eb59
6 changed files with 15 additions and 10 deletions

View File

@@ -23,7 +23,7 @@ type stream struct {
func main() { func main() {
// Handle ffmpeg logs // Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug) astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(l astiav.LogLevel, msg, parent string) { astiav.SetLogCallback(func(l astiav.LogLevel, fmt, msg, parent string) {
log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l) log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l)
}) })

View File

@@ -37,7 +37,7 @@ type stream struct {
func main() { func main() {
// Handle ffmpeg logs // Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug) astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(l astiav.LogLevel, msg, parent string) { astiav.SetLogCallback(func(l astiav.LogLevel, fmt, msg, parent string) {
log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l) log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l)
}) })

View File

@@ -18,7 +18,7 @@ var (
func main() { func main() {
// Handle ffmpeg logs // Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug) astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(l astiav.LogLevel, msg, parent string) { astiav.SetLogCallback(func(l astiav.LogLevel, fmt, msg, parent string) {
log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l) log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l)
}) })

View File

@@ -42,7 +42,7 @@ type stream struct {
func main() { func main() {
// Handle ffmpeg logs // Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug) astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(l astiav.LogLevel, msg, parent string) { astiav.SetLogCallback(func(l astiav.LogLevel, fmt, msg, parent string) {
log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l) log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l)
}) })

10
log.go
View File

@@ -3,7 +3,7 @@ package astiav
//#cgo pkg-config: libavutil //#cgo pkg-config: libavutil
//#include <libavutil/log.h> //#include <libavutil/log.h>
/* /*
extern void goAstiavLogCallback(int level, char* msg, char* parent); extern void goAstiavLogCallback(int level, char* fmt, char* msg, char* parent);
static inline void astiavLogCallback(void *avcl, int level, const char *fmt, va_list vl) static inline void astiavLogCallback(void *avcl, int level, const char *fmt, va_list vl)
{ {
@@ -15,7 +15,7 @@ static inline void astiavLogCallback(void *avcl, int level, const char *fmt, va_
} }
char msg[1024]; char msg[1024];
vsprintf(msg, fmt, vl); vsprintf(msg, fmt, vl);
goAstiavLogCallback(level, msg, parent); goAstiavLogCallback(level, (char*)(fmt), msg, parent);
} }
static inline void astiavSetLogCallback() static inline void astiavSetLogCallback()
{ {
@@ -51,7 +51,7 @@ func SetLogLevel(l LogLevel) {
C.av_log_set_level(C.int(l)) C.av_log_set_level(C.int(l))
} }
type LogCallback func(l LogLevel, msg, parent string) type LogCallback func(l LogLevel, fmt, msg, parent string)
var logCallback LogCallback var logCallback LogCallback
@@ -61,11 +61,11 @@ func SetLogCallback(c LogCallback) {
} }
//export goAstiavLogCallback //export goAstiavLogCallback
func goAstiavLogCallback(level C.int, msg, parent *C.char) { func goAstiavLogCallback(level C.int, fmt, msg, parent *C.char) {
if logCallback == nil { if logCallback == nil {
return return
} }
logCallback(LogLevel(level), C.GoString(msg), C.GoString(parent)) logCallback(LogLevel(level), C.GoString(fmt), C.GoString(msg), C.GoString(parent))
} }
func ResetLogCallback() { func ResetLogCallback() {

View File

@@ -8,14 +8,16 @@ import (
) )
type logItem struct { type logItem struct {
fmt string
l astiav.LogLevel l astiav.LogLevel
msg string msg string
} }
func TestLog(t *testing.T) { func TestLog(t *testing.T) {
var lis []logItem var lis []logItem
astiav.SetLogCallback(func(l astiav.LogLevel, msg, parent string) { astiav.SetLogCallback(func(l astiav.LogLevel, fmt, msg, parent string) {
lis = append(lis, logItem{ lis = append(lis, logItem{
fmt: fmt,
l: l, l: l,
msg: msg, msg: msg,
}) })
@@ -27,14 +29,17 @@ func TestLog(t *testing.T) {
astiav.Log(astiav.LogLevelFatal, "fatal") astiav.Log(astiav.LogLevelFatal, "fatal")
require.Equal(t, []logItem{ require.Equal(t, []logItem{
{ {
fmt: "warning",
l: astiav.LogLevelWarning, l: astiav.LogLevelWarning,
msg: "warning", msg: "warning",
}, },
{ {
fmt: "error",
l: astiav.LogLevelError, l: astiav.LogLevelError,
msg: "error", msg: "error",
}, },
{ {
fmt: "fatal",
l: astiav.LogLevelFatal, l: astiav.LogLevelFatal,
msg: "fatal", msg: "fatal",
}, },