Browse Source

Init commit

Joachim M. Giæver 8 years ago
parent
commit
47052a1d9f
3 changed files with 277 additions and 0 deletions
  1. 98 0
      log/config/config.go
  2. 9 0
      log/errors/errors.go
  3. 170 0
      log/log.go

+ 98 - 0
log/config/config.go

@@ -0,0 +1,98 @@
+package config
+
+import (
+	"errors"
+	"io"
+	"io/ioutil"
+	"log"
+	"os"
+	"strings"
+
+	_c "source.uit.com/mikevoets/inf-3200-1/config"
+)
+
+type LogPrefix string
+type LogLevel int
+type LogHandle io.Writer
+type LogFlag int
+
+var (
+	Trace   *log.Logger
+	Info    *log.Logger
+	Warning *log.Logger
+	Error   *log.Logger
+	Panic   *log.Logger
+)
+
+var (
+	DISCARD_HANDLE LogHandle = ioutil.Discard
+	HANDLE_TRACE   LogHandle = os.Stdout
+	HANDLE_INFO    LogHandle = os.Stdout
+	HANDLE_WARNING LogHandle = os.Stdout
+	HANDLE_ERROR   LogHandle = os.Stderr
+	HANDLE_PANIC   LogHandle = os.Stderr
+	LOG_DIR                  = _c.TmpDir + string(os.PathSeparator) + "logs"
+	FILE_EXTRA     string    = ""
+	LOG_FORMAT               = log.Ldate | log.Lmicroseconds
+)
+
+const (
+	FILE_MASK = os.O_CREATE | os.O_WRONLY | os.O_APPEND
+	FILE_PERM = 0666
+)
+
+const (
+	PREFIX_TRACE   LogPrefix = "TRACE"
+	PREFIX_INFO    LogPrefix = "INFO"
+	PREFIX_WARNING LogPrefix = "WARNING"
+	PREFIX_ERROR   LogPrefix = "ERROR"
+	PREFIX_PANIC   LogPrefix = "PANIC"
+)
+
+const (
+	TRACE   LogLevel = 1<<iota | 0
+	INFO    LogLevel = 1<<iota | TRACE
+	WARNING LogLevel = 1<<iota | INFO
+	ERROR   LogLevel = 1<<iota | WARNING
+	PANIC   LogLevel = 1<<iota | ERROR
+)
+
+var (
+	LOG_LEVEL_OUTPUT LogLevel = INFO
+	LOG_LEVEL_FILE   LogLevel = WARNING
+)
+
+func (c *LogLevel) Set(l string) error {
+
+	l = strings.ToUpper(l)
+
+	if l == "TRACE" {
+		*c = TRACE
+	} else if l == "INFO" {
+		*c = INFO
+	} else if l == "WARNING" {
+		*c = WARNING
+	} else if l == "ERROR" {
+		*c = ERROR
+	} else if l == "PANIC" {
+		*c = PANIC
+	} else {
+		return errors.New("Unknown log level: TRACE|INFO|WARNING|ERROR|PANIC")
+	}
+	return nil
+}
+
+func (c *LogLevel) String() string {
+	switch *c {
+	case TRACE:
+		return "TRACE"
+	case INFO:
+		return "INFO"
+	case WARNING:
+		return "WARNING"
+	case ERROR:
+		return "ERROR"
+	default:
+		return "PANIC"
+	}
+}

+ 9 - 0
log/errors/errors.go

@@ -0,0 +1,9 @@
+package errors
+
+import (
+    e "errors"
+)
+
+var (
+    UNKNOWN_LOGGER = e.New("Unknown logger")
+)

+ 170 - 0
log/log.go

