Skip to content
This repository

Add syslog support #6

Closed
wants to merge 6 commits into from

2 participants

Steffen Zieger Mike Krieger
Steffen Zieger
saz commented

Add syslog support to node2dm.

node-syslog is required for syslog support.

Mike Krieger
Owner

Hey saz--thanks for the pull request. Can I just cherry-pick that last commit (9404)?

Steffen Zieger
saz commented

Woops. 9404 is the right one. Sorry for that.

Mike Krieger
Owner

Thanks man, merged.

Mike Krieger mikeyk closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 31 additions and 13 deletions. Show diff stats Hide diff stats

  1. +1 0  exampleConfig.js
  2. +30 13 node2dm.js
1  exampleConfig.js
@@ -17,5 +17,6 @@
17 17 var config = {}
18 18 config.port = 8120;
19 19 config.address= "127.0.0.1";
  20 + config.syslog = false;
20 21 module.exports = config;
21 22
43 node2dm.js 100644 → 100755
@@ -10,6 +10,15 @@ var dgram = require('dgram')
10 10 , fs = require('fs')
11 11 , net = require('net')
12 12
  13 +if (config.syslog) {
  14 + try {
  15 + var syslog = require('node-syslog');
  16 + syslog.init('node2dm', syslog.LOG_PID | syslog.LOG_ODELAY, syslog.LOG_DAEMON);
  17 + } catch (e) {
  18 + config.syslog = false;
  19 + log('node-syslog is required for syslog support.');
  20 + }
  21 +}
13 22
14 23 function userConfig() {
15 24 return process.argv[2] ?
@@ -17,6 +26,14 @@ function userConfig() {
17 26 'config'
18 27 }
19 28
  29 +function log(msg) {
  30 + if (config.syslog) {
  31 + syslog.log(syslog.LOG_INFO, msg);
  32 + } else {
  33 + util.log(msg);
  34 + }
  35 +}
  36 +
20 37 function C2DMMessage(deviceToken, collapseKey, notification) {
21 38 this.deviceToken = deviceToken;
22 39 this.collapseKey = collapseKey;
@@ -29,7 +46,7 @@ function C2DMReceiver(config, connection) {
29 46
30 47 var msgParts = msg.toString().match(/^([^:]+):([^:]+):(.*)$/);
31 48 if (!msgParts) {
32   - util.log("Invalid message");
  49 + log("Invalid message");
33 50 return;
34 51 };
35 52 var token = msgParts[1];
@@ -40,7 +57,7 @@ function C2DMReceiver(config, connection) {
40 57 connection.notifyDevice(c2dmMessage);
41 58 });
42 59 this.server.bind(config.port || 8120);
43   - util.log("server is up");
  60 + log("server is up");
44 61 }
45 62
46 63
@@ -149,12 +166,12 @@ function C2DMConnection(config) {
149 166 totalErrors++;
150 167 var errMessage = err.match(/Error=(.+)$/);
151 168 if (!errMessage) {
152   - util.log("Unknown error: " + err);
  169 + log("Unknown error: " + err);
153 170 }
154 171 var googleError = errMessage[1];
155 172 switch (googleError) {
156 173 case "QuotaExceeded":
157   - util.log("WARNING: Google Quota Exceeded");
  174 + log("WARNING: Google Quota Exceeded");
158 175 // write a lock file; will require manual intervention
159 176 fs.open('./quota.lock', 'w', '0666', function(e, id) {
160 177 fs.write(id, 'locked at ' + new Date().toString(), null, 'utf8', function() {
@@ -178,7 +195,7 @@ function C2DMConnection(config) {
178 195 break;
179 196
180 197 case "MessageTooBig":
181   - util.log("ERROR: message too big");
  198 + log("ERROR: message too big");
182 199 break;
183 200
184 201 }
@@ -191,7 +208,7 @@ function C2DMConnection(config) {
191 208 return;
192 209 }
193 210 if (self.rateLimitedTokens[message.deviceToken]) {
194   - util.log("not sending; this token has been rate limited");
  211 + log("not sending; this token has been rate limited");
195 212 return;
196 213 }
197 214
@@ -244,7 +261,7 @@ function C2DMConnection(config) {
244 261
245 262 postRequest.on('error', function(error) {
246 263 totalErrors++;
247   - util.log(error);
  264 + log(error);
248 265 });
249 266
250 267 postRequest.write(stringBody);
@@ -269,7 +286,7 @@ function C2DMConnection(config) {
269 286 return;
270 287 }
271 288 if (self.authFails > 10) {
272   - util.log("Could not auth after 10 attempts!");
  289 + log("Could not auth after 10 attempts!");
273 290 process.exit(1);
274 291 }
275 292
@@ -298,19 +315,19 @@ function C2DMConnection(config) {
298 315 self.authFails = 0;
299 316 self.emit('loginComplete');
300 317 } else {
301   - util.log("Auth fail; body: " + buffer);
  318 + log("Auth fail; body: " + buffer);
302 319 if (buffer.match(/CaptchaToken/)) {
303   - util.log("Must auth with captcha; exiting");
  320 + log("Must auth with captcha; exiting");
304 321 process.exit(1);
305 322 }
306 323 self.authFails++;
307 324 }
308   - util.log('auth token: ' + self.currentAuthorizationToken);
  325 + log('auth token: ' + self.currentAuthorizationToken);
309 326 authInProgress = false;
310 327 });
311 328 });
312 329 loginReq.on('error', function(e) {
313   - util.log(e);
  330 + log(e);
314 331 authInProgress = false;
315 332 });
316 333 loginReq.write(loginBodyString);
@@ -375,7 +392,7 @@ util.inherits(C2DMConnection, emitter);
375 392 // don't start until removed
376 393 fs.stat('quota.lock', function(err, stats) {
377 394 if (!err) {
378   - util.log("Can't start; quota.lock present");
  395 + log("Can't start; quota.lock present");
379 396 process.exit(1);
380 397 }
381 398

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.