Tidy up debug logging

This commit is contained in:
Arthur Barr
2018-02-21 09:58:49 +00:00
parent 8c92e02a23
commit 02cce7ab96
2 changed files with 13 additions and 14 deletions

View File

@@ -68,6 +68,10 @@ func doMain() error {
return err return err
} }
var wg sync.WaitGroup var wg sync.WaitGroup
defer func() {
log.Debugln("Waiting for log mirroring to complete")
wg.Wait()
}()
ctx, cancelMirror := context.WithCancel(context.Background()) ctx, cancelMirror := context.WithCancel(context.Background())
defer func() { defer func() {
log.Debugln("Cancel log mirroring") log.Debugln("Cancel log mirroring")
@@ -78,10 +82,6 @@ func doMain() error {
if err != nil { if err != nil {
return err return err
} }
defer func() {
log.Debugln("Waiting for log mirroring to complete")
wg.Wait()
}()
err = updateCommandLevel() err = updateCommandLevel()
if err != nil { if err != nil {
return err return err

View File

@@ -18,7 +18,6 @@ package main
import ( import (
"bufio" "bufio"
"context" "context"
"fmt"
"os" "os"
"sync" "sync"
"time" "time"
@@ -63,10 +62,10 @@ func mirrorAvailableMessages(f *os.File, mf mirrorFunc) {
mf(t) mf(t)
count++ count++
} }
log.Debugf("Mirrored %v log entries", count) log.Debugf("Mirrored %v log entries from %v", count, f.Name())
err := scanner.Err() err := scanner.Err()
if err != nil { if err != nil {
log.Errorf("Error reading file: %v", err) log.Errorf("Error reading file %v: %v", f.Name(), err)
return return
} }
} }
@@ -139,7 +138,7 @@ func mirrorLog(ctx context.Context, wg *sync.WaitGroup, path string, fromStart b
// The file now exists. If it didn't exist before we started, offset=0 // The file now exists. If it didn't exist before we started, offset=0
// Always start at the beginning if we've been told to go from the start // Always start at the beginning if we've been told to go from the start
if offset != 0 && !fromStart { if offset != 0 && !fromStart {
log.Debugf("Seeking %v", offset) log.Debugf("Seeking offset %v in file %v", offset, path)
f.Seek(offset, 0) f.Seek(offset, 0)
} }
closing := false closing := false
@@ -153,7 +152,7 @@ func mirrorLog(ctx context.Context, wg *sync.WaitGroup, path string, fromStart b
return return
} }
if !os.SameFile(fi, newFI) { if !os.SameFile(fi, newFI) {
log.Debugln("Detected log rotation") log.Debugf("Detected log rotation in file %v", path)
// WARNING: There is a possible race condition here. If *another* // WARNING: There is a possible race condition here. If *another*
// log rotation happens before we can open the new file, then we // log rotation happens before we can open the new file, then we
// could skip all those messages. This could happen with a very small // could skip all those messages. This could happen with a very small
@@ -161,11 +160,11 @@ func mirrorLog(ctx context.Context, wg *sync.WaitGroup, path string, fromStart b
mirrorAvailableMessages(f, mf) mirrorAvailableMessages(f, mf)
f.Close() f.Close()
// Re-open file // Re-open file
log.Debugln("Re-opening error log file") log.Debugf("Re-opening error log file %v", path)
// Used to work with this: f, err = waitForFile2(path)
f, err = os.OpenFile(path, os.O_RDONLY, 0) f, err = os.OpenFile(path, os.O_RDONLY, 0)
if err != nil { if err != nil {
fmt.Printf("ERROR: %v", err) log.Error(err)
errorChannel <- err
return return
} }
fi = newFI fi = newFI
@@ -174,9 +173,9 @@ func mirrorLog(ctx context.Context, wg *sync.WaitGroup, path string, fromStart b
} }
select { select {
case <-ctx.Done(): case <-ctx.Done():
log.Debug("Context cancelled") log.Debugf("Context cancelled for mirroring %v", path)
if closing { if closing {
log.Debug("Shutting down mirror") log.Debugf("Shutting down mirror for %v", path)
return return
} }
// Set a flag, to allow one more time through the loop // Set a flag, to allow one more time through the loop