package mq

import (
	"fmt"
	"github.com/apache/rocketmq-client-go/v2/primitive"
)

type Logger interface {
	// Debugf logs a formatted debugging message.
	Debugf(format string, args ...interface{})

	// Infof logs a formatted informational message.
	Infof(format string, args ...interface{})

	// Warnf logs a formatted warning message.
	Warnf(format string, args ...interface{})

	// Errorf logs a formatted error message.
	Errorf(format string, args ...interface{})
}

func init() {
	SetRecoverLogger(nil)
}

// SetRecoverLogger 设置RocketMQ的recover的处理函数日志，不设置会导致协程panic后程序退出
func SetRecoverLogger(logger Logger) {
	SetRecoverHandler(func(err any) {
		if logger == nil {
			fmt.Printf("rocketmq 发生 panic：%+v\n", err)
			return
		}
		logger.Errorf("rocketmq 发生 panic：%+v", err)
	})
}

// SetRecoverHandler 设置RocketMQ的recover的处理函数，不设置会导致协程panic后程序退出
func SetRecoverHandler(fn func(any)) {
	primitive.PanicHandler = fn
}
