From cead23df0025c4e55c28c1ccc4848a1937a7a2df Mon Sep 17 00:00:00 2001 From: "J.C. Jones" Date: Fri, 1 May 2015 16:30:51 -0700 Subject: [PATCH] Updates per jsha's review --- log/audit-logger.go | 18 +++++++++++++----- log/audit-logger_test.go | 9 ++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/log/audit-logger.go b/log/audit-logger.go index 477b0bfe5..75908f12f 100644 --- a/log/audit-logger.go +++ b/log/audit-logger.go @@ -58,7 +58,7 @@ func NewAuditLogger(log *syslog.Writer, stats statsd.Statter) (*AuditLogger, err // initializeAuditLogger should only be used in unit tests. Failures in this // method are unlikely as the defaults are safe, and they are also -// of minimal consequence during unit testing -- logs get pritned to stdout +// of minimal consequence during unit testing -- logs get printed to stdout // even if syslog is missing. func initializeAuditLogger() { stats, _ := statsd.NewNoopClient(nil) @@ -71,10 +71,14 @@ func initializeAuditLogger() { // SetAuditLogger configures the singleton audit logger. This method // must only be called once, and before calling GetAuditLogger the // first time. -func SetAuditLogger(logger *AuditLogger) { - _Singleton.once.Do(func() { +func SetAuditLogger(logger *AuditLogger) (err error) { + if _Singleton.log != nil { + err = errors.New("You may not call SetAuditLogger after it has already been implicitly or explicitly set.") + _Singleton.log.WarningErr(err) + } else { _Singleton.log = logger - }) + } + return } // GetAuditLogger obtains the singleton audit logger. If SetAuditLogger @@ -82,7 +86,11 @@ func SetAuditLogger(logger *AuditLogger) { // The basic defaults cannot error, and subequent access to an already-set // AuditLogger also cannot error, so this method is error-safe. func GetAuditLogger() *AuditLogger { - initializeAuditLogger() + _Singleton.once.Do(func() { + if _Singleton.log == nil { + initializeAuditLogger() + } + }) return _Singleton.log } diff --git a/log/audit-logger_test.go b/log/audit-logger_test.go index 770408bf1..4dd399608 100644 --- a/log/audit-logger_test.go +++ b/log/audit-logger_test.go @@ -38,7 +38,10 @@ func TestSingleton(t *testing.T) { test.AssertNotError(t, err, "Could not construct audit logger") // Should not work - SetAuditLogger(log3) + err = SetAuditLogger(log3) + test.AssertError(t, err, "Can't re-set") + + // Verify no change log4 := GetAuditLogger() // Verify that log4 != log3 @@ -117,7 +120,3 @@ func TestSyslogMethods(t *testing.T) { audit.Warning("audit-logger_test.go: warning") audit.Alert("audit-logger_test.go: alert") } - -func TestEmergSyslog(t *testing.T) { - -}