@trasherdk/posix-syslog 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +19 -0
- package/README.md +110 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
- package/src/syslog.cc +144 -0
package/CMakeLists.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.6)
|
|
2
|
+
cmake_policy(SET CMP0042 NEW)
|
|
3
|
+
|
|
4
|
+
project (syslog)
|
|
5
|
+
|
|
6
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
7
|
+
add_definitions(-DNAPI_VERSION=9)
|
|
8
|
+
|
|
9
|
+
include_directories(${CMAKE_JS_INC})
|
|
10
|
+
|
|
11
|
+
set(SOURCE_FILES "src/syslog.cc")
|
|
12
|
+
|
|
13
|
+
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
|
|
14
|
+
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
|
|
15
|
+
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
|
|
16
|
+
|
|
17
|
+
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
|
|
18
|
+
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
|
|
19
|
+
endif()
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# posix-syslog
|
|
2
|
+
|
|
3
|
+
POSIX syslog bindings for Node.js — based on code from [node-posix](https://github.com/ohmu/node-posix).
|
|
4
|
+
- Extracted from a fork [node-posix](https://github.com/ilb/node-posix)
|
|
5
|
+
|
|
6
|
+
Provides `openlog`, `closelog`, `syslog`, `setlogmask`, and `createLogger` as a native C++ addon using N-API, with full TypeScript types and ESM support.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm install posix-syslog
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { openlog, syslog, closelog } from "posix-syslog";
|
|
18
|
+
|
|
19
|
+
openlog("myprog", { pid: true, ndelay: true }, "local7");
|
|
20
|
+
syslog("info", "hello, world!");
|
|
21
|
+
closelog();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Multiple facilities
|
|
25
|
+
|
|
26
|
+
Use `createLogger()` to direct messages to different syslog facilities
|
|
27
|
+
within the same process. Call `openlog()` once to set identity and options,
|
|
28
|
+
then create loggers for each facility:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { openlog, createLogger, closelog } from "posix-syslog";
|
|
32
|
+
|
|
33
|
+
openlog("myapp", { pid: true, ndelay: true }, "user");
|
|
34
|
+
|
|
35
|
+
const auth = createLogger("auth");
|
|
36
|
+
const app = createLogger("local0");
|
|
37
|
+
|
|
38
|
+
auth.info("login successful");
|
|
39
|
+
app.warning("disk usage high");
|
|
40
|
+
app.debug("processing request #42");
|
|
41
|
+
|
|
42
|
+
closelog();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### openlog(identity, options, facility)
|
|
48
|
+
|
|
49
|
+
Open a connection to the logger.
|
|
50
|
+
|
|
51
|
+
- `identity` — name of the process visible in logged entries.
|
|
52
|
+
- `options` — object with boolean flags:
|
|
53
|
+
- `cons` — Log to the system console on error.
|
|
54
|
+
- `ndelay` — Connect to syslog daemon immediately.
|
|
55
|
+
- `nowait` — Do not wait for child processes.
|
|
56
|
+
- `odelay` — Delay open until syslog() is called.
|
|
57
|
+
- `pid` — Log the process ID with each message.
|
|
58
|
+
- `facility` — facility code string:
|
|
59
|
+
`kern`, `user`, `mail`, `news`, `uucp`, `daemon`, `auth`, `authpriv`, `cron`, `ftp`, `lpr`, `syslog`, `local0`..`local7`
|
|
60
|
+
|
|
61
|
+
Only `user` and `local0`..`local7` are defined in the POSIX standard.
|
|
62
|
+
|
|
63
|
+
### closelog()
|
|
64
|
+
|
|
65
|
+
Close connection to the logger.
|
|
66
|
+
|
|
67
|
+
### setlogmask(mask)
|
|
68
|
+
|
|
69
|
+
Sets a priority mask for log messages. Further `syslog()` calls are only sent if their priority is included in the mask. Returns an object with boolean flags indicating which priorities were previously enabled.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
setlogmask({ emerg: true, alert: true, crit: true });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### syslog(priority, message)
|
|
76
|
+
|
|
77
|
+
Send a message to the syslog logger.
|
|
78
|
+
|
|
79
|
+
Priorities: `emerg`, `alert`, `crit`, `err`, `warning`, `notice`, `info`, `debug`
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
syslog("info", "hello, world!");
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### createLogger(facility)
|
|
86
|
+
|
|
87
|
+
Create a logger bound to a specific facility. The facility is OR'd into the
|
|
88
|
+
priority on each call, overriding the default set by `openlog()`.
|
|
89
|
+
|
|
90
|
+
Returns an object with a method for each priority level:
|
|
91
|
+
`emerg`, `alert`, `crit`, `err`, `warning`, `notice`, `info`, `debug`
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const log = createLogger("auth");
|
|
95
|
+
log.info("user logged in");
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## TypeScript
|
|
99
|
+
|
|
100
|
+
All exports are fully typed. Key types:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import type {
|
|
104
|
+
SyslogPriority, SyslogFacility, SyslogOptions, SyslogMask, Logger,
|
|
105
|
+
} from "posix-syslog";
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
MIT — Copyright (c) 2011-2015 Mika Eloranta
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type SyslogPriority = "emerg" | "alert" | "crit" | "err" | "warning" | "notice" | "info" | "debug";
|
|
2
|
+
export type SyslogFacility = "kern" | "user" | "mail" | "news" | "uucp" | "daemon" | "auth" | "authpriv" | "cron" | "ftp" | "lpr" | "syslog" | "local0" | "local1" | "local2" | "local3" | "local4" | "local5" | "local6" | "local7";
|
|
3
|
+
export interface SyslogOptions {
|
|
4
|
+
cons?: boolean;
|
|
5
|
+
ndelay?: boolean;
|
|
6
|
+
nowait?: boolean;
|
|
7
|
+
odelay?: boolean;
|
|
8
|
+
pid?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export type SyslogMask = Partial<Record<SyslogPriority, boolean>>;
|
|
11
|
+
export declare function openlog(ident: string, option: SyslogOptions, facility: SyslogFacility): void;
|
|
12
|
+
export declare function closelog(): void;
|
|
13
|
+
export declare function syslog(priority: SyslogPriority, message: string): void;
|
|
14
|
+
export declare function setlogmask(maskpri: SyslogMask): SyslogMask;
|
|
15
|
+
export interface Logger {
|
|
16
|
+
emerg(message: string): void;
|
|
17
|
+
alert(message: string): void;
|
|
18
|
+
crit(message: string): void;
|
|
19
|
+
err(message: string): void;
|
|
20
|
+
warning(message: string): void;
|
|
21
|
+
notice(message: string): void;
|
|
22
|
+
info(message: string): void;
|
|
23
|
+
debug(message: string): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a logger bound to a specific facility. The facility is OR'd into
|
|
27
|
+
* the priority on each call, overriding the default set by openlog().
|
|
28
|
+
* openlog() must still be called first to set identity and options.
|
|
29
|
+
*/
|
|
30
|
+
export declare function createLogger(facility: SyslogFacility): Logger;
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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AAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,cAAc,GAAG,MAAM,CAe7D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
const native = require("../build/Release/syslog.node");
|
|
4
|
+
const syslogConstants = {};
|
|
5
|
+
native.update_syslog_constants(syslogConstants);
|
|
6
|
+
function syslogConst(value) {
|
|
7
|
+
if (syslogConstants[value] === undefined) {
|
|
8
|
+
throw new Error("invalid syslog constant value: " + value);
|
|
9
|
+
}
|
|
10
|
+
return syslogConstants[value];
|
|
11
|
+
}
|
|
12
|
+
function syslogFlags(option, prefix = "") {
|
|
13
|
+
let opt = 0;
|
|
14
|
+
for (const key of Object.keys(option)) {
|
|
15
|
+
const flag = syslogConst(prefix + key);
|
|
16
|
+
opt |= option[key] ? flag : 0;
|
|
17
|
+
}
|
|
18
|
+
return opt;
|
|
19
|
+
}
|
|
20
|
+
export function openlog(ident, option, facility) {
|
|
21
|
+
native.openlog(ident, syslogFlags(option), syslogConst(facility));
|
|
22
|
+
}
|
|
23
|
+
export function closelog() {
|
|
24
|
+
native.closelog();
|
|
25
|
+
}
|
|
26
|
+
export function syslog(priority, message) {
|
|
27
|
+
native.syslog(syslogConst(priority), message);
|
|
28
|
+
}
|
|
29
|
+
export function setlogmask(maskpri) {
|
|
30
|
+
const bits = native.setlogmask(syslogFlags(maskpri, "mask_"));
|
|
31
|
+
const flags = {};
|
|
32
|
+
for (const key of Object.keys(syslogConstants)) {
|
|
33
|
+
if (key.startsWith("mask_")) {
|
|
34
|
+
const name = key.slice(5);
|
|
35
|
+
flags[name] = (bits & syslogConstants[key]) !== 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return flags;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create a logger bound to a specific facility. The facility is OR'd into
|
|
42
|
+
* the priority on each call, overriding the default set by openlog().
|
|
43
|
+
* openlog() must still be called first to set identity and options.
|
|
44
|
+
*/
|
|
45
|
+
export function createLogger(facility) {
|
|
46
|
+
const f = syslogConst(facility);
|
|
47
|
+
const log = (priority, message) => native.syslog(f | syslogConst(priority), message);
|
|
48
|
+
return {
|
|
49
|
+
emerg: (message) => log("emerg", message),
|
|
50
|
+
alert: (message) => log("alert", message),
|
|
51
|
+
crit: (message) => log("crit", message),
|
|
52
|
+
err: (message) => log("err", message),
|
|
53
|
+
warning: (message) => log("warning", message),
|
|
54
|
+
notice: (message) => log("notice", message),
|
|
55
|
+
info: (message) => log("info", message),
|
|
56
|
+
debug: (message) => log("debug", message),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAU/C,MAAM,MAAM,GAAmB,OAAO,CAAC,8BAA8B,CAAC,CAAC;AAEvE,MAAM,eAAe,GAA2B,EAAE,CAAC;AACnD,MAAM,CAAC,uBAAuB,CAAC,eAAe,CAAC,CAAC;AAEhD,SAAS,WAAW,CAAC,KAAa;IAChC,IAAI,eAAe,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,C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
|
+
"name": "@trasherdk/posix-syslog",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "POSIX syslog bindings for Node.js",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src/*.cc",
|
|
18
|
+
"CMakeLists.txt"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"posix",
|
|
22
|
+
"syslog",
|
|
23
|
+
"openlog",
|
|
24
|
+
"closelog",
|
|
25
|
+
"setlogmask",
|
|
26
|
+
"logging",
|
|
27
|
+
"system"
|
|
28
|
+
],
|
|
29
|
+
"os": [
|
|
30
|
+
"linux",
|
|
31
|
+
"darwin",
|
|
32
|
+
"freebsd",
|
|
33
|
+
"openbsd",
|
|
34
|
+
"netbsd",
|
|
35
|
+
"sunos",
|
|
36
|
+
"aix"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"cmake-js": "^8.0.0",
|
|
43
|
+
"node-addon-api": "^8.5.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^25.3.0",
|
|
47
|
+
"typescript": "^5.9.3"
|
|
48
|
+
},
|
|
49
|
+
"binary": {
|
|
50
|
+
"napi_versions": [
|
|
51
|
+
9
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build:native": "cmake-js compile --target=syslog",
|
|
56
|
+
"build:ts": "tsc",
|
|
57
|
+
"build": "tsc && cmake-js compile --target=syslog",
|
|
58
|
+
"install": "cmake-js compile --target=syslog",
|
|
59
|
+
"test": "node --test test/test-syslog.ts"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/syslog.cc
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <syslog.h>
|
|
4
|
+
|
|
5
|
+
// openlog() first argument (const char* ident) is not guaranteed to be
|
|
6
|
+
// copied within the openlog() call so we need to keep it in a safe location
|
|
7
|
+
static const size_t MAX_SYSLOG_IDENT=100;
|
|
8
|
+
static char syslog_ident[MAX_SYSLOG_IDENT+1] = {0};
|
|
9
|
+
|
|
10
|
+
Napi::Value node_openlog(const Napi::CallbackInfo& info) {
|
|
11
|
+
Napi::Env env = info.Env();
|
|
12
|
+
|
|
13
|
+
if (info.Length() != 3) {
|
|
14
|
+
Napi::Error::New(env, "openlog: requires exactly 3 arguments").ThrowAsJavaScriptException();
|
|
15
|
+
return env.Null();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
std::string ident_str = info[0].As<Napi::String>();
|
|
19
|
+
const char *ident = ident_str.data();
|
|
20
|
+
strncpy(syslog_ident, ident, MAX_SYSLOG_IDENT);
|
|
21
|
+
syslog_ident[MAX_SYSLOG_IDENT] = 0;
|
|
22
|
+
if (!info[1].IsNumber() || !info[2].IsNumber()) {
|
|
23
|
+
Napi::Error::New(env, "openlog: invalid argument values").ThrowAsJavaScriptException();
|
|
24
|
+
return env.Null();
|
|
25
|
+
}
|
|
26
|
+
openlog(syslog_ident, info[1].ToNumber().Int32Value(), info[2].ToNumber().Int32Value());
|
|
27
|
+
|
|
28
|
+
return env.Undefined();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
Napi::Value node_closelog(const Napi::CallbackInfo& info) {
|
|
32
|
+
Napi::Env env = info.Env();
|
|
33
|
+
|
|
34
|
+
if (info.Length() != 0) {
|
|
35
|
+
Napi::Error::New(env, "closelog: does not take any arguments").ThrowAsJavaScriptException();
|
|
36
|
+
return env.Null();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
closelog();
|
|
40
|
+
|
|
41
|
+
return env.Undefined();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Napi::Value node_syslog(const Napi::CallbackInfo& info) {
|
|
45
|
+
Napi::Env env = info.Env();
|
|
46
|
+
|
|
47
|
+
if (info.Length() != 2) {
|
|
48
|
+
Napi::Error::New(env, "syslog: requires exactly 2 arguments").ThrowAsJavaScriptException();
|
|
49
|
+
return env.Null();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
std::string message_str = info[1].As<Napi::String>();
|
|
53
|
+
const char *message = message_str.data();
|
|
54
|
+
syslog(info[0].ToNumber().Int32Value(), "%s", message);
|
|
55
|
+
|
|
56
|
+
return env.Undefined();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
Napi::Value node_setlogmask(const Napi::CallbackInfo& info) {
|
|
60
|
+
Napi::Env env = info.Env();
|
|
61
|
+
|
|
62
|
+
if (info.Length() != 1) {
|
|
63
|
+
Napi::Error::New(env, "setlogmask: takes exactly 1 argument").ThrowAsJavaScriptException();
|
|
64
|
+
return env.Null();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return Napi::Number::New(env, setlogmask(info[0].ToNumber().Int32Value()));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#define ADD_MASK_FLAG(name, flag) \
|
|
71
|
+
(obj).Set(Napi::String::New(env, name), Napi::Number::New(env, flag)); \
|
|
72
|
+
(obj).Set(Napi::String::New(env, "mask_" name), Napi::Number::New(env, LOG_MASK(flag)));
|
|
73
|
+
|
|
74
|
+
Napi::Value node_update_syslog_constants(const Napi::CallbackInfo& info) {
|
|
75
|
+
Napi::Env env = info.Env();
|
|
76
|
+
|
|
77
|
+
if (info.Length() != 1) {
|
|
78
|
+
Napi::Error::New(env, "update_syslog_constants: takes exactly 1 argument").ThrowAsJavaScriptException();
|
|
79
|
+
return env.Null();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!info[0].IsObject()) {
|
|
83
|
+
Napi::TypeError::New(env, "update_syslog_constants: argument must be an object").ThrowAsJavaScriptException();
|
|
84
|
+
return env.Null();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Napi::Object obj = info[0].As<Napi::Object>();
|
|
88
|
+
|
|
89
|
+
// priority constants + their LOG_MASK() values
|
|
90
|
+
ADD_MASK_FLAG("emerg", LOG_EMERG);
|
|
91
|
+
ADD_MASK_FLAG("alert", LOG_ALERT);
|
|
92
|
+
ADD_MASK_FLAG("crit", LOG_CRIT);
|
|
93
|
+
ADD_MASK_FLAG("err", LOG_ERR);
|
|
94
|
+
ADD_MASK_FLAG("warning", LOG_WARNING);
|
|
95
|
+
ADD_MASK_FLAG("notice", LOG_NOTICE);
|
|
96
|
+
ADD_MASK_FLAG("info", LOG_INFO);
|
|
97
|
+
ADD_MASK_FLAG("debug", LOG_DEBUG);
|
|
98
|
+
|
|
99
|
+
// facility constants
|
|
100
|
+
(obj).Set(Napi::String::New(env, "auth"), Napi::Number::New(env, LOG_AUTH));
|
|
101
|
+
#ifdef LOG_AUTHPRIV
|
|
102
|
+
(obj).Set(Napi::String::New(env, "authpriv"), Napi::Number::New(env, LOG_AUTHPRIV));
|
|
103
|
+
#endif
|
|
104
|
+
(obj).Set(Napi::String::New(env, "cron"), Napi::Number::New(env, LOG_CRON));
|
|
105
|
+
(obj).Set(Napi::String::New(env, "daemon"), Napi::Number::New(env, LOG_DAEMON));
|
|
106
|
+
#ifdef LOG_FTP
|
|
107
|
+
(obj).Set(Napi::String::New(env, "ftp"), Napi::Number::New(env, LOG_FTP));
|
|
108
|
+
#endif
|
|
109
|
+
(obj).Set(Napi::String::New(env, "kern"), Napi::Number::New(env, LOG_KERN));
|
|
110
|
+
(obj).Set(Napi::String::New(env, "lpr"), Napi::Number::New(env, LOG_LPR));
|
|
111
|
+
(obj).Set(Napi::String::New(env, "mail"), Napi::Number::New(env, LOG_MAIL));
|
|
112
|
+
(obj).Set(Napi::String::New(env, "news"), Napi::Number::New(env, LOG_NEWS));
|
|
113
|
+
(obj).Set(Napi::String::New(env, "syslog"), Napi::Number::New(env, LOG_SYSLOG));
|
|
114
|
+
(obj).Set(Napi::String::New(env, "user"), Napi::Number::New(env, LOG_USER));
|
|
115
|
+
(obj).Set(Napi::String::New(env, "uucp"), Napi::Number::New(env, LOG_UUCP));
|
|
116
|
+
(obj).Set(Napi::String::New(env, "local0"), Napi::Number::New(env, LOG_LOCAL0));
|
|
117
|
+
(obj).Set(Napi::String::New(env, "local1"), Napi::Number::New(env, LOG_LOCAL1));
|
|
118
|
+
(obj).Set(Napi::String::New(env, "local2"), Napi::Number::New(env, LOG_LOCAL2));
|
|
119
|
+
(obj).Set(Napi::String::New(env, "local3"), Napi::Number::New(env, LOG_LOCAL3));
|
|
120
|
+
(obj).Set(Napi::String::New(env, "local4"), Napi::Number::New(env, LOG_LOCAL4));
|
|
121
|
+
(obj).Set(Napi::String::New(env, "local5"), Napi::Number::New(env, LOG_LOCAL5));
|
|
122
|
+
(obj).Set(Napi::String::New(env, "local6"), Napi::Number::New(env, LOG_LOCAL6));
|
|
123
|
+
(obj).Set(Napi::String::New(env, "local7"), Napi::Number::New(env, LOG_LOCAL7));
|
|
124
|
+
|
|
125
|
+
// option constants
|
|
126
|
+
(obj).Set(Napi::String::New(env, "pid"), Napi::Number::New(env, LOG_PID));
|
|
127
|
+
(obj).Set(Napi::String::New(env, "cons"), Napi::Number::New(env, LOG_CONS));
|
|
128
|
+
(obj).Set(Napi::String::New(env, "ndelay"), Napi::Number::New(env, LOG_NDELAY));
|
|
129
|
+
(obj).Set(Napi::String::New(env, "odelay"), Napi::Number::New(env, LOG_ODELAY));
|
|
130
|
+
(obj).Set(Napi::String::New(env, "nowait"), Napi::Number::New(env, LOG_NOWAIT));
|
|
131
|
+
|
|
132
|
+
return env.Undefined();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
Napi::Object init(Napi::Env env, Napi::Object exports) {
|
|
136
|
+
exports.Set("openlog", Napi::Function::New(env, node_openlog));
|
|
137
|
+
exports.Set("closelog", Napi::Function::New(env, node_closelog));
|
|
138
|
+
exports.Set("syslog", Napi::Function::New(env, node_syslog));
|
|
139
|
+
exports.Set("setlogmask", Napi::Function::New(env, node_setlogmask));
|
|
140
|
+
exports.Set("update_syslog_constants", Napi::Function::New(env, node_update_syslog_constants));
|
|
141
|
+
return exports;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
NODE_API_MODULE(syslog, init);
|