libgocryptfs/cryptfs/log.go

69 lines
1.2 KiB
Go
Raw Normal View History

2015-09-03 18:57:28 +02:00
package cryptfs
2015-09-03 18:22:18 +02:00
import (
"fmt"
"strings"
"encoding/json"
2015-09-03 18:22:18 +02:00
)
type logChannel struct {
enabled bool
}
func (l *logChannel) Printf(format string, args ...interface{}) {
2015-09-03 18:22:18 +02:00
if l.enabled == true {
fmt.Printf(format, args...)
}
}
func (l *logChannel) Println(s string) {
if l.enabled == true {
fmt.Println(s)
}
}
func (l *logChannel) Dump(d []byte) {
s := string(d)
fmt.Println(strings.Replace(s, "\000", "\\0", -1))
}
func (l *logChannel) JSONDump(obj interface{}) {
if !l.enabled {
return
}
b, err := json.MarshalIndent(obj, "", "\t")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(b))
}
}
func (l *logChannel) Enable() {
l.enabled = true
}
func (l *logChannel) Disable() {
l.enabled = false
}
// Only actually calculate the md5sum if the log channel is enabled to save
// CPU cycles
func (l *logChannel) Md5sum(buf []byte) string {
if l.enabled == false {
return "disabled"
}
return md5sum(buf)
}
2015-09-03 18:22:18 +02:00
2015-11-09 22:33:42 +01:00
// As defined by http://elinux.org/Debugging_by_printing#Log_Levels
// Debug messages
var Debug = logChannel{false}
2015-11-14 17:16:17 +01:00
2015-11-09 22:33:42 +01:00
// Informational message e.g. startup information
var Info = logChannel{true}
2015-11-14 17:16:17 +01:00
2015-11-09 22:33:42 +01:00
// A warning, meaning nothing serious by itself but might indicate problems
2015-09-05 20:36:26 +02:00
var Warn = logChannel{true}