github.com/boki/go-xmp@v1.0.1/xmp/log.go (about) 1 // Copyright (c) 2017-2018 Alexander Eichhorn 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package xmp 16 17 import ( 18 "log" 19 "strconv" 20 "strings" 21 ) 22 23 type LogLevelType int 24 type LogType struct { 25 bool 26 } 27 28 type Logger interface { 29 Error(v ...interface{}) 30 Errorf(s string, v ...interface{}) 31 Warn(v ...interface{}) 32 Warnf(s string, v ...interface{}) 33 Info(v ...interface{}) 34 Infof(s string, v ...interface{}) 35 Debug(v ...interface{}) 36 Debugf(s string, v ...interface{}) 37 } 38 39 const ( 40 LogLevelInvalid LogLevelType = iota 41 LogLevelDebug 42 LogLevelInfo 43 LogLevelWarning 44 LogLevelError 45 ) 46 47 func (l LogLevelType) Prefix() string { 48 switch l { 49 case LogLevelDebug: 50 return "Debug:" 51 case LogLevelInfo: 52 return "Info:" 53 case LogLevelWarning: 54 return "Warn:" 55 case LogLevelError: 56 return "Error:" 57 default: 58 return strconv.Itoa(int(l)) 59 } 60 } 61 62 var ( 63 logLevel LogLevelType = LogLevelWarning 64 Log Logger = &LogType{} 65 ) 66 67 // package level logging control 68 func LogLevel() LogLevelType { 69 return logLevel 70 } 71 72 func SetLogLevel(l LogLevelType) { 73 logLevel = l 74 } 75 76 func SetLogger(v Logger) { 77 Log = v 78 } 79 80 // internal 81 func defaultLogger(format string, args ...interface{}) { 82 if !strings.HasSuffix(format, "\n") { 83 format = format + "\n" 84 } 85 log.Printf(format, args...) 86 } 87 88 func output(lvl LogLevelType, v ...interface{}) { 89 m := append(make([]interface{}, 0, len(v)+1), lvl.Prefix()) 90 m = append(m, v...) 91 defaultLogger("%s", m...) 92 } 93 94 func outputf(lvl LogLevelType, s string, v ...interface{}) { 95 s = strings.Join([]string{"%s ", s}, "") 96 m := append(make([]interface{}, 0, len(v)+1), lvl.Prefix()) 97 m = append(m, v...) 98 defaultLogger(s, m...) 99 } 100 101 func (x LogType) Error(v ...interface{}) { 102 if logLevel > LogLevelError { 103 return 104 } 105 output(LogLevelError, v...) 106 } 107 108 func (x LogType) Errorf(s string, v ...interface{}) { 109 if logLevel > LogLevelError { 110 return 111 } 112 outputf(LogLevelError, s, v...) 113 } 114 115 func (x LogType) Warn(v ...interface{}) { 116 if logLevel > LogLevelWarning { 117 return 118 } 119 output(LogLevelWarning, v...) 120 } 121 122 func (x LogType) Warnf(s string, v ...interface{}) { 123 if logLevel > LogLevelWarning { 124 return 125 } 126 outputf(LogLevelWarning, s, v...) 127 } 128 129 func (x LogType) Info(v ...interface{}) { 130 if logLevel > LogLevelInfo { 131 return 132 } 133 output(LogLevelInfo, v...) 134 } 135 136 func (x LogType) Infof(s string, v ...interface{}) { 137 if logLevel > LogLevelInfo { 138 return 139 } 140 outputf(LogLevelInfo, s, v...) 141 } 142 143 func (x LogType) Debug(v ...interface{}) { 144 if logLevel > LogLevelDebug { 145 return 146 } 147 output(LogLevelDebug, v...) 148 } 149 150 func (x LogType) Debugf(s string, v ...interface{}) { 151 if logLevel > LogLevelDebug { 152 return 153 } 154 outputf(LogLevelDebug, s, v...) 155 } 156 157 // package level forwarders to the real logger implementation 158 // func Error(v ...interface{}) { Log.Error(v...) } 159 // func Errorf(s string, v ...interface{}) { Log.Errorf(s, v...) } 160 // func Warn(v ...interface{}) { Log.Warn(v...) } 161 // func Warnf(s string, v ...interface{}) { Log.Warnf(s, v...) } 162 // func Info(v ...interface{}) { Log.Info(v...) } 163 // func Infof(s string, v ...interface{}) { Log.Infof(s, v...) } 164 // func Debug(v ...interface{}) { Log.Debug(v...) } 165 // func Debugf(s string, v ...interface{}) { Log.Debugf(s, v...) }