aboutsummaryrefslogtreecommitdiff
path: root/internal/tlog/log.go
blob: 5535ef3893b2aedac95bbffad75a3d15f8d36f47 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Package tlog is a "toggled logger" that can be enabled and disabled and
// provides coloring.
package tlog

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"log"
	"log/syslog"
	"os"

	"golang.org/x/term"
)

const (
	// ProgramName is used in log reports.
	ProgramName = "gocryptfs"
	wpanicMsg   = "-wpanic turns this warning into a panic: "
)

// Escape sequences for terminal colors. These are set in init() if and only
// if stdout is a terminal. Otherwise they are empty strings.
var (
	// ColorReset is used to reset terminal colors.
	ColorReset string
	// ColorGrey is a terminal color setting string.
	ColorGrey string
	// ColorRed is a terminal color setting string.
	ColorRed string
	// ColorGreen is a terminal color setting string.
	ColorGreen string
	// ColorYellow is a terminal color setting string.
	ColorYellow string
)

// JSONDump writes the object in json form.
func JSONDump(obj interface{}) string {
	b, err := json.MarshalIndent(obj, "", "\t")
	if err != nil {
		return err.Error()
	}

	return string(b)
}

// toggledLogger - a Logger than can be enabled and disabled
type toggledLogger struct {
	// Enable or disable output
	Enabled bool
	// Panic after logging a message, useful in regression tests
	Wpanic bool
	// Private prefix and postfix are used for coloring
	prefix  string
	postfix string

	Logger *log.Logger
}

// trimNewline removes one trailing newline from "msg"
func trimNewline(msg string) string {
	if len(msg) == 0 {
		return msg
	}
	if msg[len(msg)-1] == '\n' {
		return msg[:len(msg)-1]
	}
	return msg
}

func (l *toggledLogger) Printf(format string, v ...interface{}) {
	if !l.Enabled {
		return
	}
	msg := trimNewline(fmt.Sprintf(format, v...))
	l.Logger.Printf(l.prefix + msg + l.postfix)
	if l.Wpanic {
		l.Logger.Panic(wpanicMsg + msg)
	}
}
func (l *toggledLogger) Println(v ...interface{}) {
	if !l.Enabled {
		return
	}
	msg := trimNewline(fmt.Sprint(v...))
	l.Logger.Println(l.prefix + msg + l.postfix)
	if l.Wpanic {
		l.Logger.Panic(wpanicMsg + msg)
	}
}

// Debug logs debug messages
// Can be enabled by passing "-d"
var Debug *toggledLogger

// Info logs informational message
// Can be disabled by passing "-q"
var Info *toggledLogger

// Warn logs warnings,
// meaning nothing serious by itself but might indicate problems.
// Passing "-wpanic" will make this function panic after printing the message.
var Warn *toggledLogger

// Fatal error, we are about to exit
var Fatal *toggledLogger

func init() {
	if term.IsTerminal(int(os.Stdout.Fd())) {
		ColorReset = "\033[0m"
		ColorGrey = "\033[2m"
		ColorRed = "\033[31m"
		ColorGreen = "\033[32m"
		ColorYellow = "\033[33m"
	}

	Debug = &toggledLogger{
		Logger: log.New(os.Stdout, "", 0),
	}
	Info = &toggledLogger{
		Enabled: true,
		Logger:  log.New(os.Stdout, "", 0),
	}
	Warn = &toggledLogger{
		Enabled: true,
		Logger:  log.New(os.Stderr, "", 0),
		prefix:  ColorYellow,
		postfix: ColorReset,
	}
	Fatal = &toggledLogger{
		Enabled: true,
		Logger:  log.New(os.Stderr, "", 0),
		prefix:  ColorRed,
		postfix: ColorReset,
	}
}

// SwitchToSyslog redirects the output of this logger to syslog.
// p = facility | severity
func (l *toggledLogger) SwitchToSyslog(p syslog.Priority) {
	w, err := syslog.New(p, ProgramName)
	if err != nil {
		Warn.Printf("SwitchToSyslog: %v", err)
	} else {
		l.Logger.SetOutput(w)
		// Disable colors
		l.prefix = ""
		l.postfix = ""
	}
}

// SwitchLoggerToSyslog redirects the default log.Logger that the go-fuse lib uses
// to syslog.
func SwitchLoggerToSyslog() {
	p := syslog.LOG_USER | syslog.LOG_WARNING
	w, err := syslog.New(p, ProgramName)
	if err != nil {
		Warn.Printf("SwitchLoggerToSyslog: %v", err)
	} else {
		log.SetPrefix("go-fuse: ")
		// Disable printing the timestamp, syslog already provides that
		log.SetFlags(0)
		log.SetOutput(w)
	}
}

// PrintMasterkeyReminder reminds the user that he should store the master key in
// a safe place.
func PrintMasterkeyReminder(key []byte) {
	if !Info.Enabled {
		// Quiet mode
		return
	}
	if !term.IsTerminal(int(os.Stdout.Fd())) {
		// We don't want the master key to end up in a log file
		Info.Printf("Not running on a terminal, suppressing master key display\n")
		return
	}
	h := hex.EncodeToString(key)
	var hChunked string
	// Try to make it less scary by splitting it up in chunks
	for i := 0; i < len(h); i += 8 {
		hChunked += h[i : i+8]
		if i < 52 {
			hChunked += "-"
		}
		if i == 24 {
			hChunked += "\n    "
		}
	}
	Info.Printf(`
Your master key is:

    %s

If the gocryptfs.conf file becomes corrupted or you ever forget your password,
there is only one hope for recovery: The master key. Print it to a piece of
paper and store it in a drawer. This message is only printed once.

`, ColorGrey+hChunked+ColorReset)
}