forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
64 lines (55 loc) · 1.2 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
package main
import (
"fmt"
"log"
"os"
)
const (
defaultLogFormat = "direnv: %s"
errorLogFormat = defaultLogFormat
errorLogFormatWithColor = "\033[31mdirenv: %s\033[0m"
)
var debugging bool
var noColor = os.Getenv("TERM") == "dumb"
func setupLogging(env Env) {
log.SetFlags(0)
log.SetPrefix("")
if val, ok := env[DIRENV_DEBUG]; ok && val == "1" {
debugging = true
log.SetFlags(log.Ltime)
log.SetPrefix("direnv: ")
}
}
func logError(msg string, a ...interface{}) {
if noColor {
logMsg(errorLogFormat, msg, a...)
} else {
logMsg(errorLogFormatWithColor, msg, a...)
}
}
func logStatus(env Env, msg string, a ...interface{}) {
format, ok := env["DIRENV_LOG_FORMAT"]
if !ok {
format = defaultLogFormat
}
if format != "" {
logMsg(format, msg, a...)
}
}
func logDebug(msg string, a ...interface{}) {
if !debugging {
return
}
defer log.SetFlags(log.Flags())
log.SetFlags(log.Flags() | log.Lshortfile)
msg = fmt.Sprintf(msg, a...)
_ = log.Output(2, msg)
}
func logMsg(format, msg string, a ...interface{}) {
defer log.SetFlags(log.Flags())
defer log.SetPrefix(log.Prefix())
log.SetFlags(0)
log.SetPrefix("")
msg = fmt.Sprintf(format+"\n", msg)
log.Printf(msg, a...)
}