Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Improved handle for unix/windows line endings correctly. #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions log4go.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -196,10 +197,13 @@ func (log Logger) intLogf(lvl Level, format string, args ...interface{}) {
}

// Determine caller func
pc, _, lineno, ok := runtime.Caller(2)
// pc, _, lineno, ok := runtime.Caller(2)
_, file, lineno, ok := runtime.Caller(2)
src := ""
if ok {
src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
// src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
// Use the relative path
src = fmt.Sprintf("%s:%d", filepath.Base(file), lineno)
}

msg := format
Expand Down Expand Up @@ -240,10 +244,13 @@ func (log Logger) intLogc(lvl Level, closure func() string) {
}

// Determine caller func
pc, _, lineno, ok := runtime.Caller(2)
// pc, _, lineno, ok := runtime.Caller(2)
_, file, lineno, ok := runtime.Caller(2)
src := ""
if ok {
src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
// src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
// Use the relative path
src = fmt.Sprintf("%s:%d", filepath.Base(file), lineno)
}

// Make the log record
Expand Down
9 changes: 8 additions & 1 deletion pattlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"strings"
"runtime"
)

const (
Expand Down Expand Up @@ -92,7 +93,13 @@ func FormatLogRecord(format string, rec *LogRecord) string {
out.Write(piece)
}
}
out.WriteByte('\n')

// handles line endings
lineEnding := "\n"
if runtime.GOOS == "windows" {
lineEnding = "\r\n"
}
out.Write([]byte(lineEnding))

return out.String()
}
Expand Down