// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
	"github.com/pingcap/log"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

// _globalP is the global ZapProperties in log
var _globalP *log.ZapProperties

// InitLogger initializes logger
func InitLogger(level string, file string) error {
	cfg := &log.Config{
		Level: level,
		File: log.FileLogConfig{
			Filename: file,
			// default rotate by size 300M in pingcap/log never delete old files
			// MaxSize:
			// MaxDays:
			// MaxBackups:
		},
	}

	var lg *zap.Logger
	var err error
	lg, _globalP, err = log.InitLogger(cfg)
	if err != nil {
		return err
	}

	// Do not log stack traces at all, as we'll get the stack trace from the
	// error itself.
	lg = lg.WithOptions(zap.AddStacktrace(zap.DPanicLevel))

	log.ReplaceGlobals(lg, _globalP)

	return nil
}

// LogHook to get the save entrys for test
type LogHook struct {
	// save the log entrys
	Entrys       []zapcore.Entry
	originLogger *zap.Logger
}

// SetUp LogHook
func (h *LogHook) SetUp() {
	h.originLogger = log.L()
	lg := log.L().WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
		h.Entrys = append(h.Entrys, entry)
		return nil
	}))

	log.ReplaceGlobals(lg, _globalP)
}

// TearDown set back the origin logger
func (h *LogHook) TearDown() {
	log.ReplaceGlobals(h.originLogger, _globalP)
}
