summaryrefslogtreecommitdiff
path: root/internal/io/dlog/rotation.go
blob: e2a1bb881718d44ad5f88fafaa907b40614470ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package dlog

import (
	"context"
	"os"
	"os/signal"
	"syscall"

	"github.com/mimecast/dtail/internal/io/dlog/loggers"
)

func rotation(ctx context.Context) {
	rotateCh := make(chan os.Signal, 1)
	signal.Notify(rotateCh, syscall.SIGHUP)
	go rotateLoop(ctx, rotateCh, loggers.FactoryRotate)
}

// rotateLoop services the log-rotation channel until ctx is cancelled. It is
// split out from rotation so tests can drive it directly with a fake rotate
// function and a test-owned channel.
func rotateLoop(ctx context.Context, rotateCh <-chan os.Signal, rotate func()) {
	for {
		select {
		case <-rotateCh:
			Common.Debug("Invoking log rotation")
			rotate()
		case <-ctx.Done():
			return
		}
	}
}