github.com/fibonacci1729/glide@v0.0.0-20160513190140-d9640dc62d0f/msg/msg.go (about) 1 package msg 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "strings" 8 "sync" 9 ) 10 11 // Messenger provides the underlying implementation that displays output to 12 // users. 13 type Messenger struct { 14 sync.Mutex 15 16 // Quiet, if true, suppresses chatty levels, like Info. 17 Quiet bool 18 19 // IsDebugging, if true, shows verbose levels, like Debug. 20 IsDebugging bool 21 22 // NoColor, if true, will not use color in the output. 23 NoColor bool 24 25 // Stdout is the location where this prints output. 26 Stdout io.Writer 27 28 // Stderr is the location where this prints logs. 29 Stderr io.Writer 30 31 // PanicOnDie if true Die() will panic instead of exiting. 32 PanicOnDie bool 33 34 // The default exit code to use when dyping 35 ecode int 36 37 // If an error was been sent. 38 hasErrored bool 39 } 40 41 // NewMessenger creates a default Messenger to display output. 42 func NewMessenger() *Messenger { 43 m := &Messenger{ 44 Quiet: false, 45 IsDebugging: false, 46 NoColor: false, 47 Stdout: os.Stdout, 48 Stderr: os.Stderr, 49 PanicOnDie: false, 50 ecode: 1, 51 } 52 53 return m 54 } 55 56 // Default contains a default Messenger used by package level functions 57 var Default = NewMessenger() 58 59 // Info logs information 60 func (m *Messenger) Info(msg string, args ...interface{}) { 61 if m.Quiet { 62 return 63 } 64 prefix := m.Color(Green, "[INFO] ") 65 m.Msg(prefix+msg, args...) 66 } 67 68 // Info logs information using the Default Messenger 69 func Info(msg string, args ...interface{}) { 70 Default.Info(msg, args...) 71 } 72 73 // Debug logs debug information 74 func (m *Messenger) Debug(msg string, args ...interface{}) { 75 if m.Quiet || !m.IsDebugging { 76 return 77 } 78 prefix := "[DEBUG] " 79 Msg(prefix+msg, args...) 80 } 81 82 // Debug logs debug information using the Default Messenger 83 func Debug(msg string, args ...interface{}) { 84 Default.Debug(msg, args...) 85 } 86 87 // Warn logs a warning 88 func (m *Messenger) Warn(msg string, args ...interface{}) { 89 prefix := m.Color(Yellow, "[WARN] ") 90 m.Msg(prefix+msg, args...) 91 } 92 93 // Warn logs a warning using the Default Messenger 94 func Warn(msg string, args ...interface{}) { 95 Default.Warn(msg, args...) 96 } 97 98 // Err logs an error. 99 func (m *Messenger) Err(msg string, args ...interface{}) { 100 prefix := m.Color(Red, "[ERROR] ") 101 m.Msg(prefix+msg, args...) 102 m.hasErrored = true 103 } 104 105 // Err logs anderror using the Default Messenger 106 func Err(msg string, args ...interface{}) { 107 Default.Err(msg, args...) 108 } 109 110 // Die prints an error message and immediately exits the application. 111 // If PanicOnDie is set to true a panic will occur instead of os.Exit being 112 // called. 113 func (m *Messenger) Die(msg string, args ...interface{}) { 114 m.Err(msg, args...) 115 if m.PanicOnDie { 116 panic("trapped a Die() call") 117 } 118 os.Exit(m.ecode) 119 } 120 121 // Die prints an error message and immediately exits the application using the 122 // Default Messenger. If PanicOnDie is set to true a panic will occur instead of 123 // os.Exit being called. 124 func Die(msg string, args ...interface{}) { 125 Default.Die(msg, args...) 126 } 127 128 // ExitCode sets the exit code used by Die. 129 // 130 // The default is 1. 131 // 132 // Returns the old error code. 133 func (m *Messenger) ExitCode(e int) int { 134 m.Lock() 135 old := m.ecode 136 m.ecode = e 137 m.Unlock() 138 return old 139 } 140 141 // ExitCode sets the exit code used by Die using the Default Messenger. 142 // 143 // The default is 1. 144 // 145 // Returns the old error code. 146 func ExitCode(e int) int { 147 return Default.ExitCode(e) 148 } 149 150 // Msg prints a message with optional arguments, that can be printed, of 151 // varying types. 152 func (m *Messenger) Msg(msg string, args ...interface{}) { 153 // When operations in Glide are happening concurrently messaging needs to be 154 // locked to avoid displaying one message in the middle of another one. 155 m.Lock() 156 defer m.Unlock() 157 158 // Get rid of the annoying fact that messages need \n at the end, but do 159 // it in a backward compatible way. 160 if !strings.HasSuffix(msg, "\n") { 161 msg += "\n" 162 } 163 164 if len(args) == 0 { 165 fmt.Fprint(m.Stderr, msg) 166 } else { 167 fmt.Fprintf(m.Stderr, msg, args...) 168 } 169 } 170 171 // Msg prints a message with optional arguments, that can be printed, of 172 // varying types using the Default Messenger. 173 func Msg(msg string, args ...interface{}) { 174 Default.Msg(msg, args...) 175 } 176 177 // Puts formats a message and then prints to Stdout. 178 // 179 // It does not prefix the message, does not color it, or otherwise decorate it. 180 // 181 // It does add a line feed. 182 func (m *Messenger) Puts(msg string, args ...interface{}) { 183 // When operations in Glide are happening concurrently messaging needs to be 184 // locked to avoid displaying one message in the middle of another one. 185 m.Lock() 186 defer m.Unlock() 187 188 fmt.Fprintf(m.Stdout, msg, args...) 189 fmt.Fprintln(m.Stdout) 190 } 191 192 // Puts formats a message and then prints to Stdout using the Default Messenger. 193 // 194 // It does not prefix the message, does not color it, or otherwise decorate it. 195 // 196 // It does add a line feed. 197 func Puts(msg string, args ...interface{}) { 198 Default.Puts(msg, args...) 199 } 200 201 // Print prints exactly the string given. 202 // 203 // It prints to Stdout. 204 func (m *Messenger) Print(msg string) { 205 // When operations in Glide are happening concurrently messaging needs to be 206 // locked to avoid displaying one message in the middle of another one. 207 m.Lock() 208 defer m.Unlock() 209 210 fmt.Fprint(m.Stdout, msg) 211 } 212 213 // Print prints exactly the string given using the Default Messenger. 214 // 215 // It prints to Stdout. 216 func Print(msg string) { 217 Default.Print(msg) 218 } 219 220 // HasErrored returns if Error has been called. 221 // 222 // This is useful if you want to known if Error was called to exit with a 223 // non-zero exit code. 224 func (m *Messenger) HasErrored() bool { 225 return m.hasErrored 226 } 227 228 // HasErrored returns if Error has been called on the Default Messenger. 229 // 230 // This is useful if you want to known if Error was called to exit with a 231 // non-zero exit code. 232 func HasErrored() bool { 233 return Default.HasErrored() 234 } 235 236 // Color returns a string in a certain color if colors are enabled and 237 // available on that platform. 238 func Color(code, msg string) string { 239 return Default.Color(code, msg) 240 }