feat(ffmpeg): logging callback handler
This commit is contained in:
parent
f11d2b351c
commit
57541418b9
6 changed files with 71 additions and 1 deletions
|
|
@ -52,3 +52,8 @@ func goInterrupt(opaque unsafe.Pointer) C.int {
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//export goAVLoggingHandler
|
||||||
|
func goAVLoggingHandler(level C.int, cstr *C.char) {
|
||||||
|
log(AVLogLevel(level), C.GoString(cstr))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,4 +69,4 @@ extern int goPacketRead(void *opaque, uint8_t *buf, int buf_size);
|
||||||
|
|
||||||
extern int64_t goPacketSeek(void *opaque, int64_t seek, int whence);
|
extern int64_t goPacketSeek(void *opaque, int64_t seek, int whence);
|
||||||
|
|
||||||
extern int goInterrupt(void *opaque);
|
extern int goInterrupt(void *opaque);
|
||||||
|
|
|
||||||
12
ffmpeg/logging.c
Normal file
12
ffmpeg/logging.c
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include "logging.h"
|
||||||
|
|
||||||
|
void goavLogCallback(void *class_ptr, int level, const char *fmt, va_list vl) {
|
||||||
|
char line[LINE_SZ];
|
||||||
|
int print_prefix = 1;
|
||||||
|
av_log_format_line(class_ptr, level, fmt, vl, line, LINE_SZ, &print_prefix);
|
||||||
|
goAVLoggingHandler(level, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
void goavLogSetup() {
|
||||||
|
av_log_set_callback(goavLogCallback);
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package ffmpeg
|
package ffmpeg
|
||||||
|
|
||||||
// #include "ffmpeg.h"
|
// #include "ffmpeg.h"
|
||||||
|
// #include "logging.h"
|
||||||
import "C"
|
import "C"
|
||||||
|
import "sync"
|
||||||
|
|
||||||
// AVLogLevel defines the ffmpeg threshold for dumping information to stderr.
|
// AVLogLevel defines the ffmpeg threshold for dumping information to stderr.
|
||||||
type AVLogLevel int
|
type AVLogLevel int
|
||||||
|
|
@ -19,6 +21,12 @@ const (
|
||||||
AVLogTrace
|
AVLogTrace
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
currentLoggingHandlerFunction = noopLoggingHandler
|
||||||
|
currentLoggingVerbosity AVLogLevel
|
||||||
|
onceLogging sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
func logLevel() AVLogLevel {
|
func logLevel() AVLogLevel {
|
||||||
return AVLogLevel(C.av_log_get_level())
|
return AVLogLevel(C.av_log_get_level())
|
||||||
}
|
}
|
||||||
|
|
@ -26,4 +34,25 @@ func logLevel() AVLogLevel {
|
||||||
// SetFFmpegLogLevel allows you to change the log level from the default (AVLogInfo).
|
// SetFFmpegLogLevel allows you to change the log level from the default (AVLogInfo).
|
||||||
func SetFFmpegLogLevel(logLevel AVLogLevel) {
|
func SetFFmpegLogLevel(logLevel AVLogLevel) {
|
||||||
C.av_log_set_level(C.int(logLevel))
|
C.av_log_set_level(C.int(logLevel))
|
||||||
|
currentLoggingVerbosity = logLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoggingHandlerFunction func(messageLevel AVLogLevel, message string)
|
||||||
|
|
||||||
|
func SetLogging(handler LoggingHandlerFunction) {
|
||||||
|
onceLogging.Do(func() {
|
||||||
|
C.goavLogSetup()
|
||||||
|
})
|
||||||
|
if handler != nil {
|
||||||
|
currentLoggingHandlerFunction = handler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func noopLoggingHandler(_ AVLogLevel, _ string) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func log(level AVLogLevel, message string) {
|
||||||
|
if level <= currentLoggingVerbosity {
|
||||||
|
currentLoggingHandlerFunction(level, message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
ffmpeg/logging.h
Normal file
11
ffmpeg/logging.h
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#define LINE_SZ 1024
|
||||||
|
|
||||||
|
extern void goAVLoggingHandler(int level, char *str);
|
||||||
|
extern void av_log_set_callback(void (*callback)(void *, int, const char *, va_list));
|
||||||
|
extern void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl, char *line, int line_size, int *print_prefix);
|
||||||
|
|
||||||
|
void goavLogCallback(void *class_ptr, int level, const char *fmt, va_list vl);
|
||||||
|
|
||||||
|
void goavLogSetup();
|
||||||
13
processor.go
13
processor.go
|
|
@ -29,6 +29,19 @@ func NewProcessor(options ...Option) *Processor {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Processor) Startup(_ context.Context) error {
|
func (p *Processor) Startup(_ context.Context) error {
|
||||||
|
ffmpeg.SetLogging(func(level ffmpeg.AVLogLevel, message string) {
|
||||||
|
message = strings.TrimSuffix(message, "\n")
|
||||||
|
switch level {
|
||||||
|
case ffmpeg.AVLogTrace, ffmpeg.AVLogDebug, ffmpeg.AVLogVerbose:
|
||||||
|
p.Logger.Debug("ffmpeg", zap.String("msg", message))
|
||||||
|
case ffmpeg.AVLogInfo:
|
||||||
|
p.Logger.Info("ffmpeg", zap.String("msg", message))
|
||||||
|
case ffmpeg.AVLogWarning:
|
||||||
|
p.Logger.Warn("ffmpeg", zap.String("msg", message))
|
||||||
|
case ffmpeg.AVLogError, ffmpeg.AVLogFatal, ffmpeg.AVLogPanic:
|
||||||
|
p.Logger.Error("ffmpeg", zap.String("msg", message))
|
||||||
|
}
|
||||||
|
})
|
||||||
if p.Debug {
|
if p.Debug {
|
||||||
ffmpeg.SetFFmpegLogLevel(ffmpeg.AVLogDebug)
|
ffmpeg.SetFFmpegLogLevel(ffmpeg.AVLogDebug)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue