github.com/XiaoMi/Gaea@v1.2.5/log/xlog/util.go (about) 1 // Copyright 2019 The Gaea Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain 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, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package xlog 16 17 import ( 18 "bytes" 19 "errors" 20 "fmt" 21 "runtime" 22 "strconv" 23 "strings" 24 ) 25 26 // log level 27 const ( 28 DebugLevel = iota 29 TraceLevel 30 NoticeLevel 31 WarnLevel 32 FatalLevel 33 NoneLevel 34 ) 35 36 // log skip num 37 const ( 38 XLogDefSkipNum = 4 39 ) 40 41 var ( 42 levelTextArray = []string{ 43 DebugLevel: "DEBUG", 44 TraceLevel: "TRACE", 45 NoticeLevel: "NOTICE", 46 WarnLevel: "WARN", 47 FatalLevel: "FATAL", 48 } 49 ) 50 51 // LevelFromStr get log level from level string 52 func LevelFromStr(level string) int { 53 resultLevel := DebugLevel 54 levelLower := strings.ToLower(level) 55 switch levelLower { 56 case "debug": 57 resultLevel = DebugLevel 58 case "trace": 59 resultLevel = TraceLevel 60 case "notice": 61 resultLevel = NoticeLevel 62 case "warn": 63 resultLevel = WarnLevel 64 case "fatal": 65 resultLevel = FatalLevel 66 case "none": 67 resultLevel = NoneLevel 68 default: 69 resultLevel = NoticeLevel 70 } 71 72 return resultLevel 73 } 74 75 func getRuntimeInfo(skip int) (function, filename string, lineno int) { 76 function = "???" 77 pc, filename, lineno, ok := runtime.Caller(skip) 78 if ok { 79 function = runtime.FuncForPC(pc).Name() 80 } 81 return 82 } 83 84 func formatLog(body *string, fields ...string) string { 85 var buffer bytes.Buffer 86 for _, v := range fields { 87 buffer.WriteString("[") 88 buffer.WriteString(v) 89 buffer.WriteString("] ") 90 } 91 92 buffer.WriteString(*body) 93 buffer.WriteString("\n") 94 95 return buffer.String() 96 } 97 98 func formatValue(format string, a ...interface{}) (result string) { 99 if len(a) == 0 { 100 result = format 101 return 102 } 103 104 result = fmt.Sprintf(format, a...) 105 return 106 } 107 108 func formatLineInfo(runtime bool, functionName, filename, logText string, lineno int) string { 109 var buffer bytes.Buffer 110 if runtime { 111 buffer.WriteString("[") 112 buffer.WriteString(functionName) 113 buffer.WriteString(":") 114 115 buffer.WriteString(filename) 116 buffer.WriteString(":") 117 118 buffer.WriteString(strconv.FormatInt(int64(lineno), 10)) 119 buffer.WriteString("] ") 120 } 121 buffer.WriteString(logText) 122 123 return buffer.String() 124 } 125 126 func newError(format string, a ...interface{}) error { 127 err := fmt.Sprintf(format, a...) 128 pc, file, line, ok := runtime.Caller(1) 129 if !ok { 130 return errors.New(err) 131 } 132 133 function := runtime.FuncForPC(pc).Name() 134 msg := fmt.Sprintf("%s func:%s file:%s line:%d", 135 err, function, file, line) 136 return errors.New(msg) 137 }