-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogger.go
89 lines (68 loc) · 1.7 KB
/
logger.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package logger
import (
"log"
"os"
"fmt"
"strings"
)
const (
LogPrefix = ""
prefixMain = ":: "
prefixSub = " -> "
prefixCmd = " $ "
prefixErr = "[ERROR] "
)
type SyncLogger struct {
*log.Logger
}
var (
Logger *SyncLogger
Verbose bool
CommandName string
)
func GetInstance(commandName string, flags int) *SyncLogger {
CommandName = commandName
if Logger == nil {
Logger = &SyncLogger{log.New(os.Stdout, LogPrefix, flags)}
}
return Logger
}
func (SyncLogger SyncLogger) Verbose(message string, sprintf ...interface{}) {
if Verbose {
if len(sprintf) > 0 {
message = fmt.Sprintf(message, sprintf...)
}
SyncLogger.Println(message)
}
}
func (SyncLogger SyncLogger) Main(message string, sprintf ...interface{}) {
if len(sprintf) > 0 {
message = fmt.Sprintf(message, sprintf...)
}
SyncLogger.Println(prefixMain + message)
}
func (SyncLogger SyncLogger) Step(message string, sprintf ...interface{}) {
if len(sprintf) > 0 {
message = fmt.Sprintf(message, sprintf...)
}
SyncLogger.Println(prefixSub + message)
}
func (SyncLogger SyncLogger) Command(message string) {
SyncLogger.Println(prefixCmd + message)
}
func (SyncLogger SyncLogger) FatalExit(exitCode int, message string, sprintf ...interface{}) {
if len(sprintf) > 0 {
message = fmt.Sprintf(message, sprintf...)
}
SyncLogger.Fatal(message)
os.Exit(exitCode)
}
// Log error object as message
func (SyncLogger SyncLogger) FatalErrorExit(exitCode int, err error) {
if CommandName != "" {
cmdline := fmt.Sprintf("%s %s", CommandName, strings.Join(os.Args[1:], " "))
fmt.Fprintln(os.Stderr, fmt.Sprintf("Command: %s", cmdline))
}
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s %s", prefixErr, err))
os.Exit(exitCode)
}