From c3edb590dfcabc7390c0a73e8f269be212a2f7be Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Sat, 12 Mar 2016 11:10:54 +0100 Subject: [PATCH] ... --- LICENSE | 20 +++++++++++--- xlog.go | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 xlog.go diff --git a/LICENSE b/LICENSE index 472ac23..736435b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,8 +1,20 @@ MIT License -Copyright (c) +Copyright (c) 2012 Andreas Neue -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. diff --git a/xlog.go b/xlog.go new file mode 100644 index 0000000..f39f1d3 --- /dev/null +++ b/xlog.go @@ -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) +}