From 17f0eb13396ad31083e786ed64aef368646c2aa6 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 20 Jan 2016 20:55:56 +0100 Subject: Convert logging to standard Go log.Logger This is in preparation of logging to syslog. --- cryptfs/config_file.go | 10 ++++---- cryptfs/config_test.go | 6 +++-- cryptfs/cryptfs_content.go | 6 ++--- cryptfs/gcm_go14.go | 4 +-- cryptfs/kdf.go | 2 +- cryptfs/log.go | 64 +++++++++++----------------------------------- 6 files changed, 30 insertions(+), 62 deletions(-) (limited to 'cryptfs') diff --git a/cryptfs/config_file.go b/cryptfs/config_file.go index 138426a..013b82d 100644 --- a/cryptfs/config_file.go +++ b/cryptfs/config_file.go @@ -75,17 +75,17 @@ func LoadConfFile(filename string, password string) ([]byte, *ConfFile, error) { // Unmarshal err = json.Unmarshal(js, &cf) if err != nil { - Warn.Printf("Failed to unmarshal config file\n") + Warn.Printf("Failed to unmarshal config file") return nil, nil, err } if cf.Version != HEADER_CURRENT_VERSION { - return nil, nil, fmt.Errorf("Unsupported on-disk format %d\n", cf.Version) + return nil, nil, fmt.Errorf("Unsupported on-disk format %d", cf.Version) } for _, flag := range cf.FeatureFlags { if cf.isFeatureFlagKnown(flag) == false { - return nil, nil, fmt.Errorf("Unsupported feature flag %s\n", flag) + return nil, nil, fmt.Errorf("Unsupported feature flag %s", flag) } } @@ -98,8 +98,8 @@ func LoadConfFile(filename string, password string) ([]byte, *ConfFile, error) { cfs := NewCryptFS(scryptHash, false, false, false) key, err := cfs.DecryptBlock(cf.EncryptedKey, 0, nil) if err != nil { - Warn.Printf("failed to unlock master key: %s\n", err.Error()) - Warn.Printf("Password incorrect.\n") + Warn.Printf("failed to unlock master key: %s", err.Error()) + Warn.Printf("Password incorrect.") return nil, nil, err } diff --git a/cryptfs/config_test.go b/cryptfs/config_test.go index 817e6ae..5cf04bf 100644 --- a/cryptfs/config_test.go +++ b/cryptfs/config_test.go @@ -2,6 +2,8 @@ package cryptfs import ( "fmt" + "io/ioutil" + "os" "testing" "time" ) @@ -33,10 +35,10 @@ func TestLoadV2(t *testing.T) { func TestLoadV2PwdError(t *testing.T) { if !testing.Verbose() { - Warn.Disable() + Warn.SetOutput(ioutil.Discard) } _, _, err := LoadConfFile("config_test/v2.conf", "wrongpassword") - Warn.Enable() + Warn.SetOutput(os.Stderr) if err == nil { t.Errorf("Loading with wrong password must fail but it didn't") } diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go index 9a79db4..2036e58 100644 --- a/cryptfs/cryptfs_content.go +++ b/cryptfs/cryptfs_content.go @@ -55,12 +55,12 @@ func (be *CryptFS) DecryptBlock(ciphertext []byte, blockNo uint64, fileId []byte // All-zero block? if bytes.Equal(ciphertext, be.allZeroBlock) { - Debug.Printf("DecryptBlock: file hole encountered\n") + Debug.Printf("DecryptBlock: file hole encountered") return make([]byte, be.plainBS), nil } if len(ciphertext) < be.gcmIVLen { - Warn.Printf("DecryptBlock: Block is too short: %d bytes\n", len(ciphertext)) + Warn.Printf("DecryptBlock: Block is too short: %d bytes", len(ciphertext)) return nil, errors.New("Block is too short") } @@ -77,7 +77,7 @@ func (be *CryptFS) DecryptBlock(ciphertext []byte, blockNo uint64, fileId []byte plaintext, err := be.gcm.Open(plaintext, nonce, ciphertext, aData) if err != nil { - Warn.Printf("DecryptBlock: %s, len=%d, md5=%s\n", err.Error(), len(ciphertextOrig), Warn.Md5sum(ciphertextOrig)) + Warn.Printf("DecryptBlock: %s, len=%d, md5=%s", err.Error(), len(ciphertextOrig), md5sum(ciphertextOrig)) Debug.Println(hex.Dump(ciphertextOrig)) return nil, err } diff --git a/cryptfs/gcm_go14.go b/cryptfs/gcm_go14.go index b675b51..0d4d5a6 100644 --- a/cryptfs/gcm_go14.go +++ b/cryptfs/gcm_go14.go @@ -13,8 +13,8 @@ import ( // compiled on 1.4. func goGCMWrapper(bc cipher.Block, nonceSize int) (cipher.AEAD, error) { if nonceSize != 12 { - Warn.Printf("128 bit GCM IVs are not supported by Go 1.4 and lower.\n") - Warn.Printf("Please use openssl crypto or recompile using a newer Go runtime.\n") + Warn.Printf("128 bit GCM IVs are not supported by Go 1.4 and lower.") + Warn.Printf("Please use openssl crypto or recompile using a newer Go runtime.") return nil, fmt.Errorf("128 bit GCM IVs are not supported by Go 1.4 and lower") } return cipher.NewGCM(bc) diff --git a/cryptfs/kdf.go b/cryptfs/kdf.go index 9a1d81c..e958413 100644 --- a/cryptfs/kdf.go +++ b/cryptfs/kdf.go @@ -28,7 +28,7 @@ func NewScryptKdf(logN int) scryptKdf { s.N = 1 << SCRYPT_DEFAULT_LOGN } else { if logN < 10 { - fmt.Printf("Error: scryptn below 10 is too low to make sense. Aborting.\n") + fmt.Println("Error: scryptn below 10 is too low to make sense. Aborting.") os.Exit(1) } s.N = 1 << uint32(logN) diff --git a/cryptfs/log.go b/cryptfs/log.go index 8a6ace8..32fe228 100644 --- a/cryptfs/log.go +++ b/cryptfs/log.go @@ -2,67 +2,33 @@ package cryptfs import ( "encoding/json" - "fmt" - "strings" + "io/ioutil" + "log" + "os" ) -type logChannel struct { - enabled bool -} - -func (l *logChannel) Printf(format string, args ...interface{}) { - 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 - } +func JSONDump(obj interface{}) string { b, err := json.MarshalIndent(obj, "", "\t") if err != nil { - fmt.Println(err) + return err.Error() } 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 string(b) } - return md5sum(buf) } // As defined by http://elinux.org/Debugging_by_printing#Log_Levels // Debug messages -var Debug = logChannel{false} +var Debug *log.Logger // Informational message e.g. startup information -var Info = logChannel{true} +var Info *log.Logger // A warning, meaning nothing serious by itself but might indicate problems -var Warn = logChannel{true} +var Warn *log.Logger + +func init() { + Debug = log.New(ioutil.Discard, "", 0) + Info = log.New(os.Stdout, "", 0) + Warn = log.New(os.Stderr, "", 0) +} -- cgit v1.2.3