process.on
(
	'uncaughtException',
	function (err)
	{
		var stack = err.stack;
		var timeout = 1;
		
		// print note to logger
		logger.log("SERVER CRASHED!");
		// logger.printLastLogs();
		logger.log(err, stack);
		
		
		// save log to timestamped logfile
		// var filename = "crash_" + _2.formatDate(new Date()) + ".log";
		// logger.log("LOGGING ERROR TO "+filename);
		// var fs = require('fs');
		// fs.writeFile('logs/'+filename, log);
		
		
		// email log to developer
		if(helper.Config.get('email_on_error') == 'true')
		{
			logger.log("EMAILING ERROR");
			require('./Mailer'); // this is a simple wrapper around nodemailer http://documentup.com/andris9/nodemailer/
			helper.Mailer.sendMail("GAMEHUB NODE SERVER CRASHED", stack);
			timeout = 10;
		}
		
		// Send signal to clients
//		logger.log("EMITTING SERVER DOWN CODE");
//		helper.IO.emit(SIGNALS.SERVER.DOWN, "The server has crashed unexpectedly. Restarting in 10s..");
		
		
		// If we exit straight away, the write log and send email operations wont have time to run
		setTimeout
		(
			function()
			{
				logger.log("KILLING PROCESS");
				process.exit();
			},
			// timeout * 1000
			timeout * 100000 // extra time. pm2 auto-restarts on crash...
		);
	}
);