github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/pkg/utils/log/print.go (about) 1 package logUtils 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "net/http" 8 "os" 9 "regexp" 10 "strings" 11 12 constant "github.com/easysoft/zendata/internal/pkg/const" 13 commonUtils "github.com/easysoft/zendata/pkg/utils/common" 14 "github.com/easysoft/zendata/pkg/utils/vari" 15 "github.com/fatih/color" 16 17 zd "github.com/easysoft/zendata" 18 ) 19 20 var ( 21 exampleFile = fmt.Sprintf("res%sen%ssample.yaml", string(os.PathSeparator), string(os.PathSeparator)) 22 usageFile = fmt.Sprintf("res%sen%susage.txt", string(os.PathSeparator), string(os.PathSeparator)) 23 24 OutputFileWriter *os.File 25 OutputHttpWriter http.ResponseWriter 26 OutputFilePath string // for excel output 27 ) 28 29 func PrintExample() { 30 if vari.Config.Language == "zh" { 31 exampleFile = strings.Replace(exampleFile, "en", "zh", 1) 32 usageFile = strings.Replace(usageFile, "en", "zh", 1) 33 } 34 35 content, _ := zd.ReadResData(exampleFile) 36 log.Println(string(content)) 37 } 38 39 func PrintUsage() { 40 if vari.Config.Language == "zh" { 41 exampleFile = strings.Replace(exampleFile, "en", "zh", 1) 42 usageFile = strings.Replace(usageFile, "en", "zh", 1) 43 } 44 45 usageBytes, _ := zd.ReadResData(usageFile) 46 usage := string(usageBytes) 47 exeFile := "zd" 48 if commonUtils.IsWin() { 49 exeFile += ".exe" 50 } 51 52 if !commonUtils.IsWin() { 53 regx, _ := regexp.Compile(`\\`) 54 usage = regx.ReplaceAllString(usage, "/") 55 56 regx, _ = regexp.Compile(`zd.exe`) 57 usage = regx.ReplaceAllString(usage, "zd") 58 59 regx, _ = regexp.Compile(`d:\\zd\\config `) 60 usage = regx.ReplaceAllString(usage, "/home/user/zd/config") 61 } 62 63 log.Println(usage) 64 } 65 66 func Info(str string) { 67 PrintTo(str) 68 } 69 func Infof(str string, args ...interface{}) { 70 PrintTo(fmt.Sprintf(str, args)) 71 } 72 73 func PrintTo(str string) { 74 output := color.Output 75 fmt.Fprint(output, str+"\n") 76 } 77 78 func PrintToWithColor(msg string, attr color.Attribute) { 79 output := color.Output 80 81 if attr == -1 { 82 fmt.Fprint(output, msg+"\n") 83 } else { 84 color.New(attr).Fprintf(output, msg+"\n") 85 } 86 } 87 88 func PrintErrMsg(msg string) { 89 PrintToWithColor(msg, color.FgCyan) 90 } 91 92 func PrintRecord(str string) { 93 if OutputFileWriter != nil { 94 PrintToFile(str) 95 } else if vari.GlobalVars.RunMode == constant.RunModeServerRequest { 96 PrintToHttp(str) 97 } else { 98 PrintToScreen(fmt.Sprintf("%s", str)) 99 } 100 } 101 102 func PrintLine(line string) { 103 if vari.DefType == constant.DefTypeText { 104 line += "\n" 105 } 106 107 if OutputFileWriter != nil { 108 PrintToFile(line) 109 } else if vari.GlobalVars.RunMode == constant.RunModeServerRequest { 110 PrintToHttp(line) 111 } else { 112 PrintToScreen(line) 113 } 114 115 return 116 } 117 func PrintToFile(line string) { 118 fmt.Fprint(OutputFileWriter, line) 119 } 120 func PrintToHttp(line string) { 121 fmt.Fprint(OutputHttpWriter, line) 122 } 123 func PrintToScreen(line string) { 124 log.Print(line) 125 } 126 127 func PrintVersion(appVersion, buildTime, goVersion, gitHash string) { 128 fmt.Printf("%s \n", appVersion) 129 fmt.Printf("Build TimeStamp: %s \n", buildTime) 130 fmt.Printf("GoLang Version: %s \n", goVersion) 131 fmt.Printf("Git Commit Hash: %s \n", gitHash) 132 } 133 134 func ConvertUnicode(str []byte) string { 135 var a interface{} 136 137 temp := strings.Replace(string(str), "\\\\", "\\", -1) 138 139 err := json.Unmarshal([]byte(temp), &a) 140 141 var msg string 142 if err == nil { 143 bytes, _ := json.Marshal(a) 144 msg = string(bytes) 145 } else { 146 msg = temp 147 } 148 149 return msg 150 }