Skip to content

Commit

Permalink
Merge pull request #7 from lpabon/debug
Browse files Browse the repository at this point in the history
log: Debug can now print file and function
  • Loading branch information
lpabon committed Feb 22, 2017
2 parents 562752a + 7485cc5 commit 0e37244
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func logWithLongFile(l *log.Logger, format string, v ...interface{}) {
fmt.Sprintf(format, v...))
}

func logWithLongFileAndFunction(l *log.Logger, format string, v ...interface{}) {
pc, file, line, _ := runtime.Caller(2)
caller_func_info := runtime.FuncForPC(pc)
l.Print(fmt.Sprintf("%v:%v(%v): ", path.Base(file), line, path.Base(caller_func_info.Name())) +
fmt.Sprintf(format, v...))
}

// Create a new logger
func NewLogger(prefix string, level LogLevel) *Logger {
godbc.Require(level >= 0, level)
Expand Down Expand Up @@ -96,10 +103,12 @@ func (l *Logger) SetLevel(level LogLevel) {
}

// Log critical information
func (l *Logger) Critical(format string, v ...interface{}) {
func (l *Logger) Critical(format string, v ...interface{}) error {
if l.level >= LEVEL_CRITICAL {
logWithLongFile(l.critlog, format, v...)
}

return fmt.Errorf(format, v...)
}

// Log error string
Expand All @@ -121,10 +130,12 @@ func (l *Logger) Err(err error) error {
}

// Log warning information
func (l *Logger) Warning(format string, v ...interface{}) {
func (l *Logger) Warning(format string, v ...interface{}) error {
if l.level >= LEVEL_WARNING {
l.warninglog.Printf(format, v...)
logWithLongFile(l.warninglog, format, v...)
}

return fmt.Errorf(format, v...)
}

// Log error variable as a warning
Expand All @@ -139,13 +150,13 @@ func (l *Logger) WarnErr(err error) error {
// Log string
func (l *Logger) Info(format string, v ...interface{}) {
if l.level >= LEVEL_INFO {
l.infolog.Printf(format, v...)
logWithLongFile(l.infolog, format, v...)
}
}

// Log string as debug
func (l *Logger) Debug(format string, v ...interface{}) {
if l.level >= LEVEL_DEBUG {
logWithLongFile(l.debuglog, format, v...)
logWithLongFileAndFunction(l.debuglog, format, v...)
}
}

0 comments on commit 0e37244

Please sign in to comment.