This commit is contained in:
Andreas Neue 2016-03-12 11:10:54 +01:00
parent 9df054a1cc
commit c3edb590df
2 changed files with 99 additions and 4 deletions

20
LICENSE
View File

@ -1,8 +1,20 @@
MIT License MIT License
Copyright (c) <year> <copyright holders> Copyright (c) 2012 Andreas Neue <an@dnix.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

83
xlog.go Normal file
View File

@ -0,0 +1,83 @@
// vi:ts=4:sts=4:sw=4:noet:tw=72
package xlog
import (
"fmt"
"log"
"os"
"runtime"
)
const (
FATAL = iota
ERROR
WARNING
INFO
DEBUG
)
var levelNames = map[int]string{
FATAL: "[FAT] ",
ERROR: "[ERR] ",
WARNING: "[WRN] ",
INFO: "[INF] ",
DEBUG: "[DBG] ",
}
var logLevel int
var logger *log.Logger
type LogWriter struct {
}
func (w *LogWriter) Write(buf []byte) (int, error) {
fmt.Printf("%s", string(buf))
return len(buf), nil
}
func Init(lvl int) {
logger = log.New(&LogWriter{}, "", log.Ldate|log.Ltime)
logLevel = lvl
}
func Log(lvl int, s string, a ...interface{}) {
if lvl <= logLevel {
_, file, line, ok := runtime.Caller(2)
if !ok {
file = "???"
} else {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
}
l := fmt.Sprintf("%d", line)
logger.Printf(levelNames[lvl]+file+":"+l+": "+s, a...)
}
}
func Debug(s string, a ...interface{}) {
Log(DEBUG, s, a...)
}
func Info(s string, a ...interface{}) {
Log(INFO, s, a...)
}
func Warning(s string, a ...interface{}) {
Log(WARNING, s, a...)
}
func Error(s string, a ...interface{}) {
Log(ERROR, s, a...)
}
func Fatal(s string, a ...interface{}) {
Log(FATAL, s, a...)
os.Exit(0)
}