-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
50 lines (45 loc) · 1.37 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Package console tries to bring the convenience of console.log/console.error in JS/NodeJS to Go
package console
import (
"fmt"
"log"
"time"
)
var (
//LogMode Use this to turn Log output to console off or on
LogMode = true
//ErrorMode Use this to turn Error output to console off or on
ErrorMode = true
//TimedMode Use this to turn Timed Output to console off or on
TimedMode = true
)
//Log use as console.log(pattern,vars...) return a string
func Log(pattern string, vars ...interface{}) string {
var msg = fmt.Sprintf(pattern, vars...)
if LogMode {
log.Println(msg)
}
return msg
}
//Error return an error object as per the provided format
// Usage console.Error(pattern,vars...)
// Output: This will Return an error object
func Error(pattern string, vars ...interface{}) error {
var err = fmt.Errorf(pattern, vars...)
if ErrorMode {
log.Println(err.Error())
}
return err
}
//Timed returns a string object appended with the time elapsed provided from the starttime
// Usage: console.Timed(starttime,pattern,vars...)
// Output: returns the formatted string appended with elapsed time and outputs the string to stdio if TimedMode is true
func Timed(startTime time.Time, pattern string, vars ...interface{}) string {
pattern += "\t%s"
vars = append(vars, time.Since(startTime))
var msg = fmt.Sprintf(pattern, vars...)
if TimedMode {
fmt.Println(msg)
}
return msg
}