Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
45587: log: secondary logger fixes r=ajwerner a=knz

Needed by / will be exercised by  cockroachdb#45193

Co-authored-by: Raphael 'kena' Poss <knz@thaumogen.net>
  • Loading branch information
craig[bot] and knz committed Mar 3, 2020
2 parents 0414b69 + e2da8bd commit 120d53f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
21 changes: 20 additions & 1 deletion pkg/util/log/secondary_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,27 @@ func NewSecondaryLogger(

// Ensure the registry knows about this logger.
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
secondaryLogRegistry.mu.loggers = append(secondaryLogRegistry.mu.loggers, l)
secondaryLogRegistry.mu.Unlock()

// Make the registry forget about this logger when the context is
// canceled. This avoids stacking many secondary loggers together
// when there are subsequent tests starting servers in the same
// package.
go func() {
// Wait for the stopper to say "goodbye".
<-ctx.Done()
// De-register.
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
for i, thatLogger := range secondaryLogRegistry.mu.loggers {
if thatLogger != l {
continue
}
secondaryLogRegistry.mu.loggers = append(secondaryLogRegistry.mu.loggers[:i], secondaryLogRegistry.mu.loggers[i+1:]...)
return
}
}()

if enableGc {
// Start the log file GC for the secondary logger.
Expand Down
33 changes: 24 additions & 9 deletions pkg/util/log/test_log_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,39 @@ func calledDuringPanic() bool {
// dirTestOverride sets the default value for the logging output directory
// for use in tests.
func dirTestOverride(expected, newDir string) error {
mainLog.mu.Lock()
defer mainLog.mu.Unlock()
if err := mainLog.dirTestOverride(expected, newDir); err != nil {
return err
}
// Same with secondary loggers.
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
for _, l := range secondaryLogRegistry.mu.loggers {
if err := l.logger.dirTestOverride(expected, newDir); err != nil {
return err
}
}
return nil
}

func (l *loggerT) dirTestOverride(expected, newDir string) error {
l.mu.Lock()
defer l.mu.Unlock()

mainLog.logDir.Lock()
l.logDir.Lock()
// The following check is intended to catch concurrent uses of
// Scope() or TestLogScope.Close(), which would be invalid.
if mainLog.logDir.name != expected {
mainLog.logDir.Unlock()
if l.logDir.name != expected {
l.logDir.Unlock()
return errors.Errorf("unexpected logDir setting: set to %q, expected %q",
mainLog.logDir.name, expected)
l.logDir.name, expected)
}
mainLog.logDir.name = newDir
mainLog.logDir.Unlock()
l.logDir.name = newDir
l.logDir.Unlock()

// When we change the directory we close the current logging
// output, so that a rotation to the new directory is forced on
// the next logging event.
return mainLog.closeFileLocked()
return l.closeFileLocked()
}

func (l *loggerT) closeFileLocked() error {
Expand Down

0 comments on commit 120d53f

Please sign in to comment.