//go:build stub
// +build stub

package log

import (
	"fmt"
	"os"

	"chaitin.cn/dev/go/log"
	"chaitin.cn/patronus/safeline-2/management/webserver/pkg/config"
)

func GetLogger(name string) *log.Logger {
	return log.GetLogger(name)
}

func LoadLogLevel() {
	lv, _ := log.ParseLevel(config.GlobalConfig.Log.Level)
	log.SetLevel(log.AllLoggers, lv)
}

func SetLogFormatter() {
	formatter := &log.TextFormatter{
		FullTimestamp:   true,
		TimestampFormat: "2006/01/02 15:04:05",
	}
	log.SetFormatter(log.AllLoggers, formatter)
}

func InitLogger() error {
	switch config.GlobalConfig.Log.Output {
	case "stdout":
		log.SetOutput(log.AllLoggers, os.Stdout)
	case "stderr":
		log.SetOutput(log.AllLoggers, os.Stderr)
	default:
		if config.GlobalConfig.Log.Output != "" {
			fp, err := os.OpenFile(config.GlobalConfig.Log.Output, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
			if err != nil {
				return fmt.Errorf("failed to open log file: %s", err.Error())
			}
			log.SetOutput(log.AllLoggers, fp)
		}
	}

	LoadLogLevel()
	return nil
}

type RuntimeHook struct{}

func NewRuntimeHook() *RuntimeHook {
	return &RuntimeHook{}
}
