<commit_before>package logger

import (
	"fmt"
	"os"
	"sync"
	"time"

	logging "github.com/keybase/go-logging"
)

// LogFileConfig is the config structure for new style log files with rotation.
type LogFileConfig struct {
	// Path is the path of the log file to use
	Path string
	// MaxSize is the size of log file before rotation, 0 for infinite.
	MaxSize int64
	// MaxAge is th duration before log rotation, zero value for infinite.
	MaxAge time.Duration
}

// SetLogFileConfig sets the log file config to be used globally.
func SetLogFileConfig(lfc *LogFileConfig) error {
	globalLock.Lock()
	defer globalLock.Unlock()

	first := true
	var w = currentLogFileWriter
	if w != nil {
		first = false
		w.lock.Lock()
		defer w.lock.Unlock()
		w.Close()
	} else {
		w = &logFileWriter{}
	}
	w.config = *lfc

	err := w.Open(time.Now())
	if err != nil {
		return err
	}

	if first {
		fileBackend := logging.NewLogBackend(w, "", 0)
		logging.SetBackend(fileBackend)

		stderrIsTerminal = false
	}
	return nil
}

type logFileWriter struct {
	lock         sync.Mutex
	config       LogFileConfig
	file         *os.File
	currentSize  int64
	currentStart time.Time
}

func (lfw *logFileWriter) Open(at time.Time) error {
	var err error
	_, lfw.file, err = OpenLogFile(lfw.config.Path)
	if err != nil {
		return err<commit_msg>logger: Handle switching log file name multiple times<commit_after>package logger

import (
	"fmt"
	"os"
	"sync"
	"time"

	logging "github.com/keybase/go-logging"
)

// LogFileConfig is the config structure for new style log files with rotation.
type LogFileConfig struct {
	// Path is the path of the log file to use
	Path string
	// MaxSize is the size of log file before rotation, 0 for infinite.
	MaxSize int64
	// MaxAge is th duration before log rotation, zero value for infinite.
	MaxAge time.Duration
}

// SetLogFileConfig sets the log file config to be used globally.
func SetLogFileConfig(lfc *LogFileConfig) error {
	globalLock.Lock()
	defer globalLock.Unlock()

	first := true
	var w = currentLogFileWriter
	if w != nil {
		first = false
		w.lock.Lock()
		defer w.lock.Unlock()
		w.Close()
	} else {
		w = &logFileWriter{}
	}
	w.config = *lfc

	err := w.Open(time.Now())
	if err != nil {
		return err
	}

	if first {
		fileBackend := logging.NewLogBackend(w, "", 0)
		logging.SetBackend(fileBackend)

		stderrIsTerminal = false
		currentLogFileWriter = w
	}
	return nil
}

type logFileWriter struct {
	lock         sync.Mutex
	config       LogFileConfig
	file         *os.File
	currentSize  int64
	currentStart time.Time
}

func (lfw *logFileWriter) Open(at time.Time) error {
	var err error
	_, lfw.file, err = OpenLogFile(lfw.config.Path)
	if err != nil {
		return err
{'fraction_non_alphanumeric': 0.0955909596146721, 'fraction_numerical': 0.00444609114486847, 'mean_word_length': 3.32, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6154294', 'n_tokens_mistral': 948, 'n_tokens_neox': 912, 'n_words': 300}