@@ -0,0 +1,170 @@
+package log
+
+import (
+	"flag"
+	"fmt"
+	"io"
+	_log "log"
+	"os"
+	"strings"
+
+	"source.uit.com/mikevoets/inf-3200-1/tools/log/config"
+	"source.uit.com/mikevoets/inf-3200-1/tools/log/errors"
+)
+
+func log_file(p config.LogPrefix) *os.File {
+	os.MkdirAll(config.LOG_DIR, 0777)
+	f, err := os.OpenFile(
+		fmt.Sprintf("%s%c%s%s.log", config.LOG_DIR, os.PathSeparator, strings.ToLower(string(p)), config.FILE_EXTRA),
+		config.FILE_MASK,
+		config.FILE_PERM,
+	)
+
+	if err != nil {
+		_log.Fatal(err.Error())
+		return nil
+	}
+
+	return f
+}
+
+func log_new(l config.LogLevel, h config.LogHandle, p config.LogPrefix) *_log.Logger {
+
+	if (config.LOG_LEVEL_FILE&l) == config.LOG_LEVEL_FILE && (config.LOG_LEVEL_OUTPUT&l) == config.LOG_LEVEL_OUTPUT {
+		f := log_file(p)
+		return _log.New(
+			io.MultiWriter(f, h),
+			fmt.Sprintf("%s ", p),
+			config.LOG_FORMAT,
+		)
+	}
+
+	if (config.LOG_LEVEL_FILE & l) == config.LOG_LEVEL_FILE {
+		f := log_file(p)
+		return _log.New(
+			f, fmt.Sprintf("%s ", p),
+			config.LOG_FORMAT,
+		)
+	}
+
+	if (config.LOG_LEVEL_OUTPUT & l) == config.LOG_LEVEL_OUTPUT {
+		return _log.New(
+			h, fmt.Sprintf("%s ", p),
+			config.LOG_FORMAT,
+		)
+	}
+
+	return _log.New(
+		config.DISCARD_HANDLE, fmt.Sprintf("%s ", p),
+		config.LOG_FORMAT,
+	)
+}
+
+func init() {
+	flag.Var(&config.LOG_LEVEL_OUTPUT, "llo", "Minimum log level that will be written to stdout.")
+	flag.Var(&config.LOG_LEVEL_FILE, "llf", "Minimum log level that will be written to file")
+}
+
+func log(t config.LogLevel) *_log.Logger {
+	var l *_log.Logger
+	switch t {
+	case config.TRACE:
+		if config.Trace == nil {
+			config.Trace = log_new(t, config.HANDLE_TRACE, config.PREFIX_TRACE)
+		}
+		l = config.Trace
+	case config.INFO:
+		if config.Info == nil {
+			config.Info = log_new(t, config.HANDLE_INFO, config.PREFIX_INFO)
+		}
+		l = config.Info
+	case config.WARNING:
+		if config.Warning == nil {
+			config.Warning = log_new(t, config.HANDLE_WARNING, config.PREFIX_WARNING)
+		}
+		l = config.Warning
+	case config.ERROR:
+		if config.Error == nil {
+			config.Error = log_new(t, config.HANDLE_ERROR, config.PREFIX_ERROR)
+		}
+		l = config.Error
+	case config.PANIC:
+		if config.Panic == nil {
+			config.Panic = log_new(t, config.HANDLE_ERROR, config.PREFIX_PANIC)
+		}
+		l = config.Panic
+	}
+
+	if l == nil {
+		_log.Panic(errors.UNKNOWN_LOGGER.Error())
+	}
+
+	return l
+}
+
+func LLO() *config.LogLevel {
+	return &config.LOG_LEVEL_OUTPUT
+}
+
+func LLF() *config.LogLevel {
+	return &config.LOG_LEVEL_FILE
+}
+
+func Trace(m string) {
+	log(config.TRACE).Print(m)
+}
+
+func Tracef(f string, v ...interface{}) {
+	log(config.TRACE).Printf(f, v...)
+}
+
+func Traceln(v ...interface{}) {
+	log(config.TRACE).Println(v...)
+}
+
+func Info(m string) {
+	log(config.INFO).Print(m)
+}
+
+func Infof(f string, v ...interface{}) {
+	log(config.INFO).Printf(f, v...)
+}
+
+func Infoln(v ...interface{}) {
+	log(config.INFO).Println(v...)
+}
+
+func Warning(m string) {
+	log(config.WARNING).Print(m)
+}
+
+func Warningf(f string, v ...interface{}) {
+	log(config.WARNING).Printf(f, v...)
+}
+
+func Warningln(v ...interface{}) {
+	log(config.WARNING).Println(v...)
+}
+func Error(m string) {
+	log(config.ERROR).Fatal(m)
+}
+
+func Errorf(f string, v ...interface{}) {
+	log(config.ERROR).Fatalf(f, v...)
+}
+
+func Errorln(v ...interface{}) {
+	log(config.ERROR).Fatalln(v...)
+}
+
+func Panic(m string) {
+	log(config.PANIC).Panic(m)
+}
+
+func Panicf(f string, v ...interface{}) {
+	log(config.PANIC).Panicf(f, v...)
+}
+
+func Panicln(v ...interface{}) {
+	log(config.PANIC).Panicln(v...)
+}