package log

import (
	"designs/global"
	"fmt"

	"github.com/pkg/errors"
	"go.uber.org/zap"
)

type causer interface {
	Format(s fmt.State, verb rune)
}

// 会打印error时的堆栈
func ErrorWithStack(err error, msg ...string) error {
	titleMsg := ""
	if len(msg) > 0 {
		titleMsg = msg[0]
	}
	if _, ok := err.(causer); ok {
		global.App.Log.Errorf("%s%+v", titleMsg, errors.WithStack(err))
	} else {
		global.App.Log.Error(titleMsg, err.Error(), zap.StackSkip("", 1))
	}

	return err
}

func Debug(args ...interface{}) {
	global.App.Log.Debug(args...)
}

func Info(args ...interface{}) {
	global.App.Log.Info(args...)
}

func Warn(args ...interface{}) {
	global.App.Log.Warn(args...)
}

func Error(args ...interface{}) {
	global.App.Log.Error(args...)
}

func Debugf(format string, args ...interface{}) {
	global.App.Log.Debugf(format, args...)
}

func Infof(format string, args ...interface{}) {
	global.App.Log.Infof(format, args...)
}

func Warnf(format string, args ...interface{}) {
	global.App.Log.Warnf(format, args...)
}

func Errorf(format string, args ...interface{}) {
	global.App.Log.Errorf(format, args...)
}
