-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
vips_init.go
78 lines (67 loc) Β· 1.81 KB
/
vips_init.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
package thumb
import (
"strings"
"sync"
"github.com/davidbyttow/govips/v2/vips"
"github.com/sirupsen/logrus"
)
var (
vipsStarted = false
vipsStart = sync.Once{}
)
// VipsInit initializes libvips by checking its version and loading the ICC profiles once.
func VipsInit() {
vipsStart.Do(vipsInit)
}
// VipsShutdown shuts down libvips and removes temporary files.
func VipsShutdown() {
if vipsStarted {
vipsStarted = false
vipsStart = sync.Once{}
vips.Shutdown()
}
}
// vipsInit calls vips.Startup() to initialize libvips.
func vipsInit() {
if vipsStarted == true {
log.Warnf("vips: already initialized - you may have found a bug")
return
}
vipsStarted = true
// Configure logging.
vips.LoggingSettings(func(domain string, level vips.LogLevel, msg string) {
switch level {
case vips.LogLevelError, vips.LogLevelCritical:
log.Errorf("%s: %s", strings.TrimSpace(strings.ToLower(domain)), msg)
case vips.LogLevelWarning:
log.Warnf("%s: %s", strings.TrimSpace(strings.ToLower(domain)), msg)
default:
log.Tracef("%s: %s", strings.TrimSpace(strings.ToLower(domain)), msg)
}
}, vipsLogLevel())
// Start libvips.
vips.Startup(vipsConfig())
}
// vipsConfig provides the config for initializing libvips.
func vipsConfig() *vips.Config {
return &vips.Config{
MaxCacheMem: MaxCacheMem,
MaxCacheSize: MaxCacheSize,
MaxCacheFiles: MaxCacheFiles,
ConcurrencyLevel: NumWorkers,
ReportLeaks: false,
CacheTrace: false,
CollectStats: false,
}
}
// vipsLogLevel provides the libvips equivalent of the current log level.
func vipsLogLevel() vips.LogLevel {
switch log.GetLevel() {
case logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel:
return vips.LogLevelError
case logrus.TraceLevel:
return vips.LogLevelDebug
default:
return vips.LogLevelWarning
}
}