Merge branch 'newgen' of b612/starlog into master

master
兔子 4 years ago committed by Gogs
commit 70bd67381c

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

@ -10,6 +10,31 @@ import (
"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) {
logger.mu.Lock()
defer logger.mu.Unlock()

@ -1,6 +1,7 @@
package starlog
import (
"fmt"
"io"
"math/rand"
"sync"
@ -197,10 +198,37 @@ func Panicln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Fatal(str...)
Std.Fatalln(str...)
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) {
Std.SetWriter(wr)
}

@ -173,3 +173,15 @@ func (logger *StarLogger) Fatalf(format string, str ...interface{}) {
func (logger *StarLogger) Fatalln(str ...interface{}) {
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