Skip to content

Commit

Permalink
add build tags: log_linenumber
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng committed Nov 3, 2016
1 parent 37e3ef4 commit ad1306c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
16 changes: 0 additions & 16 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,6 @@ func (logger *Logger) Close() {
logger.baseFile = nil
}

func (logger *Logger) doPrintf(level int, printLevel string, format string, a ...interface{}) {
if level < logger.level {
return
}
if logger.baseLogger == nil {
panic("logger closed")
}

format = printLevel + format
logger.baseLogger.Printf(format, a...)

if level == fatalLevel {
os.Exit(1)
}
}

func (logger *Logger) Debug(format string, a ...interface{}) {
logger.doPrintf(debugLevel, printDebugLevel, format, a...)
}
Expand Down
21 changes: 21 additions & 0 deletions log/printf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build !log_linenumber

package log

import "os"

func (logger *Logger) doPrintf(level int, printLevel string, format string, a ...interface{}) {
if level < logger.level {
return
}
if logger.baseLogger == nil {
panic("logger closed")
}

format = printLevel + format
logger.baseLogger.Printf(format, a...)

if level == fatalLevel {
os.Exit(1)
}
}
35 changes: 35 additions & 0 deletions log/printf_linenumber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build log_linenumber

package log

import (
"os"
"runtime"
"strconv"
)

func logSite() string {
_, file, line, ok := runtime.Caller(4)
if !ok {
file = "???"
line = 0
}
c := string(file + ":" + strconv.FormatInt(int64(line), 10))
return c
}

func (logger *Logger) doPrintf(level int, printLevel string, format string, a ...interface{}) {
if level < logger.level {
return
}
if logger.baseLogger == nil {
panic("logger closed")
}

format = printLevel + "["+logSite()+"] " + format
logger.baseLogger.Printf(format, a...)

if level == fatalLevel {
os.Exit(1)
}
}

0 comments on commit ad1306c

Please sign in to comment.