github.com/vmware/transport-go@v1.3.4/log/logger.go (about) 1 // Copyright 2019-2020 VMware, Inc. 2 // SPDX-License-Identifier: BSD-2-Clause 3 4 package log 5 6 import ( 7 "fmt" 8 "github.com/fatih/color" 9 "os" 10 "strings" 11 ) 12 13 // These flags have to be set from the "opts" module in each sewing-machine tool 14 15 var WarnFlag = true 16 var TraceFlag = true 17 var DebugFlag = true 18 var VerboseFlag = true 19 var RecoverOnError = true 20 var Version = "" 21 22 // Print warnings 23 func Warn(format string, arg ...interface{}) { 24 color.NoColor = false 25 color.Set(color.FgHiMagenta) 26 if !WarnFlag { 27 fmt.Printf("⚠️🚨 WARNING: "+format, arg...) 28 } 29 color.Unset() 30 } 31 32 // Print traces 33 func Trace(format string, arg ...interface{}) { 34 color.NoColor = false 35 color.Set(color.FgCyan) 36 color.Set(color.Faint) 37 if TraceFlag { 38 fmt.Printf(format, arg...) 39 } 40 color.Unset() 41 } 42 43 // Print debug 44 func Debug(format string, arg ...interface{}) { 45 if DebugFlag { 46 fmt.Printf(format, arg...) 47 } 48 } 49 50 // Print verbose 51 func Verbose(format string, arg ...interface{}) { 52 color.NoColor = false 53 color.Set(color.FgHiMagenta) 54 if VerboseFlag { 55 fmt.Printf(format, arg...) 56 } 57 color.Unset() 58 } 59 60 // Catchable Panic 61 func Panicf(format string, args ...interface{}) { 62 color.NoColor = false 63 color.Set(color.FgRed) 64 color.Set(color.Bold) 65 66 fmt.Printf("❌ FATAL: "+format, args...) 67 color.Unset() 68 if !RecoverOnError { 69 os.Exit(4) 70 } 71 } 72 73 func SetVersion(version string) { 74 Version = version 75 if strings.Contains(Version, "-") { 76 Version = Version[:strings.Index(version, "-")] 77 } else { 78 Version = "v2.285" 79 } 80 }