github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/output/rotation/trigger_by_size.go (about)

     1  package rotation
     2  
     3  import "os"
     4  
     5  type TriggerBySize struct {
     6  	SizeInKB int64
     7  }
     8  
     9  func (trigger *TriggerBySize) UpdateStat(stat interface{}, file *os.File, buf []byte) (interface{}, bool, error) {
    10  	var size int64
    11  	if stat == nil {
    12  		fileInfo, err := file.Stat()
    13  		if err != nil {
    14  			return nil, false, err
    15  		}
    16  		size = fileInfo.Size()
    17  	} else {
    18  		size = stat.(int64)
    19  		size += int64(len(buf))
    20  	}
    21  	triggered := false
    22  	if size >= trigger.SizeInKB*1024 {
    23  		triggered = true
    24  	}
    25  	return size, triggered, nil
    26  }