Skip to content

Commit da495ef

Browse files
authored
Merge pull request #121 from hpe-storage/logger_windows_append_CRLF
logger: Windows Notepad requires CRLF and not just LF
2 parents 43272f9 + 564bf53 commit da495ef

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

logger/logger.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Hewlett Packard Enterprise Development LP
1+
// Copyright 2020 Hewlett Packard Enterprise Development LP
22

33
package logger
44

@@ -332,6 +332,17 @@ func (hook *FileHook) Fire(entry *log.Entry) error {
332332
return err
333333
}
334334

335+
// For Windows only, insert '/r' in front of any tailing '/n'. Windows text files end
336+
// lines with CRLF while other platforms just end with LF.
337+
if runtime.GOOS == "windows" {
338+
for i := len(lineBytes) - 1; i > 0; i-- {
339+
if (lineBytes[i] != '\n') || (i > 0 && lineBytes[i-1] == '\r') {
340+
break
341+
}
342+
lineBytes = append(lineBytes[:i], append([]byte{'\r'}, lineBytes[i:]...)...)
343+
}
344+
}
345+
335346
hook.logWriter.Write(lineBytes)
336347
return nil
337348
}

0 commit comments

Comments
 (0)