compat with v2.0

pull/2/head
兔子 3 years ago
parent 623cc55dbd
commit 044ca3e36a

@ -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 {

@ -10,6 +10,31 @@ import (
"time" "time"
) )
func generateCoreLogStr(skip int, logstr string) string {
var line int = 0
var funcname, fileName string
now := time.Now()
pc, fName, codeln, ok := runtime.Caller(skip)
if !ok {
return ""
}
line = codeln
funcname = runtime.FuncForPC(pc).Name()
funcname = filepath.Ext(funcname)
funcname = strings.TrimPrefix(funcname, ".")
fileName = filepath.Base(fName)
y, m, d := now.Date()
h, i, s := now.Clock()
micro := now.Nanosecond() / 1e3
logStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%06d", y, m, d, h, i, s, micro)
logStr += " " + fileName + ":" + strconv.Itoa(line)
logStr += " <" + funcname + ">"
logStr += " " + logstr
return logStr
}
func (logger *starlog) build(thread string, isStd bool, handler func([]Attr, string), level int, logDetail string) { func (logger *starlog) build(thread string, isStd bool, handler func([]Attr, string), level int, logDetail string) {
logger.mu.Lock() logger.mu.Lock()
defer logger.mu.Unlock() defer logger.mu.Unlock()

@ -1,6 +1,7 @@
package starlog package starlog
import ( import (
"fmt"
"io" "io"
"math/rand" "math/rand"
"sync" "sync"
@ -197,10 +198,37 @@ func Panicln(str ...interface{}) {
stdmu.Lock() stdmu.Lock()
defer stdmu.Unlock() defer stdmu.Unlock()
Std.isStd = true Std.isStd = true
Std.Fatal(str...) Std.Fatalln(str...)
Std.isStd = false Std.isStd = false
} }
func Print(str ...interface{}) {
Std.Print(str...)
}
func Printf(format string, str ...interface{}) {
Std.Printf(format, str...)
}
func Println(str ...interface{}) {
Std.Println(str...)
}
func StdPrint(attr []Attr, str ...interface{}) {
strs := fmt.Sprint(str...)
NewColor(attr...).Fprint(stdScreen, strs)
}
func StdPrintf(attr []Attr, format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
NewColor(attr...).Fprint(stdScreen, strs)
}
func StdPrintln(attr []Attr, str ...interface{}) {
strs := fmt.Sprintln(str...)
NewColor(attr...).Fprint(stdScreen, strs)
}
func SetWriter(wr io.Writer) { func SetWriter(wr io.Writer) {
Std.SetWriter(wr) Std.SetWriter(wr)
} }

@ -173,3 +173,15 @@ func (logger *StarLogger) Fatalf(format string, str ...interface{}) {
func (logger *StarLogger) Fatalln(str ...interface{}) { func (logger *StarLogger) Fatalln(str ...interface{}) {
logger.logcore.Fatalln(logger.thread, logger.isStd, logger.handlerFunc, str...) logger.logcore.Fatalln(logger.thread, logger.isStd, logger.handlerFunc, str...)
} }
func (logger *StarLogger) Print(str ...interface{}) {
logger.logcore.Print(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func (logger *StarLogger) Printf(format string, str ...interface{}) {
logger.logcore.Printf(logger.thread, logger.isStd, logger.handlerFunc, format, str...)
}
func (logger *StarLogger) Println(str ...interface{}) {
logger.logcore.Println(logger.thread, logger.isStd, logger.handlerFunc, str...)
}

Loading…
Cancel
Save