// Copyright (C) 2019 Marius Schellenberger

package log

import (
	"git.giftfish.de/ston1th/docstore/pkg/core"
	stdlog "log"
	"os"
	"path/filepath"
)

var (
	log   = stdlog.New(os.Stdout, "", stdlog.LstdFlags)
	debug = false
)

func InitLogger(cfg core.Config) {
	debug = cfg.Debug
	if cfg.LogFile == "-" {
		return
	}
	f, err := os.OpenFile(filepath.Join(cfg.DataDir, cfg.LogFile), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640)
	if err != nil {
		stdlog.Fatal(err)
	}
	stdlog.SetOutput(f)
	log = stdlog.New(f, "", stdlog.LstdFlags)
}

func Println(v ...interface{}) {
	log.Println(v...)
}

func Printf(fmt string, v ...interface{}) {
	log.Printf(fmt, v...)
}

func Fatal(v ...interface{}) {
	log.Fatal(v...)
}

func Debug(v ...interface{}) {
	if debug {
		log.Println(append([]interface{}{"debug:"}, v...)...)
	}
}

func Debugf(fmt string, v ...interface{}) {
	if debug {
		log.Printf("debug: "+fmt, v...)
	}
}
