package shared

import (
	"io"
	"log"
	"os"
	"path"
)

var defLogger *log.Logger
var defCloser func()

func InitLogger() {
	name, _ := os.Executable()
	base := path.Base(name)

	logfile, _ := os.OpenFile(base+".log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
	defCloser = func() {
		logfile.Close()
	}
	defLogger = log.New(io.MultiWriter(logfile, os.Stdout), "", log.LstdFlags)
}

func Logger() *log.Logger {
	return defLogger
}

func CloseLogger() {
	if defCloser != nil {
		defCloser()
	}
}
