Skip to content

Commit

Permalink
Support node up to and including 12.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-github committed May 6, 2019
1 parent b64ea3f commit d7908a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ node_js:
- '4'
- '5'
- '6'
- '8'
- '10'
- '11'
- '12'
sudo: false
addons:
apt:
sources:
Expand Down
61 changes: 34 additions & 27 deletions core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#include <syslog.h>

using v8::Function;
using v8::FunctionTemplate;
using v8::Handle;
using v8::Int32;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::String;
Expand All @@ -24,14 +23,15 @@ class Worker: public Nan::AsyncWorker {
}

void Execute() {
syslog(priority, "%s", message);
if(message)
syslog(priority, "%s", message);
}

void HandleOKCallback() {
Nan::HandleScope scope;

if(callback)
callback->Call(0, NULL);
callback->Call(0, NULL, async_resource);
};

private:
Expand All @@ -44,18 +44,26 @@ static char ident[1024];
// wrap: void openlog(const char *ident, int option, int facility);
NAN_METHOD(OpenLog) {

// openlog requires ident be statically allocated. Write doesn't guarantee
// NULL-termination, so preserve last byte as NULL.
info[0]->ToString()->WriteUtf8(ident, sizeof(ident)-1);
int option = info[1]->Int32Value();
int facility = info[2]->Int32Value();
const Nan::Utf8String arg0(info[0]);
Local<Int32> arg1 = Nan::To<Int32>(info[1]).ToLocalChecked();
Local<Int32> arg2 = Nan::To<Int32>(info[2]).ToLocalChecked();

// openlog() requires ident be statically allocated.
size_t length = arg0.length();
if(length) {
if(length > sizeof(ident)-1)
length = sizeof(ident)-1;
strncpy(ident, *arg0, length);
}
int option = arg1->Value();
int facility = arg2->Value();

openlog(ident, option, facility);

return;
}

static char* dupBuf(const Handle<Value>& arg) {
static char* dupBuf(const Local<Value>& arg) {
const char* mem = node::Buffer::Data(arg);
size_t memsz = node::Buffer::Length(arg);
char* s = new char[memsz + 1];
Expand All @@ -64,38 +72,36 @@ static char* dupBuf(const Handle<Value>& arg) {
return s;
}

static char* dupStr(const Local<String>& m) {
if(m.IsEmpty())
return NULL;

// Exact calculation of UTF length involves double traversal. Avoid this
// because we know UTF8 expansion is < 4 bytes out per byte in.
char* s = new char[m->Length() * 4];
m->WriteUtf8(s);
static char* dupStr(const Local<Value>& arg) {
const Nan::Utf8String str(arg);
const char* mem = *str;
size_t memsz = str.length();
char* s = new char[memsz + 1];
memcpy(s, mem, memsz);
s[memsz] = 0;
return s;
}

// wrap: void syslog(int priority, const char *format, ...);
NAN_METHOD(SysLog) {

int priority = info[0]->Int32Value();
Local<Int32> arg0 = Nan::To<Int32>(info[0]).ToLocalChecked();

int priority = arg0->Value();
char* message = NULL;
Nan::Callback *callback = NULL;

if (info[2]->IsFunction())
if(info[2]->IsFunction())
callback = new Nan::Callback(info[2].As<Function>());

if(node::Buffer::HasInstance(info[1])) {
message = dupBuf(info[1]);
} else {
message = dupStr(info[1]->ToString());
message = dupStr(info[1]);
}

if (message) {
if(message || callback) {
Nan::AsyncQueueWorker(new Worker(callback, priority, message));
} else if(callback) {
callback->Call(0, NULL);
delete callback;
}

return;
Expand All @@ -104,7 +110,8 @@ NAN_METHOD(SysLog) {
// wrap: int setlogmask(int mask);
NAN_METHOD(SetLogMask) {

int mask = info[0]->Int32Value();
Local<Int32> arg0 = Nan::To<Int32>(info[0]).ToLocalChecked();
int mask = arg0->Value();
int last = setlogmask(mask);

info.GetReturnValue().Set(Nan::New<Number>(last));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tap": "^1.3.2"
},
"dependencies": {
"nan": "^2.0.5"
"nan": "^2.13.2"
},
"tags": [
"log",
Expand Down

0 comments on commit d7908a2

Please sign in to comment.