Skip to content

log: add Llevel flag and OutputL() function #49236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 49 additions & 28 deletions src/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
Lmsgprefix // move the "prefix" from the beginning of the line to before the message
Llevel // the level text: Print
LstdFlags = Ldate | Ltime // initial values for the standard logger
)

Expand Down Expand Up @@ -107,11 +108,15 @@ func itoa(buf *[]byte, i int, wid int) {
}

// formatHeader writes log header to buf in following order:
// * level (if it's not blank and Llevel is set)
// * l.prefix (if it's not blank and Lmsgprefix is unset),
// * date and/or time (if corresponding flags are provided),
// * file and line number (if corresponding flags are provided),
// * l.prefix (if it's not blank and Lmsgprefix is set).
func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
func (l *Logger) formatHeader(buf *[]byte, level string, t time.Time, file string, line int) {
if l.flag&Llevel != 0 {
*buf = append(*buf, level...)
}
if l.flag&Lmsgprefix == 0 {
*buf = append(*buf, l.prefix...)
}
Expand Down Expand Up @@ -163,13 +168,7 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
}
}

// 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 used to recover the PC and is
// provided for generality, although at the moment on all pre-defined
// paths it will be 2.
func (l *Logger) Output(calldepth int, s string) error {
func (l *Logger) outputl(calldepth int, level, s string) error {
now := time.Now() // get this early.
var file string
var line int
Expand All @@ -187,7 +186,7 @@ func (l *Logger) Output(calldepth int, s string) error {
l.mu.Lock()
}
l.buf = l.buf[:0]
l.formatHeader(&l.buf, now, file, line)
l.formatHeader(&l.buf, level, now, file, line)
l.buf = append(l.buf, s...)
if len(s) == 0 || s[len(s)-1] != '\n' {
l.buf = append(l.buf, '\n')
Expand All @@ -196,13 +195,29 @@ func (l *Logger) Output(calldepth int, s string) error {
return err
}

// 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 used to recover the PC and is
// provided for generality, although at the moment on all pre-defined
// paths it will be 2.
func (l *Logger) Output(calldepth int, s string) error {
return l.outputl(calldepth+1, "Print ", s)
}

// OutputL writes the output for a logging event. The string level contains
// the text to print at the beginning of the line.
func (l *Logger) OutputL(calldepth int, level, s string) error {
return l.outputl(calldepth+1, level, s)
}

// Printf calls l.Output to print to the logger.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Printf(format string, v ...interface{}) {
if atomic.LoadInt32(&l.isDiscard) != 0 {
return
}
l.Output(2, fmt.Sprintf(format, v...))
l.OutputL(2, "Print ", fmt.Sprintf(format, v...))
}

// Print calls l.Output to print to the logger.
Expand All @@ -211,7 +226,7 @@ func (l *Logger) Print(v ...interface{}) {
if atomic.LoadInt32(&l.isDiscard) != 0 {
return
}
l.Output(2, fmt.Sprint(v...))
l.OutputL(2, "Print ", fmt.Sprint(v...))
}

// Println calls l.Output to print to the logger.
Expand All @@ -220,45 +235,45 @@ func (l *Logger) Println(v ...interface{}) {
if atomic.LoadInt32(&l.isDiscard) != 0 {
return
}
l.Output(2, fmt.Sprintln(v...))
l.OutputL(2, "Print ", fmt.Sprintln(v...))
}

// Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
func (l *Logger) Fatal(v ...interface{}) {
l.Output(2, fmt.Sprint(v...))
l.OutputL(2, "Fatal ", fmt.Sprint(v...))
os.Exit(1)
}

// Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.Output(2, fmt.Sprintf(format, v...))
l.OutputL(2, "Fatal ", fmt.Sprintf(format, v...))
os.Exit(1)
}

// Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
func (l *Logger) Fatalln(v ...interface{}) {
l.Output(2, fmt.Sprintln(v...))
l.OutputL(2, "Fatal ", fmt.Sprintln(v...))
os.Exit(1)
}

// Panic is equivalent to l.Print() followed by a call to panic().
func (l *Logger) Panic(v ...interface{}) {
s := fmt.Sprint(v...)
l.Output(2, s)
l.OutputL(2, "Panic ", s)
panic(s)
}

// Panicf is equivalent to l.Printf() followed by a call to panic().
func (l *Logger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
l.Output(2, s)
l.OutputL(2, "Panic ", s)
panic(s)
}

// Panicln is equivalent to l.Println() followed by a call to panic().
func (l *Logger) Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
l.Output(2, s)
l.OutputL(2, "Panic ", s)
panic(s)
}

Expand Down Expand Up @@ -339,7 +354,7 @@ func Print(v ...interface{}) {
if atomic.LoadInt32(&std.isDiscard) != 0 {
return
}
std.Output(2, fmt.Sprint(v...))
std.OutputL(2, "Print ", fmt.Sprint(v...))
}

// Printf calls Output to print to the standard logger.
Expand All @@ -348,7 +363,7 @@ func Printf(format string, v ...interface{}) {
if atomic.LoadInt32(&std.isDiscard) != 0 {
return
}
std.Output(2, fmt.Sprintf(format, v...))
std.OutputL(2, "Print ", fmt.Sprintf(format, v...))
}

// Println calls Output to print to the standard logger.
Expand All @@ -357,45 +372,45 @@ func Println(v ...interface{}) {
if atomic.LoadInt32(&std.isDiscard) != 0 {
return
}
std.Output(2, fmt.Sprintln(v...))
std.OutputL(2, "Print ", 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...))
std.OutputL(2, "Fatal ", 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...))
std.OutputL(2, "Fatal ", 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...))
std.OutputL(2, "Fatal ", 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)
std.OutputL(2, "Panic ", 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)
std.OutputL(2, "Panic ", 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)
std.OutputL(2, "Panic ", s)
panic(s)
}

Expand All @@ -407,5 +422,11 @@ func Panicln(v ...interface{}) {
// 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.
return std.OutputL(calldepth+1, "Print ", s) // +1 for this frame.
}

// OutputL writes the output for a logging event. The string level contains
// the text to print before the prefix.
func OutputL(calldepth int, level, s string) error {
return std.OutputL(calldepth+1, level, s)
}