|
|
|
@ -11,6 +11,12 @@ import (
|
|
|
|
|
"b612.me/starmap"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var archMap starmap.StarMapKV
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
archMap = starmap.NewStarMap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Archive interface {
|
|
|
|
|
ShouldArchiveNow(string, os.FileInfo) bool
|
|
|
|
|
NextLogFilePath(string, os.FileInfo) string
|
|
|
|
@ -42,15 +48,15 @@ func SetLogFile(path string, logger *StarLogger, appendMode bool) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if starmap.MustGet(logger.logcore.id) != nil {
|
|
|
|
|
if archMap.MustGet(logger.logcore.id) != nil {
|
|
|
|
|
logger.SetSwitching(true)
|
|
|
|
|
err := starmap.MustGet(logger.logcore.id).(logfileinfo).pointer.Close()
|
|
|
|
|
err := archMap.MustGet(logger.logcore.id).(logfileinfo).pointer.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.logcore.output = nil
|
|
|
|
|
logger.SetSwitching(false)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = starmap.Delete(logger.logcore.id)
|
|
|
|
|
err = archMap.Delete(logger.logcore.id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.logcore.output = nil
|
|
|
|
|
logger.SetSwitching(false)
|
|
|
|
@ -58,7 +64,7 @@ func SetLogFile(path string, logger *StarLogger, appendMode bool) error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = starmap.Store(logger.logcore.id, logfileinfo{
|
|
|
|
|
err = archMap.Store(logger.logcore.id, logfileinfo{
|
|
|
|
|
fullpath: fullpath,
|
|
|
|
|
pointer: fp,
|
|
|
|
|
})
|
|
|
|
@ -74,16 +80,15 @@ func SetLogFile(path string, logger *StarLogger, appendMode bool) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Close(logger *StarLogger) error {
|
|
|
|
|
defer logger.SetSwitching(false)
|
|
|
|
|
if starmap.MustGet(logger.logcore.id) != nil {
|
|
|
|
|
func CloseWithSwitching(logger *StarLogger) error {
|
|
|
|
|
if archMap.MustGet(logger.logcore.id) != nil {
|
|
|
|
|
logger.SetSwitching(true)
|
|
|
|
|
err := starmap.MustGet(logger.logcore.id).(*os.File).Close()
|
|
|
|
|
err := archMap.MustGet(logger.logcore.id).(*os.File).Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.logcore.output = nil
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = starmap.Delete(logger.logcore.id)
|
|
|
|
|
err = archMap.Delete(logger.logcore.id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -91,19 +96,24 @@ func Close(logger *StarLogger) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Close(logger *StarLogger) error {
|
|
|
|
|
defer logger.SetSwitching(false)
|
|
|
|
|
return CloseWithSwitching(logger)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetLogFileInfo(logger *StarLogger) (os.FileInfo, error) {
|
|
|
|
|
if starmap.MustGet(logger.logcore.id) != nil {
|
|
|
|
|
return starmap.MustGet(logger.logcore.id).(logfileinfo).pointer.Stat()
|
|
|
|
|
if archMap.MustGet(logger.logcore.id) != nil {
|
|
|
|
|
return archMap.MustGet(logger.logcore.id).(logfileinfo).pointer.Stat()
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New("logger don't have a register logfile")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func StartArchive(logger *StarLogger, arch Archive) error {
|
|
|
|
|
if starmap.MustGet("arch"+logger.logcore.id) != nil {
|
|
|
|
|
if archMap.MustGet("arch"+logger.logcore.id) != nil {
|
|
|
|
|
return errors.New("already running")
|
|
|
|
|
}
|
|
|
|
|
stopChan := make(chan int)
|
|
|
|
|
starmap.Store("arch"+logger.logcore.id, stopChan)
|
|
|
|
|
archMap.Store("arch"+logger.logcore.id, stopChan)
|
|
|
|
|
go func(stopChan chan int, arch Archive, logger *StarLogger) {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
@ -116,11 +126,11 @@ func StartArchive(logger *StarLogger, arch Archive) error {
|
|
|
|
|
logger.Errorf("cannot get log file info,reason is %v\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if starmap.MustGet(logger.logcore.id) == nil {
|
|
|
|
|
if archMap.MustGet(logger.logcore.id) == nil {
|
|
|
|
|
logger.Errorf("cannot get log core info from the map:no such keys\n")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
fullpath := starmap.MustGet(logger.logcore.id).(logfileinfo).fullpath
|
|
|
|
|
fullpath := archMap.MustGet(logger.logcore.id).(logfileinfo).fullpath
|
|
|
|
|
if !arch.ShouldArchiveNow(fullpath, fileinfo) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
@ -154,17 +164,17 @@ func StartArchive(logger *StarLogger, arch Archive) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsArchiveRun(logger *StarLogger) bool {
|
|
|
|
|
if starmap.MustGet("arch"+logger.logcore.id) == nil {
|
|
|
|
|
if archMap.MustGet("arch"+logger.logcore.id) == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func StopArchive(logger *StarLogger) {
|
|
|
|
|
if starmap.MustGet("arch"+logger.logcore.id) == nil {
|
|
|
|
|
if archMap.MustGet("arch"+logger.logcore.id) == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
starmap.MustGet("arch" + logger.logcore.id).(chan int) <- 1
|
|
|
|
|
archMap.MustGet("arch" + logger.logcore.id).(chan int) <- 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ArchiveByDate struct {
|
|
|
|
|