|
|
|
@ -21,10 +21,11 @@ type entry = Entry
|
|
|
|
|
// 使用 Logger 处理日志.
|
|
|
|
|
type Logger struct {
|
|
|
|
|
exit func(code int) // protected by lock
|
|
|
|
|
beforeExit []func(code int) // protected by lock
|
|
|
|
|
beforeExitFns []func() // protected by lock
|
|
|
|
|
levelProcessors levelProcessors // protected by lock
|
|
|
|
|
entry
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
|
exitOnce sync.Once
|
|
|
|
|
level atomic.Int32
|
|
|
|
|
reportStackLevel atomic.Int32
|
|
|
|
|
callerSkip atomic.Int32
|
|
|
|
@ -36,7 +37,7 @@ type Logger struct {
|
|
|
|
|
func (logger *Logger) AddProcessor(levels []Level, processor EntryProcessor) {
|
|
|
|
|
logger.lock.Lock()
|
|
|
|
|
for _, level := range levels {
|
|
|
|
|
logger.levelProcessors[level+LevelOffset] = append(logger.levelProcessors[level+LevelOffset], processor)
|
|
|
|
|
logger.levelProcessors[level+levelOffset] = append(logger.levelProcessors[level+levelOffset], processor)
|
|
|
|
|
}
|
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
}
|
|
|
|
@ -99,7 +100,7 @@ func (logger *Logger) SetReportStackLevel(level Level) {
|
|
|
|
|
func (logger *Logger) Reset() {
|
|
|
|
|
logger.lock.Lock()
|
|
|
|
|
logger.exit = nil
|
|
|
|
|
logger.beforeExit = nil
|
|
|
|
|
logger.beforeExitFns = nil
|
|
|
|
|
logger.levelProcessors = levelProcessors{}
|
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
|
|
|
|
@ -110,16 +111,27 @@ func (logger *Logger) Reset() {
|
|
|
|
|
logger.SetReportStack(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exit 退出程序, 执行的具体过程可以通过 SetExit 指定
|
|
|
|
|
// BeforeExit 按照先添加后执行的顺序执行 AddBeforeExit 添加的函数,
|
|
|
|
|
// AddBeforeExit 只会执行1次.
|
|
|
|
|
func (logger *Logger) BeforeExit() {
|
|
|
|
|
logger.exitOnce.Do(func() {
|
|
|
|
|
logger.lock.RLock()
|
|
|
|
|
beforeExitFns := make([]func(), len(logger.beforeExitFns))
|
|
|
|
|
copy(beforeExitFns, logger.beforeExitFns)
|
|
|
|
|
logger.lock.RUnlock()
|
|
|
|
|
for i := len(beforeExitFns) - 1; i >= 0; i-- {
|
|
|
|
|
beforeExitFns[i]()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exit 执行 BeforeExit 后退出程序, 执行的退出函数可以通过 SetExit 指定,
|
|
|
|
|
// 当没有通过 SetExit 指定退出函数时调用 [os.Exit].
|
|
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
|
logger.BeforeExit()
|
|
|
|
|
logger.lock.RLock()
|
|
|
|
|
exit := logger.exit
|
|
|
|
|
beforeExit := make([]func(int), len(logger.beforeExit))
|
|
|
|
|
copy(beforeExit, logger.beforeExit)
|
|
|
|
|
logger.lock.RUnlock()
|
|
|
|
|
for i := len(beforeExit) - 1; i >= 0; i-- {
|
|
|
|
|
beforeExit[i](code)
|
|
|
|
|
}
|
|
|
|
|
if exit == nil {
|
|
|
|
|
exit = os.Exit
|
|
|
|
|
}
|
|
|
|
@ -135,16 +147,16 @@ func (logger *Logger) SetExit(fn func(code int)) {
|
|
|
|
|
|
|
|
|
|
// AddBeforeExit 增加 Exit 在调用 SetExit 指定的函数前执行的函数,
|
|
|
|
|
// 先增加的后执行.
|
|
|
|
|
func (logger *Logger) AddBeforeExit(fn ...func(code int)) {
|
|
|
|
|
func (logger *Logger) AddBeforeExit(fn ...func()) {
|
|
|
|
|
logger.lock.Lock()
|
|
|
|
|
logger.beforeExit = append(logger.beforeExit, fn...)
|
|
|
|
|
logger.beforeExitFns = append(logger.beforeExitFns, fn...)
|
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
|
|
|
|
|
logger.lock.RLock()
|
|
|
|
|
defer logger.lock.RUnlock()
|
|
|
|
|
return logger.levelProcessors[level+LevelOffset]
|
|
|
|
|
return logger.levelProcessors[level+levelOffset]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (logger *Logger) newEntry() Entry {
|
|
|
|
|