Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ NAN_METHOD(Logger::New)
return Nan::ThrowError(Nan::Error("Provide the max size and max files"));
}
const std::string logName = *Nan::Utf8String(info[1]);
const std::string fileName = *Nan::Utf8String(info[2]);

logger = spdlog::get(logName);

if (!logger)
{
#if defined(_WIN32)
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const std::wstring fileName = converter.from_bytes(*Nan::Utf8String(info[2]));
#else
const std::string fileName = *Nan::Utf8String(info[2]);
#endif

logger = spdlog::rotating_logger_mt(logName, fileName, info[3]->IntegerValue(), info[4]->IntegerValue());
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
// Prevent child processes from inheriting the file handles
#define SPDLOG_PREVENT_CHILD_FD

#if defined(_WIN32)
// Use wide character file names in windows
#define SPDLOG_WCHAR_FILENAMES
#endif

#include <spdlog/spdlog.h>

NAN_METHOD(setAsyncMode);
Expand Down
76 changes: 52 additions & 24 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ suite('API', function () {
var EOL = '\n';
var testObject;

var filesToDelete = [];

suiteSetup(() => {
tempDirectory = path.join(__dirname, 'logs');
if (!fs.existsSync(tempDirectory)) {
fs.mkdirSync(tempDirectory);
}
logFile = path.join(tempDirectory, 'test.log');
filesToDelete.push(logFile);
if (fs.existsSync(logFile)) {
fs.unlinkSync(logFile);
}
Expand All @@ -39,19 +42,26 @@ suite('API', function () {
}
});

setup(() => {
testObject = new spdlog.RotatingLogger('test', logFile, 1048576 * 5, 2);
testObject.setPattern('%+');
});

teardown(() => {
testObject.drop();
if (testObject) {
testObject.drop();
}
});

suiteTeardown(() => {
if (fs.existsSync(logFile)) {
fs.unlinkSync(logFile);
}
filesToDelete.forEach(file => {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
});
});

test('is loaded', function () {
const spdloghPath = path.join(__dirname, '..', 'deps', 'spdlog', 'include', 'spdlog', 'common.h');
const contents = fs.readFileSync(spdloghPath, 'utf8');
const version = /SPDLOG_VERSION "([\d\.]+)"/.exec(contents)[1];

assert.equal(spdlog.version, version);
});

test('is loaded', function () {
Expand All @@ -67,19 +77,20 @@ suite('API', function () {
});

test('Create rotating logger', function () {
testObject = aTestObject(logFile);
assert.ok(fs.existsSync(logFile));
});

test('Log critical message', function () {

testObject = aTestObject(logFile);
testObject.critical('Hello World');

const actual = getLastLine();
assert.ok(actual.endsWith('[test] [critical] Hello World'));
});

test('Log error', function () {

testObject = aTestObject(logFile);
testObject.error('Hello World');
testObject.flush();

Expand All @@ -88,23 +99,23 @@ suite('API', function () {
});

test('Log warning', function () {

testObject = aTestObject(logFile);
testObject.warn('Hello World');

const actual = getLastLine();
assert.ok(actual.endsWith('[test] [warning] Hello World'));
});

test('Log info', function () {

testObject = aTestObject(logFile);
testObject.info('Hello World');

const actual = getLastLine();
assert.ok(actual.endsWith('[test] [info] Hello World'));
});

test('Log debug', function () {

testObject = aTestObject(logFile);
testObject.setLevel(1);
testObject.debug('Hello World');

Expand All @@ -113,7 +124,7 @@ suite('API', function () {
});

test('Log trace', function () {

testObject = aTestObject(logFile);
testObject.setLevel(0);
testObject.trace('Hello World');

Expand All @@ -123,7 +134,7 @@ suite('API', function () {


test('set level', function () {

testObject = aTestObject(logFile);
testObject.setLevel(0);
assert.equal(testObject.getLevel(), 0);

Expand All @@ -147,7 +158,7 @@ suite('API', function () {
});

test('Off Log', function () {

testObject = aTestObject(logFile);
testObject.setLevel(6);
testObject.critical('This message should not be written');
testObject.flush();
Expand All @@ -157,7 +168,7 @@ suite('API', function () {
});

test('set log level to trace', function () {

testObject = aTestObject(logFile);
testObject.setLevel(0);
testObject.trace('This trace message should be written');

Expand All @@ -166,7 +177,7 @@ suite('API', function () {
});

test('set log level to debug', function () {

testObject = aTestObject(logFile);
testObject.setLevel(1);
testObject.trace('This trace message should not be written');

Expand All @@ -175,7 +186,7 @@ suite('API', function () {
});

test('set log level to info', function () {

testObject = aTestObject(logFile);
testObject.setLevel(2);
testObject.trace('This trace message should not be written');
testObject.flush();
Expand All @@ -190,6 +201,7 @@ suite('API', function () {
});

test('set global log level to trace', function () {
testObject = aTestObject(logFile);
spdlog.setLevel(0);

testObject.trace('This trace message should be written');
Expand All @@ -199,6 +211,7 @@ suite('API', function () {
});

test('set global log level to debug', function () {
testObject = aTestObject(logFile);
spdlog.setLevel(1);

testObject.trace('This trace message should not be written');
Expand All @@ -208,6 +221,7 @@ suite('API', function () {
});

test('set global log level to info', function () {
testObject = aTestObject(logFile);
spdlog.setLevel(2);

testObject.trace('This trace message should not be written');
Expand All @@ -222,16 +236,16 @@ suite('API', function () {
});

test('drop logger and create logger with same name and same file', function () {
testObject.drop();

testObject = new spdlog.RotatingLogger('test', logFile, 1048576 * 5, 2);
testObject = aTestObject(logFile);
});

test('set async mode', function () {
spdlog.setAsyncMode(8192, 2000);
});

test('set pattern', function () {
testObject = aTestObject(logFile);

testObject.setPattern('%v');

testObject.info('This message should be written as is');
Expand All @@ -241,6 +255,8 @@ suite('API', function () {
});

test('clear formatters', function () {
testObject = aTestObject(logFile);

testObject.clearFormatters();

testObject.info('Cleared Formatters: ');
Expand All @@ -253,15 +269,27 @@ suite('API', function () {
assert.equal(actuals[actuals.length - 1], 'Cleared Formatters: This message should be written as is');
});

test('create log file with special characters in file name', function () {
let file = path.join(__dirname, 'abcdø', 'test.log');
filesToDelete.push(file);
testObject = new spdlog.RotatingLogger('test', file, 1048576 * 5, 2);
});

function getLastLine() {
const lines = getAllLines();
return lines[lines.length - 2];
}

function aTestObject(logfile) {
const logger = new spdlog.RotatingLogger('test', logfile, 1048576 * 5, 2);
logger.setPattern('%+');
return logger;
}

function getAllLines() {
testObject.drop();
const content = fs.readFileSync(logFile).toString();
testObject = new spdlog.RotatingLogger('test', logFile, 1048576 * 5, 2);
testObject = aTestObject(logFile);
return content.split(EOL);
}

Expand Down