From: Darren Date: Sun, 28 Oct 2012 03:14:47 +0000 (+0000) Subject: Daemonize option and outputting to file X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=11dbb00fc1a8b894b1adb3f526c42ad305316401;p=KiwiIRC.git Daemonize option and outputting to file --- diff --git a/server/config.js b/config.js similarity index 95% rename from server/config.js rename to config.js index f4b07bf..ed20689 100644 --- a/server/config.js +++ b/config.js @@ -5,6 +5,10 @@ conf.user = ""; conf.group = ""; +// Log file location +conf.log = "kiwi.log"; + + // Server listen blocks conf.servers = []; diff --git a/kiwi.js b/kiwi.js new file mode 100644 index 0000000..751c275 --- /dev/null +++ b/kiwi.js @@ -0,0 +1,40 @@ +var kiwi_app = './server/kiwi.js'; + + +var daemon = require('daemonize2').setup({ + main: kiwi_app, + name: 'kiwiirc', + pidfile: 'kiwiirc.pid' +}); + +switch (process.argv[2]) { + case '-f': + require(kiwi_app); + break; + + case 'start': + daemon.start(); + break; + + case 'stop': + daemon.stop(); + break; + + case 'restart': + daemon.stop(function(err) { + daemon.start(); + }); + break; + + case 'status': + var pid = daemon.status(); + if (pid) + console.log('Daemon running. PID: ' + pid); + else + console.log('Daemon is not running.'); + break; + + + default: + console.log('Usage: [-f|start|stop|restart|status]'); +} \ No newline at end of file diff --git a/package.json b/package.json index e10d160..b5fdaed 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "node-static": "0.5.9", "uglify-js": "1.2.3", "socket.io": "0.8.7", - "underscore": "1.3.3" + "underscore": "1.3.3", + "daemonize2": "0.4.0-rc.5" } } \ No newline at end of file diff --git a/server/configuration.js b/server/configuration.js index 99581c0..aaf9513 100644 --- a/server/configuration.js +++ b/server/configuration.js @@ -1,9 +1,9 @@ var fs = require('fs'); var config_filename = 'config.js', - config_dirs = ['/etc/kiwiirc/', __dirname + '/'], + config_dirs = ['/etc/kiwiirc/', __dirname + '/../'], environment = 'production', - loaded_config = Object.create(null); + loaded_config = Object.create(null); function loadConfig() { @@ -21,7 +21,6 @@ function loadConfig() { // Try load the new config file new_config = require(conf_filepath); - console.log('Loaded config file ' + config_dirs[i] + config_filename); break; } } catch (e) { @@ -37,24 +36,24 @@ function loadConfig() { } if (new_config) { - loaded_config = new_config; - return loaded_config; + loaded_config = new_config; + return loaded_config; } else { - return false; + return false; } } module.exports.setEnvironment = function (new_environment) { - environment = new_environment; + environment = new_environment; }; // Get the current config. Optionally for a different environment than currently set module.exports.get = function (specific_environment) { - specific_environment = specific_environment || environment; - - return loaded_config[specific_environment]; + specific_environment = specific_environment || environment; + + return loaded_config[specific_environment]; }; module.exports.loadConfig = loadConfig; \ No newline at end of file diff --git a/server/kiwi.js b/server/kiwi.js index 9784930..7db9138 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -6,9 +6,40 @@ var fs = require('fs'), +process.chdir(__dirname + '/../'); +config.loadConfig(); + + +// If we're not running in the forground and we have a log file.. switch +// console.log to output to a file +if (process.argv.indexOf('-f') === -1 && config.get().log) { + (function () { + var log_file_name = config.get().log; + + if (log_file_name[0] !== '/') { + log_file_name = __dirname + '/../' + log_file_name; + } + + + + console.log = function() { + var logfile = fs.openSync(log_file_name, 'a'), + out; + + out = Array.prototype.join.apply(arguments, [' ']); + + // Make sure we out somthing to log and we have an open file + if (!out || !logfile) return; + + out += '\n'; + fs.writeSync(logfile, out, null); + + fs.closeSync(logfile); + }; + })(); +} -config.loadConfig(); // Make sure we have a valid config file and at least 1 server if (Object.keys(config.get()).length === 0) { diff --git a/server/weblistener.js b/server/weblistener.js index 33e93ba..abde51f 100644 --- a/server/weblistener.js +++ b/server/weblistener.js @@ -64,7 +64,8 @@ var WebListener = function (web_config, transports) { console.log('Listening on ' + web_config.address + ':' + web_config.port.toString() + ' without SSL'); } - this.ws.set('log level', 1); + this.ws.set('log level', 0); + this.ws.set('log color', false); this.ws.enable('browser client minification'); this.ws.enable('browser client etag'); this.ws.set('transports', transports);