forked from schamane/node-syslog
/
syslog.cc
150 lines (120 loc) · 3.1 KB
/
syslog.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "node-syslog.h"
#include "compat-inl.h"
using namespace v8;
using compat::ReturnType;
using compat::ArgumentType;
using compat::ReturnableHandleScope;
bool Syslog::connected_ = false;
char Syslog::name[1024];
void
Syslog::Initialize ( Handle<Object> target)
{
NODE_SET_METHOD(target, "init", Syslog::init);
NODE_SET_METHOD(target, "log", Syslog::log);
NODE_SET_METHOD(target, "setMask", Syslog::setMask);
NODE_SET_METHOD(target, "close", Syslog::destroy);
}
ReturnType
Syslog::init ( const ArgumentType& args)
{
ReturnableHandleScope scope(args);
if (args.Length() == 0 || !args[0]->IsString()) {
return scope.ThrowError("Must give daemonname string as argument");
}
if (args.Length() < 3 ) {
return scope.ThrowError("Must have at least 3 params as argument");
}
if(connected_)
close();
//open syslog
args[0]->ToString()->WriteUtf8(name);
int options = args[1]->ToInt32()->Value();
int facility = args[2]->ToInt32()->Value();
open( options , facility );
return scope.Return();
}
struct log_request {
uv_work_t work;
uint32_t log_level;
char msg[1]; // variable length msg buffer
};
static void UV_AfterLog(uv_work_t *req, int) {
free(req->data);
}
static void UV_Log(uv_work_t *req) {
struct log_request *log_req = (struct log_request *)(req->data);
syslog(log_req->log_level, "%s", log_req->msg);
return;
}
ReturnType
Syslog::log ( const ArgumentType& args)
{
ReturnableHandleScope scope(args);
uint32_t log_level = args[0]->Int32Value();
String::Utf8Value msg(args[1]);
if(!connected_)
return scope.ThrowError("Init method has to be called before syslog");
if(!args[1]->IsString()) {
return scope.ThrowError("Log message must be a string");
}
struct log_request * log_req = (struct log_request *) malloc(
sizeof(*log_req) + msg.length());
if(!log_req) {
return scope.ThrowError("Could not allocate enough memory");
}
log_req->work.data = log_req;
log_req->log_level = log_level;
strcpy(&log_req->msg[0], *msg);
int status = uv_queue_work(uv_default_loop(), &log_req->work,
UV_Log, UV_AfterLog);
assert(status == 0);
return scope.Return();
}
ReturnType
Syslog::destroy ( const ArgumentType& args)
{
ReturnableHandleScope scope(args);
close();
return scope.Return();
}
void
Syslog::open ( int option, int facility)
{
openlog( name, option, facility );
connected_ = true;
}
ReturnType
Syslog::setMask ( const ArgumentType& args)
{
bool upTo = false;
int mask, value;
ReturnableHandleScope scope(args);
if (args.Length() < 1) {
return scope.ThrowError("You must provide an mask");
}
if (!args[0]->IsNumber()) {
return scope.ThrowError("First parameter (mask) should be numeric");
}
if (args.Length() == 2 && !args[1]->IsBoolean()) {
return scope.ThrowError("Second parameter (upTo) should be boolean");
}
if (args.Length() == 2 && args[1]->IsBoolean()) {
upTo = true;
}
value = args[0]->Int32Value();
if(upTo) {
mask = LOG_UPTO(value);
} else {
mask = LOG_MASK(value);
}
return scope.Return(setlogmask(mask));
}
void
Syslog::close ()
{
if(connected_) {
closelog();
connected_ = false;
}
}
NODE_MODULE(syslog, Syslog::Initialize);