-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlog.go
245 lines (211 loc) · 7 KB
/
vlog.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Package vlog add leveled log on std log(golang.org/pkg/log/)
// It implements most std log functions(except logger), variables
// and add provides V-style logging controlled by the -v flag or SetLogLevel()
// If flag.Parse be called before any logging, -v flag(default 0) use automaticlly.
//
// 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.
//
// Basic examples:
// vlog.SetLogLevel(3)
// vlog.GetLogLevel()
//
// vlog.Println("Prepare to repel boarders")
//
// vlog.Fatalf("Initialization failed: %s", err)
//
// See the documentation for the V function for an explanation of these examples:
//
// if vlog.V(2) {
// vlog.Print("Starting transaction...")
// }
//
// vlog.V(2).Println("Processed", nItems, "elements")
package vlog
import (
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
)
// These flags define which text to prefix to each log entry generated by the Logger.
const (
// Bits or'ed together to control what's printed.
// There is no control over the order they appear (the order listed
// here) or the format they present (as described in the comments).
// The prefix is followed by a colon only when Llongfile or Lshortfile
// is specified.
// For example, flags Ldate | Ltime (or LstdFlags) produce,
// 2009/01/23 01:23:23 message
// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
// 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
Ldate = log.Ldate // the date in the local time zone: 2009/01/23
Ltime = log.Ltime // the time in the local time zone: 01:23:23
Lmicroseconds = log.Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
Llongfile = log.Llongfile // full file name and line number: /a/b/c/d.go:23
Lshortfile = log.Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC = log.LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
LstdFlags = log.Ldate | log.Ltime // initial values for the standard logger
)
var (
level Level
vParsed bool
std = log.New(os.Stderr, "", LstdFlags)
)
func init() {
flag.Var(&level, "v", "log level for V logs(default 0)")
}
// Level log level
type Level int32
// String is part of the flag.Value interface.
func (l *Level) String() string {
return strconv.FormatInt(int64(*l), 10)
}
// Get is part of the flag.Value interface.
func (l *Level) Get() interface{} {
return int32(*l)
}
// Set is part of the flag.Value interface.
func (l *Level) Set(value string) error {
v, err := strconv.ParseInt(value, 0, 32)
if err != nil {
return err
}
*l = Level(v)
vParsed = true
return nil
}
// SetLogLevel set log level, just use at parameter initialize zone.
func SetLogLevel(v Level) {
level = v
}
// GetLogLevel get log level, just use at parameter initialize zone.
func GetLogLevel() Level {
return level
}
// IsLevelParsed get bool, whether be parsed for command line(use or not use -v flag)
func IsLevelParsed() bool {
return vParsed
}
// Verbose is a boolean type that implements Print like function.
// See the documentation of V for more information.
type Verbose bool
// V function return Verbose,
// whether an individual call to V generates a log record depends on the setting of level.
func V(v Level) Verbose {
if v <= level {
return true
}
return false
}
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func (vb Verbose) Printf(format string, v ...interface{}) {
if vb {
std.Output(2, fmt.Sprintf(format, v...))
}
}
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func (vb Verbose) Println(v ...interface{}) {
if vb {
std.Output(2, fmt.Sprintln(v...))
}
}
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func (vb Verbose) Print(v ...interface{}) {
if vb {
std.Output(2, fmt.Sprint(v...))
}
}
// SetOutput sets the output destination for the standard logger.
func SetOutput(w io.Writer) {
std.SetOutput(w)
}
// Flags returns the output flags for the standard logger.
func Flags() int {
return std.Flags()
}
// SetFlags sets the output flags for the standard logger.
func SetFlags(flag int) {
std.SetFlags(flag)
}
// Prefix returns the output prefix for the standard logger.
func Prefix() string {
return std.Prefix()
}
// SetPrefix sets the output prefix for the standard logger.
func SetPrefix(prefix string) {
std.SetPrefix(prefix)
}
// These functions write to the standard logger.
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
std.Output(2, fmt.Sprint(v...))
}
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
std.Output(2, fmt.Sprintf(format, v...))
}
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
std.Output(2, fmt.Sprintln(v...))
}
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
std.Output(2, fmt.Sprint(v...))
os.Exit(1)
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
std.Output(2, fmt.Sprintf(format, v...))
os.Exit(1)
}
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
func Fatalln(v ...interface{}) {
std.Output(2, fmt.Sprintln(v...))
os.Exit(1)
}
// Panic is equivalent to Print() followed by a call to panic().
func Panic(v ...interface{}) {
s := fmt.Sprint(v...)
std.Output(2, s)
panic(s)
}
// Panicf is equivalent to Printf() followed by a call to panic().
func Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
std.Output(2, s)
panic(s)
}
// Panicln is equivalent to Println() followed by a call to panic().
func Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
std.Output(2, s)
panic(s)
}
// Output writes the output for a logging event. The string s contains
// the text to print after the prefix specified by the flags of the
// Logger. A newline is appended if the last character of s is not
// already a newline. Calldepth is the count of the number of
// frames to skip when computing the file name and line number
// if Llongfile or Lshortfile is set; a value of 1 will print the details
// for the caller of Output.
func Output(calldepth int, s string) error {
return std.Output(calldepth+1, s) // +1 for this frame.
}