-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdebug.go
More file actions
63 lines (50 loc) · 1.08 KB
/
debug.go
File metadata and controls
63 lines (50 loc) · 1.08 KB
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
package sshlib
import (
"fmt"
"os"
"strings"
"sync"
)
const (
debugEnv = "GO_SSHLIB_DEBUG"
debugLogEnv = "GO_SSHLIB_DEBUG_LOG"
)
var (
debugLogMu sync.Mutex
debugLogFile *os.File
)
func debugEnabled() bool {
value := strings.TrimSpace(strings.ToLower(os.Getenv(debugEnv)))
return value == "1" || value == "true" || value == "yes" || value == "on"
}
func debugWriter() *os.File {
if path := strings.TrimSpace(os.Getenv(debugLogEnv)); path != "" {
debugLogMu.Lock()
defer debugLogMu.Unlock()
if debugLogFile != nil && debugLogFile.Name() == path {
return debugLogFile
}
if debugLogFile != nil {
_ = debugLogFile.Close()
debugLogFile = nil
}
file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err == nil {
debugLogFile = file
return debugLogFile
}
}
return os.Stderr
}
func debugf(format string, args ...interface{}) {
if !debugEnabled() {
return
}
_, _ = fmt.Fprintf(debugWriter(), format, args...)
}
func debugln(args ...interface{}) {
if !debugEnabled() {
return
}
_, _ = fmt.Fprintln(debugWriter(), args...)
}