54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
|
|
package starlog
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"b612.me/starlog/internal/rotatemanage"
|
||
|
|
)
|
||
|
|
|
||
|
|
type RotateManageOptions struct {
|
||
|
|
MaxBackups int
|
||
|
|
MaxAge time.Duration
|
||
|
|
Compress bool
|
||
|
|
Pattern string
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewManagedRotateArchive(policy RotatePolicy, checkInterval int64, options RotateManageOptions) *RotatePolicyArchive {
|
||
|
|
archive := NewRotatePolicyArchive(policy, checkInterval)
|
||
|
|
return WithRotateManageOptions(archive, options)
|
||
|
|
}
|
||
|
|
|
||
|
|
func WithRotateManageOptions(archive *RotatePolicyArchive, options RotateManageOptions) *RotatePolicyArchive {
|
||
|
|
if archive == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
previous := archive.HookAfterArchive()
|
||
|
|
archive.SetHookAfterArchive(func(logger *StarLogger, archivePath string, currentPath string, info os.FileInfo) error {
|
||
|
|
if previous != nil {
|
||
|
|
if err := previous(logger, archivePath, currentPath, info); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ApplyRotateManageOptions(archivePath, currentPath, options)
|
||
|
|
})
|
||
|
|
return archive
|
||
|
|
}
|
||
|
|
|
||
|
|
func ApplyRotateManageOptions(archivePath string, currentPath string, options RotateManageOptions) error {
|
||
|
|
return rotatemanage.Apply(archivePath, currentPath, toInternalRotateManageOptions(options))
|
||
|
|
}
|
||
|
|
|
||
|
|
func toInternalRotateManageOptions(options RotateManageOptions) rotatemanage.Options {
|
||
|
|
return rotatemanage.Options{
|
||
|
|
MaxBackups: options.MaxBackups,
|
||
|
|
MaxAge: options.MaxAge,
|
||
|
|
Compress: options.Compress,
|
||
|
|
Pattern: options.Pattern,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func isManagedBackupName(name string, base string, stem string, pattern string) (bool, error) {
|
||
|
|
return rotatemanage.IsManagedBackupName(name, base, stem, pattern)
|
||
|
|
}
|