package starlog import ( "fmt" "strings" "github.com/fatih/color" "os" "path/filepath" "strconv" "time" ) var Path string var LogPar *os.File var CSpace bool = false func CreateLog(logpath, prefix string) string { logname := strconv.FormatInt(time.Now().Unix(), 10) logpath, _ = filepath.Abs(logpath) _, exs := os.Stat(logpath) if exs != nil && os.IsNotExist(exs) { os.MkdirAll(logpath, 0755) } logname = logpath + `/` + prefix + logname + ".log" LogPar, _ = os.Create(logname) strpath, _ := filepath.Abs(logname) return strpath } func WriteError(err error, other string) { now := time.Now().Format("2006-01-02 15:04:05") strlog := now + ": " + other + ":" + err.Error() + "\n" LogPar.Write([]byte(strlog)) } func WriteLog(strs string) { now := time.Now().Format("2006-01-02 15:04:05") strlog := now + ": " + strs + "\n" LogPar.Write([]byte(strlog)) } func PrintError(sakura ...interface{}) { var mycolor, bold string lens := len(sakura) if lens < 2 { return } if lens >= 3 { mycolor, _ = sakura[2].(string) if lens == 4 { bold, _ = sakura[3].(string) } } err, _ := sakura[0].(error) other, _ := sakura[1].(string) now := time.Now().Format("2006-01-02 15:04:05") strlog := now + ": " + other + ":" + err.Error() switch strings.ToLower(mycolor) { case "blue": color.Set(color.FgBlue) case "black": color.Set(color.FgBlack) case "cyan": color.Set(color.FgCyan) case "green": color.Set(color.FgGreen) case "magenta": color.Set(color.FgMagenta) case "red": color.Set(color.FgRed) case "white": color.Set(color.FgWhite) case "yellow": color.Set(color.FgYellow) case "grey": color.Set(color.FgHiYellow) default: color.Set(color.Reset) } for _, v := range []byte(bold) { switch string([]byte{v}) { case "b": color.Set(color.Bold) case "l": color.Set(color.BlinkRapid) case "u": color.Set(color.Underline) } } if CSpace { strlog += "\n" } fmt.Println(strlog) color.Set(color.Reset) LogPar.Write([]byte(strlog + "\n")) } func Println(sakura ...interface{}) { var mycolor, bold string lens := len(sakura) if lens < 2 { fmt.Println(sakura) return } if lens >= 2 { mycolor, _ = sakura[lens-2].(string) if lens == 3 { bold, _ = sakura[lens-1].(string) } } switch strings.ToLower(mycolor) { case "blue": color.Set(color.FgBlue) case "black": color.Set(color.FgBlack) case "cyan": color.Set(color.FgCyan) case "green": color.Set(color.FgGreen) case "magenta": color.Set(color.FgMagenta) case "red": color.Set(color.FgRed) case "white": color.Set(color.FgWhite) case "yellow": color.Set(color.FgYellow) case "grey": color.Set(color.FgHiYellow) default: color.Set(color.Reset) } for _, v := range []byte(bold) { switch string([]byte{v}) { case "b": color.Set(color.Bold) case "l": color.Set(color.BlinkRapid) case "u": color.Set(color.Underline) } } var hoe []interface{} for i := 0; i < lens-2; i++ { hoe = append(hoe, sakura[i]) } if len(hoe) == 1 { fmt.Println(hoe[0]) } else { fmt.Println(hoe) } color.Set(color.Reset) } func Print(sakura ...interface{}) { var mycolor, bold string lens := len(sakura) if lens < 2 { fmt.Print(sakura) return } if lens >= 2 { mycolor, _ = sakura[lens-2].(string) if lens == 3 { bold, _ = sakura[lens-1].(string) } } switch strings.ToLower(mycolor) { case "blue": color.Set(color.FgBlue) case "black": color.Set(color.FgBlack) case "cyan": color.Set(color.FgCyan) case "green": color.Set(color.FgGreen) case "magenta": color.Set(color.FgMagenta) case "red": color.Set(color.FgRed) case "white": color.Set(color.FgWhite) case "yellow": color.Set(color.FgYellow) case "grey": color.Set(color.FgHiYellow) default: color.Set(color.Reset) } for _, v := range []byte(bold) { switch string([]byte{v}) { case "b": color.Set(color.Bold) case "l": color.Set(color.BlinkRapid) case "u": color.Set(color.Underline) } } var hoe []interface{} for i := 0; i < lens-2; i++ { hoe = append(hoe, sakura[i]) } if len(hoe) == 1 { fmt.Println(hoe[0]) } else { fmt.Println(hoe) } color.Set(color.Reset) } func PrintLog(sakura ...interface{}) { var mycolor, bold string lens := len(sakura) if lens < 1 { return } if lens >= 2 { mycolor, _ = sakura[1].(string) if lens == 3 { bold, _ = sakura[2].(string) } } strs, _ := sakura[0].(string) now := time.Now().Format("2006-01-02 15:04:05") strlog := now + ": " + strs switch strings.ToLower(mycolor) { case "blue": color.Set(color.FgBlue) case "black": color.Set(color.FgBlack) case "cyan": color.Set(color.FgCyan) case "green": color.Set(color.FgGreen) case "magenta": color.Set(color.FgMagenta) case "red": color.Set(color.FgRed) case "white": color.Set(color.FgWhite) case "yellow": color.Set(color.FgYellow) case "grey": color.Set(color.FgHiYellow) default: color.Set(color.Reset) } for _, v := range []byte(bold) { switch string([]byte{v}) { case "b": color.Set(color.Bold) case "l": color.Set(color.BlinkRapid) case "u": color.Set(color.Underline) } } if CSpace { strlog += "\n" } fmt.Println(strlog) color.Set(color.Reset) LogPar.Write([]byte(strlog + "\n")) } func End() { LogPar.Close() } func ThrowError(err error, other string) { if err != nil { PrintError(err, other) } End() time.Sleep(time.Second * 8) panic(err) os.Exit(233) }