From d1631696556cb6460db1d6eedf9fbc3015433b1c Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 21 Jan 2016 22:59:11 +0100 Subject: Wrap Logger to make disabling output more efficient Instead of using SetOutput(ioutil.Discard), which means that Printf is still called for every debug message, use a simple and fast boolean check. Streaming write performance improves from 86 to 93 MB/s. --- cryptfs/config_test.go | 3 +-- cryptfs/cryptfs.go | 2 ++ cryptfs/log.go | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 10 deletions(-) (limited to 'cryptfs') diff --git a/cryptfs/config_test.go b/cryptfs/config_test.go index 5cf04bf..0dedbd9 100644 --- a/cryptfs/config_test.go +++ b/cryptfs/config_test.go @@ -2,7 +2,6 @@ package cryptfs import ( "fmt" - "io/ioutil" "os" "testing" "time" @@ -35,7 +34,7 @@ func TestLoadV2(t *testing.T) { func TestLoadV2PwdError(t *testing.T) { if !testing.Verbose() { - Warn.SetOutput(ioutil.Discard) + Warn.Enabled = false } _, _, err := LoadConfFile("config_test/v2.conf", "wrongpassword") Warn.SetOutput(os.Stderr) diff --git a/cryptfs/cryptfs.go b/cryptfs/cryptfs.go index 232129f..5b3f074 100644 --- a/cryptfs/cryptfs.go +++ b/cryptfs/cryptfs.go @@ -9,6 +9,8 @@ import ( ) const ( + PROGRAM_NAME = "gocryptfs" + DEFAULT_PLAINBS = 4096 KEY_LEN = 32 // AES-256 AUTH_TAG_LEN = 16 diff --git a/cryptfs/log.go b/cryptfs/log.go index 32fe228..a5b5115 100644 --- a/cryptfs/log.go +++ b/cryptfs/log.go @@ -2,8 +2,8 @@ package cryptfs import ( "encoding/json" - "io/ioutil" "log" + "log/syslog" "os" ) @@ -16,19 +16,45 @@ func JSONDump(obj interface{}) string { } } -// As defined by http://elinux.org/Debugging_by_printing#Log_Levels +// toggledLogger - a Logger than can be enabled and disabled +type toggledLogger struct { + Enabled bool + *log.Logger +} + +func (l *toggledLogger) Printf(format string, v ...interface{}) { + if !l.Enabled { + return + } + l.Logger.Printf(format, v...) +} +func (l *toggledLogger) Println(v ...interface{}) { + if !l.Enabled { + return + } + l.Logger.Println(v...) +} +func (l *toggledLogger) SwitchToSyslog(p syslog.Priority) { + w, err := syslog.New(p, PROGRAM_NAME) + if err != nil { + Warn.Printf("Cannot switch 0x%02x to syslog: %v", p, err) + } else { + l.SetOutput(w) + } +} +// As defined by http://elinux.org/Debugging_by_printing#Log_Levels // Debug messages -var Debug *log.Logger +var Debug *toggledLogger // Informational message e.g. startup information -var Info *log.Logger +var Info *toggledLogger // A warning, meaning nothing serious by itself but might indicate problems -var Warn *log.Logger +var Warn *toggledLogger func init() { - Debug = log.New(ioutil.Discard, "", 0) - Info = log.New(os.Stdout, "", 0) - Warn = log.New(os.Stderr, "", 0) + Debug = &toggledLogger{false, log.New(os.Stdout, "", 0)} + Info = &toggledLogger{true, log.New(os.Stdout, "", 0)} + Warn = &toggledLogger{true, log.New(os.Stderr, "", 0)} } -- cgit v1.2.3