From 6c5ebcf61f3cdc307c53fb9670300770583d23ae Mon Sep 17 00:00:00 2001 From: ren yuze Date: Fri, 28 Jun 2019 10:56:57 +0800 Subject: [PATCH] init --- starlog.go | 281 ++++++++++++++++++++++++++++++++++++++++++++++++ starlog_test.go | 8 ++ 2 files changed, 289 insertions(+) create mode 100644 starlog.go create mode 100644 starlog_test.go diff --git a/starlog.go b/starlog.go new file mode 100644 index 0000000..2b77dff --- /dev/null +++ b/starlog.go @@ -0,0 +1,281 @@ +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) +} diff --git a/starlog_test.go b/starlog_test.go new file mode 100644 index 0000000..16dbcaf --- /dev/null +++ b/starlog_test.go @@ -0,0 +1,8 @@ +package starlog + +import "testing" + +func Test_StarLog(t *testing.T) { + CreateLog("./sakura", "suki") + Println("suki!", "blue", "b") +}