Skip to content

Commit

Permalink
minor changes & README updated
Browse files Browse the repository at this point in the history
  • Loading branch information
cipheras committed Apr 22, 2021
1 parent 137b01b commit 1779624
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ Try(error, mode, message)
where, mode can be true or false
If mode=true, process will exit and if mode=false, process will generate a warning message.
```
This will write the same message to logs and also will show on terminal in that particular format.
* This will write the same message to logs and also will show on terminal if some message is given.
* If no message is given, it will only log and do not show on terminal.
* To show original error on terminal, use `Cprint(err)`. If err!=nil, it will be shown on terminal.
* If you want to log `info`(other information), do:
```
Try(nil, false, message)
```

* To automatically generate `log.txt` file and write logs in it, call function `Flog()`.
<br>To automatically generate `log.txt` file and write logs in it, call function `Flog()`.
```
Examples:
Try(err, true, "logging this") //it will log this message in case of err=nil, and if err!=nil then will log this message with error in file.
Expand All @@ -35,11 +41,6 @@ OUTPUT:
2021/4/12 14:34:23 main.go:24: [INFO] generating logs
```

<br> If you want to log `info`(other information), do:
```
Try(err, false, message)
```

### How to create formatted texts on console:
* To show colors on **WINDOWS CMD** also, call function `Cwindows()`.
```
Expand Down
44 changes: 23 additions & 21 deletions gohelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Flog() error {
return err
}
log.SetOutput(f)
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
log.SetFlags(log.LstdFlags)
// defer f.Close()
return nil
}
Expand All @@ -59,23 +59,23 @@ func Try(err error, mode bool, msg ...interface{}) { //err,exit/noexit,msg
if msgs != "" {
if mode == true {
Cprint(E, msgs)
log.Println("[ERR]", msgs, err)
log.Println("[ERROR]", msgs, "-", err)
os.Exit(0)
}
Cprint(W, msgs)
log.Println("[WARN]", msgs, err)
log.Println("[WARN] ", msgs, "-", err)
return
}
if mode == true {
Cprint(E, err)
log.Println("[ERR]", err)
// Cprint(E, err)
log.Println("[ERROR]", err)
os.Exit(0)
}
Cprint(W, err)
log.Println("[WARN]", err)
// Cprint(W, err)
log.Println("[WARN] ", err)
return
} else if msgs != "" {
log.Println("[INFO]", msgs)
log.Println("[INFO] ", msgs)
}
}

Expand All @@ -85,19 +85,21 @@ func Cprint(mode string, msg ...interface{}) {
for _, v := range msg {
msgs = msgs + fmt.Sprintf("%v ", v)
}
switch mode {
case N: //normal
fmt.Println("\n" + CYAN + "[" + GREEN + "+" + CYAN + "] " + GREEN + msgs + RESET)
case E: //error
fmt.Println("\n" + CYAN + "[" + RED + BLINK + "-" + RESET + CYAN + "] " + RED + BGBLACK + BOLD + "ERROR" + RESET + " " + RED + msgs + RESET)
case W: //warning
fmt.Println("\n" + CYAN + "[" + YELLOW + BLINK + "!" + RESET + CYAN + "] " + YELLOW + BGBLACK + BOLD + "WARN" + RESET + " " + YELLOW + msgs + RESET)
case T: //text
fmt.Println("\n" + CYAN + "[" + PURPLE + "*" + CYAN + "] " + PURPLE + msgs + RESET)
case I: //info
fmt.Println("\n" + CYAN + "[" + BLUE + "i" + CYAN + "] " + BLUE + msgs + RESET)
case S: //shell
fmt.Print("\n" + CYAN + "[" + PURPLE + "*" + CYAN + "] " + PURPLE + msgs + "\n" + GREEN + ">> " + RESET)
if msgs != "<nil> " {
switch mode {
case N: //normal
fmt.Println("\n" + CYAN + "[" + GREEN + "+" + CYAN + "] " + GREEN + msgs + RESET)
case E: //error
fmt.Println("\n" + CYAN + "[" + RED + BLINK + "-" + RESET + CYAN + "] " + RED + BGBLACK + BOLD + "ERROR" + RESET + " " + RED + msgs + RESET)
case W: //warning
fmt.Println("\n" + CYAN + "[" + YELLOW + BLINK + "!" + RESET + CYAN + "] " + YELLOW + BGBLACK + BOLD + "WARN" + RESET + " " + YELLOW + msgs + RESET)
case T: //text
fmt.Println("\n" + CYAN + "[" + PURPLE + "*" + CYAN + "] " + PURPLE + msgs + RESET)
case I: //info
fmt.Println("\n" + CYAN + "[" + BLUE + "i" + CYAN + "] " + BLUE + msgs + RESET)
case S: //shell
fmt.Print("\n" + CYAN + "[" + PURPLE + "*" + CYAN + "] " + PURPLE + msgs + "\n" + GREEN + ">> " + RESET)
}
}
}

Expand Down

0 comments on commit 1779624

Please sign in to comment.