github.com/splunk/dan1-qbec@v0.7.3/internal/sio/log.go (about) 1 /* 2 Copyright 2019 Splunk Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package sio provides logging functions with optional colorization. 18 package sio 19 20 import ( 21 "fmt" 22 "io" 23 "os" 24 "strings" 25 ) 26 27 const esc = "\x1b[" 28 29 const ( 30 codeReset = esc + "0m" // reset all attributes 31 colorRed = esc + "31m" // set foreground to red 32 colorMagenta = esc + "35m" // set foreground to magenta 33 attrBold = esc + "1m" // set bold 34 attrDim = esc + "2m" // set dim 35 unicodeX = "\u2718" // X mark 36 ) 37 38 // EnableColors enables colorized output when set. True by default. 39 var EnableColors = true 40 41 func startColors(codes ...string) { 42 if EnableColors { 43 fmt.Fprint(Output, strings.Join(codes, "")) 44 } 45 } 46 47 func reset() { 48 if EnableColors { 49 fmt.Fprint(Output, codeReset) 50 } 51 } 52 53 // Output is the writer to which all prompts, errors and messages go. 54 // This is set to standard error by default. 55 var Output io.Writer = os.Stderr 56 57 // Println prints the supplied arguments to the standard writer 58 func Println(args ...interface{}) { 59 fmt.Fprintln(Output, args...) 60 } 61 62 // Printf prints the supplied arguments to the standard writer. 63 func Printf(format string, args ...interface{}) { 64 fmt.Fprintf(Output, format, args...) 65 } 66 67 // Noticeln prints the supplied arguments in a way that they will be noticed. 68 // Use sparingly. 69 func Noticeln(args ...interface{}) { 70 startColors(attrBold) 71 fmt.Fprintln(Output, args...) 72 reset() 73 } 74 75 // Noticef prints the supplied arguments in a way that they will be noticed. 76 // Use sparingly. 77 func Noticef(format string, args ...interface{}) { 78 startColors(attrBold) 79 fmt.Fprintf(Output, format, args...) 80 reset() 81 } 82 83 // Debugln prints the supplied arguments to the standard writer, de-emphasized 84 func Debugln(args ...interface{}) { 85 startColors(attrDim) 86 fmt.Fprintln(Output, args...) 87 reset() 88 } 89 90 // Debugf prints the supplied arguments to the standard writer, de-emphasized. 91 func Debugf(format string, args ...interface{}) { 92 startColors(attrDim) 93 fmt.Fprintf(Output, format, args...) 94 reset() 95 } 96 97 // Warnln prints the supplied arguments to the standard writer 98 // with some indication for a warning. 99 func Warnln(args ...interface{}) { 100 startColors(colorMagenta, attrBold) 101 fmt.Fprint(Output, "[warn] ") 102 fmt.Fprintln(Output, args...) 103 reset() 104 } 105 106 // Warnf prints the supplied arguments to the standard writer 107 // with some indication for a warning. 108 func Warnf(format string, args ...interface{}) { 109 startColors(colorMagenta, attrBold) 110 fmt.Fprint(Output, "[warn] ") 111 fmt.Fprintf(Output, format, args...) 112 reset() 113 } 114 115 // Errorln prints the supplied arguments to the standard writer 116 // with some indication that an error has occurred. 117 func Errorln(args ...interface{}) { 118 startColors(colorRed, attrBold) 119 fmt.Fprint(Output, unicodeX+" ") 120 fmt.Fprintln(Output, args...) 121 reset() 122 } 123 124 // Errorf prints the supplied arguments to the standard writer 125 // with some indication that an error has occurred. 126 func Errorf(format string, args ...interface{}) { 127 startColors(colorRed, attrBold) 128 fmt.Fprint(Output, unicodeX+" ") 129 fmt.Fprintf(Output, format, args...) 130 reset() 131 }