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