forked from minio/minio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
163 lines (148 loc) · 4.46 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Mini Copy (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"sync"
"path/filepath"
)
type logLevel int
const (
levelUnknown logLevel = iota
levelDebug
levelInfo
levelError
levelFatal
)
var (
mutex = &sync.RWMutex{}
// Fatal prints a error message and exits
Fatal = func(a ...interface{}) { privatePrint(levelFatal, a...) }
// Fatalln prints a error message with a new line and exits
Fatalln = func(a ...interface{}) { privatePrintln(levelFatal, a...) }
// Fatalf prints a error message with formatting and exits
Fatalf = func(f string, a ...interface{}) { privatePrintf(levelFatal, f, a...) }
// Error prints a error message
Error = func(a ...interface{}) { privatePrint(levelError, a...) }
// Errorln prints a error message with a new line
Errorln = func(a ...interface{}) { privatePrintln(levelError, a...) }
// Errorf prints a error message with formatting
Errorf = func(f string, a ...interface{}) { privatePrintf(levelError, f, a...) }
// Info prints a informational message
Info = func(a ...interface{}) { privatePrint(levelInfo, a...) }
// Infoln prints a informational message with a new line
Infoln = func(a ...interface{}) { privatePrintln(levelInfo, a...) }
// Infof prints a informational message with formatting
Infof = func(f string, a ...interface{}) { privatePrintf(levelInfo, f, a...) }
// Debug prints a debug message
Debug = func(a ...interface{}) { privatePrint(levelDebug, a...) }
// Debugln prints a debug message with a new line
Debugln = func(a ...interface{}) { privatePrintln(levelDebug, a...) }
// Debugf prints a debug message with formatting
Debugf = func(f string, a ...interface{}) { privatePrintf(levelDebug, f, a...) }
)
var (
// print prints a message prefixed with message type and program name
privatePrint = func(l logLevel, a ...interface{}) {
switch l {
case levelDebug:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <DEBUG> ")
fmt.Fprint(os.Stderr, a...)
mutex.Unlock()
case levelInfo:
mutex.Lock()
fmt.Print(ProgramName() + ": <INFO> ")
fmt.Print(a...)
mutex.Unlock()
case levelError:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <ERROR> ")
fmt.Fprint(os.Stderr, a...)
mutex.Unlock()
case levelFatal:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <FATAL> ")
fmt.Fprint(os.Stderr, a...)
mutex.Unlock()
os.Exit(1)
default:
fmt.Print(a...)
}
}
// println - same as print with a new line
privatePrintln = func(l logLevel, a ...interface{}) {
switch l {
case levelDebug:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <DEBUG> ")
fmt.Fprintln(os.Stderr, a...)
mutex.Unlock()
case levelInfo:
mutex.Lock()
fmt.Print(ProgramName() + ": <INFO> ")
fmt.Println(a...)
mutex.Unlock()
case levelError:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <ERROR> ")
fmt.Fprintln(os.Stderr, a...)
mutex.Unlock()
case levelFatal:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <FATAL> ")
fmt.Fprintln(os.Stderr, a...)
mutex.Unlock()
os.Exit(1)
default:
fmt.Print(a...)
}
}
// printf - same as print, but takes a format specifier
privatePrintf = func(l logLevel, f string, a ...interface{}) {
switch l {
case levelDebug:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <DEBUG> ")
fmt.Fprintf(os.Stderr, f, a...)
mutex.Unlock()
case levelInfo:
mutex.Lock()
fmt.Print(ProgramName() + ": <INFO> ")
fmt.Printf(f, a...)
mutex.Unlock()
case levelError:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <ERROR> ")
fmt.Fprintf(os.Stderr, f, a...)
mutex.Unlock()
case levelFatal:
mutex.Lock()
fmt.Fprint(os.Stderr, ProgramName()+": <FATAL> ")
fmt.Fprintf(os.Stderr, f, a...)
mutex.Unlock()
os.Exit(1)
default:
fmt.Printf(f, a...)
}
}
)
// ProgramName - return the name of the executable program
func ProgramName() string {
_, progName := filepath.Split(os.Args[0])
return progName
}