Skip to content

Commit

Permalink
core: use host OS specific line endings
Browse files Browse the repository at this point in the history
Rather than just forcing LF line endings everywhere, allow for CR/LF
when running on Windows.

Signed-off-by: Ryan Graham <[email protected]>
  • Loading branch information
rmg committed Dec 12, 2018
1 parent 907a21f commit 2e83587
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
7 changes: 4 additions & 3 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

var os = require('os');
var util = require('util');

var StringDecoder = require('string_decoder').StringDecoder;
Expand Down Expand Up @@ -104,7 +105,7 @@ function reLiner() {

function appendNewline(line, _enc, callback) {
this.push(line);
callback(null, Buffer.from('\n'));
callback(null, Buffer.from(os.EOL));
}
}

Expand Down Expand Up @@ -143,7 +144,7 @@ function textFormatter(options) {
if (options.timeStamp) {
line = util.format('%s %s', new Date(logEvent.time).toISOString(), line);
}
callback(null, line.replace(/\n/g, '\\n'));
callback(null, line.replace(/\r?\n/g, '\\n'));
}

function textifyTags(tags) {
Expand Down Expand Up @@ -190,7 +191,7 @@ function lineMerger(host) {
function lineMergerWrite(line, _enc, callback) {
if (/^\s+/.test(line.msg)) {
if (previousLine) {
previousLine.msg += '\n' + line.msg;
previousLine.msg += os.EOL + line.msg;
} else {
previousLine = line;
}
Expand Down
32 changes: 17 additions & 15 deletions test/test-bad-utf8.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var os = require('os');
var tap = require('tap');
var Log = require('../');


tap.test('truncated utf8', function(t) {
var slt = Log();
var input = [
Buffer.from('good line\n'),
Buffer.from('good line\n'),
Buffer.from('good line\n'),
Buffer.from('good line\n'),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from([
// an incomplete utf8 sequence (3/4 bytes)
0xf0, // byte 1 of 4 marker
Expand All @@ -17,32 +17,34 @@ tap.test('truncated utf8', function(t) {
]),
];
var expected = Buffer.concat([
Buffer.from("good line\n"),
Buffer.from("good line\n"),
Buffer.from('good line\n'),
Buffer.from('good line\n'),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from([
0xef, 0xbf, 0xbd, // single replacement character
0x0a, // trailing newline adde by strong-log-transformer
]),
// trailing newline adde by strong-log-transformer
Buffer.from(os.EOL),
]);
var received = [];

if (/^v(4|6)\./.test(process.version)) {
expected = Buffer.concat([
Buffer.from("good line\n"),
Buffer.from("good line\n"),
Buffer.from('good line\n'),
Buffer.from('good line\n'),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from('good line' + os.EOL),
Buffer.from([
// prior to node 8 each byte of an invalid utf8 sequence would be
// replaced by a UTF replacement character. For more details, see
// https://github.com/nodejs/node/commit/24ef1e6775
0xef, 0xbf, 0xbd, // replacement character
0xef, 0xbf, 0xbd, // replacement character
0xef, 0xbf, 0xbd, // replacement character
0x0a, // trailing newline adde by strong-log-transformer
]),
// trailing newline adde by strong-log-transformer
Buffer.from(os.EOL),
]);
}

Expand Down
9 changes: 5 additions & 4 deletions test/test-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var common = require('./common');
var fs = require('fs');
var os = require('os');
var tap = require('tap');

// These tests were ported from the original shell script based tests. The
Expand Down Expand Up @@ -74,10 +75,10 @@ tap.test('text: object tagged', function(t) {
tap.test('text: timestamps', function(t) {
var input = fs.readFileSync('test/fixtures/basic.in');
var expected = fs.readFileSync('test/fixtures/text-timestamp.grep');
var expectedLines = expected.toString('utf8').split('\n');
var expectedLines = expected.toString('utf8').split(os.EOL);
var expectedPatterns = expectedLines.map(function(line) { return new RegExp(line); });
common.sltCLI(['--timeStamp'], input, function(err, output) {
var outputLines = output.toString('utf8').split('\n');
var outputLines = output.toString('utf8').split(os.EOL);
t.ifErr(err);
t.same(outputLines.length, expectedPatterns.length, 'correct number of lines');
for (var l = 0; l < expectedLines.length; l++) {
Expand All @@ -100,10 +101,10 @@ tap.test('json', function(t) {
tap.test('json: timestamps', function(t) {
var input = fs.readFileSync('test/fixtures/basic.in');
var expected = fs.readFileSync('test/fixtures/json-timestamp.grep');
var expectedLines = expected.toString('utf8').split('\n');
var expectedLines = expected.toString('utf8').split(os.EOL);
var expectedPatterns = expectedLines.map(function(line) { return new RegExp(line); });
common.sltCLI(['--timeStamp', '--format=json'], input, function(err, output) {
var outputLines = output.toString('utf8').split('\n');
var outputLines = output.toString('utf8').split(os.EOL);
t.ifErr(err);
t.same(outputLines.length, expectedPatterns.length, 'correct number of lines');
for (var l = 0; l < expectedLines.length; l++) {
Expand Down

0 comments on commit 2e83587

Please sign in to comment.