forked from asticode/go-astiav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
79 lines (68 loc) · 1.81 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package astiav
//#cgo pkg-config: libavutil
//#include <libavutil/log.h>
/*
extern void goAstiavLogCallback(int level, char* msg, char* parent);
static inline void astiavLogCallback(void *avcl, int level, const char *fmt, va_list vl)
{
if (level > av_log_get_level()) return;
AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
char parent[1024];
if (avc) {
sprintf(parent, "%p", avcl);
}
char msg[1024];
vsprintf(msg, fmt, vl);
goAstiavLogCallback(level, msg, parent);
}
static inline void astiavSetLogCallback()
{
av_log_set_callback(astiavLogCallback);
}
static inline void astiavResetLogCallback()
{
av_log_set_callback(av_log_default_callback);
}
static inline void astiavLog(int level, const char *fmt)
{
av_log(NULL, level, fmt, NULL);
}
*/
import "C"
import "unsafe"
type LogLevel int
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavutil/log.h#L162
const (
LogLevelQuiet = LogLevel(C.AV_LOG_QUIET)
LogLevelPanic = LogLevel(C.AV_LOG_PANIC)
LogLevelFatal = LogLevel(C.AV_LOG_FATAL)
LogLevelError = LogLevel(C.AV_LOG_ERROR)
LogLevelWarning = LogLevel(C.AV_LOG_WARNING)
LogLevelInfo = LogLevel(C.AV_LOG_INFO)
LogLevelVerbose = LogLevel(C.AV_LOG_VERBOSE)
LogLevelDebug = LogLevel(C.AV_LOG_DEBUG)
)
func SetLogLevel(l LogLevel) {
C.av_log_set_level(C.int(l))
}
type LogCallback func(l LogLevel, msg, parent string)
var logCallback LogCallback
func SetLogCallback(c LogCallback) {
logCallback = c
C.astiavSetLogCallback()
}
//export goAstiavLogCallback
func goAstiavLogCallback(level C.int, msg, parent *C.char) {
if logCallback == nil {
return
}
logCallback(LogLevel(level), C.GoString(msg), C.GoString(parent))
}
func ResetLogCallback() {
C.astiavResetLogCallback()
}
func Log(l LogLevel, msg string) {
msgc := C.CString(msg)
defer C.free(unsafe.Pointer(msgc))
C.astiavLog(C.int(l), msgc)
}