1package forestdb
2
3//#include "log.h"
4import "C"
5import (
6	"log"
7	"unsafe"
8)
9
10//export LogCallbackInternal
11func LogCallbackInternal(errCode C.int, msg *C.char, ctx *C.char) {
12	context := (*C.log_context)(unsafe.Pointer(ctx))
13	cb := logCallbacks[context.offset]
14	userCtx := logContexts[context.offset]
15	cb(C.GoString(context.name), int(errCode), C.GoString(msg), userCtx)
16}
17
18//export FatalErrorCallbackInternal
19func FatalErrorCallbackInternal() {
20	fatalErrorCallback()
21}
22
23// Logger interface
24type Logger interface {
25	// Warnings, logged by default.
26	Warnf(format string, v ...interface{})
27	// Errors, logged by default.
28	Errorf(format string, v ...interface{})
29	// Fatal errors. Will not terminate execution.
30	Fatalf(format string, v ...interface{})
31	// Informational messages.
32	Infof(format string, v ...interface{})
33	// Timing utility
34	Debugf(format string, v ...interface{})
35	// Program execution tracing. Not logged by default
36	Tracef(format string, v ...interface{})
37}
38
39type Dummy struct {
40}
41
42func (*Dummy) Fatalf(_ string, _ ...interface{}) {
43}
44
45func (*Dummy) Errorf(_ string, _ ...interface{}) {
46}
47
48func (*Dummy) Warnf(_ string, _ ...interface{}) {
49}
50
51func (*Dummy) Infof(_ string, _ ...interface{}) {
52}
53
54func (*Dummy) Debugf(_ string, _ ...interface{}) {
55}
56
57func (*Dummy) Tracef(_ string, _ ...interface{}) {
58}
59
60type LogLevel int
61
62const (
63	LogFatal LogLevel = iota
64	LogError
65	LogWarn
66	LogInfo
67	LogDebug
68	LogTrace
69)
70
71type LeveledLog struct {
72	level LogLevel
73}
74
75func NewLeveledLog(level LogLevel) *LeveledLog {
76	return &LeveledLog{level: level}
77}
78
79func (l *LeveledLog) Fatalf(format string, a ...interface{}) {
80	if l.level >= LogFatal {
81		log.Fatalf(format, a...)
82	}
83}
84
85func (l *LeveledLog) Errorf(format string, a ...interface{}) {
86	if l.level >= LogError {
87		log.Printf(format, a...)
88	}
89}
90
91func (l *LeveledLog) Warnf(format string, a ...interface{}) {
92	if l.level >= LogWarn {
93		log.Printf(format, a...)
94	}
95}
96
97func (l *LeveledLog) Infof(format string, a ...interface{}) {
98	if l.level >= LogInfo {
99		log.Printf(format, a...)
100	}
101}
102
103func (l *LeveledLog) Debugf(format string, a ...interface{}) {
104	if l.level >= LogDebug {
105		log.Printf(format, a...)
106	}
107}
108
109func (l *LeveledLog) Tracef(format string, a ...interface{}) {
110	if l.level >= LogTrace {
111		log.Printf(format, a...)
112	}
113}
114
115// Logger to use
116var Log Logger = &Dummy{}
117