<commit_before>/*
	# ferment.at-local

	The local (LAN) component of ferment.at.
*/
/*jshint node:true */

var app;
var async = require('async');
var bunyan = require('bunyan');
var db;
var express = require('express');
var errorHandler = require('./lib/use/errorHandler');
var fd;
var flash = require('connect-flash');
var fs = require('fs');
var http = require('http');
var levelup = require('levelup');
var moment = require('moment');
var path = require('path');
var ringbuffer = new bunyan.RingBuffer({ limit: 100 });

var MemDOWN = require('memdown');

/*
	Initialize application.
*/
app = express();

app.set('config', require('./config/' + app.get('env') + '.json'));
app.set('package', require('./package.json'));

/*
	Jade locals.
*/
app.locals.logLevels = {
	10: 'TRACE',
	20: 'DEBUG',
	30: 'INFO',
	40: 'WARN',
	50: 'ERROR',
	60: 'FATAL'
};
app.locals.logs = function () {
	var level = app.get('env') === 'production' ? bunyan.resolveLevel('info') : bunyan.resolveLevel('trace');
	var logs = [];
	
	for (var i = ringbuffer.records.length - 1; i >= 0; i--) {
		if (ringbuffer.records[i].level >= level) logs.push({
			level: ringbuffer.records[i].level,
			msg: ringbuffer.records[i].msg,
			time: ringbuffer.records[i].time
		});
	}
	return logs;
};
app.locals.moment = moment;
app.locals.version = app.get('package').version;

/*
	Check writable disk state.
*/
try {
	fs.closeSync(fs.openSync(app.get('config').touchFile, 'w'));
	app.set('environmentWritable', true);
} catch (e) {
	app.set('environmentWritable', false);
}

/*
	Services initialization.
*/
async.series({
	/*
		Bunyan logger.
	*/
	logger: function (next) {
		// Log only to a ringbuffer, except in development mode.
		app.log = bunyan.createLogger({
			name: app.get('package').name,
			serializers: bunyan.stdSerializers,
			streams: [
				{
					level: app.get('env') === 'development' ? 'trace' : 'info',
					type: 'raw',
					stream: ringbuffer
				},
				app.get('env') === 'development' ? {
					level: 'trace',
					stream: process.stdout
				} : undefined
			].filter(function (a) {return !!a;})
		});
		app.log.info({
			environment: app.get('env'),
			writable: app.get('environmentWritable')
		}, 'Starting up.');
		app.log.trace('Logger initialized.');
		next();
	},
	/*
		Database.
	*/
	database: function (next) {
		if (app.get('environmentWritable')) {
			// In a writable environment, use leveldb on disk.
			levelup('./leveldb', {
				valueEncoding: 'json'
			}, function (e, db) {
				if (e) return next(e);
				app.db = db;
				app.log.trace({
					store: 'LevelDOWN'
				}, 'LevelDB initialized using disk I/O.');
				next();
			});
		} else {
			// In a read-only environment, use MemDOWN ram store instead.
			levelup('', {
				db: MemDOWN,
				valueEncoding: 'json'
			}, function (e, db) {
				if (e) return next(e);
				app.db = db;
				app.log.trace({
					store: 'MemDOWN'
				}, 'LevelDB initialized using volatile RAM.');<commit_msg>decrease ring buffer size, expose log object<commit_after>/*
	# ferment.at-local

	The local (LAN) component of ferment.at.
*/
/*jshint node:true */

var app;
var async = require('async');
var bunyan = require('bunyan');
var db;
var express = require('express');
var errorHandler = require('./lib/use/errorHandler');
var fd;
var flash = require('connect-flash');
var fs = require('fs');
var http = require('http');
var levelup = require('levelup');
var moment = require('moment');
var path = require('path');
var ringbuffer = new bunyan.RingBuffer({ limit: 50 });

var MemDOWN = require('memdown');

/*
	Initialize application.
*/
app = express();

app.set('config', require('./config/' + app.get('env') + '.json'));
app.set('package', require('./package.json'));

/*
	Jade locals.
*/
app.locals.logLevels = {
	10: 'TRACE',
	20: 'DEBUG',
	30: 'INFO',
	40: 'WARN',
	50: 'ERROR',
	60: 'FATAL'
};
app.locals.logs = function () {
	var level = app.get('env') === 'production' ? bunyan.resolveLevel('info') : bunyan.resolveLevel('trace');
	var logs = [];
	
	for (var i = ringbuffer.records.length - 1; i >= 0; i--) {
		if (ringbuffer.records[i].level >= level) logs.push({
			level: ringbuffer.records[i].level,
			msg: ringbuffer.records[i].msg,
			object: ringbuffer.records[i],
			time: ringbuffer.records[i].time
		});
	}
	return logs;
};
app.locals.moment = moment;
app.locals.version = app.get('package').version;

/*
	Check writable disk state.
*/
try {
	fs.closeSync(fs.openSync(app.get('config').touchFile, 'w'));
	app.set('environmentWritable', true);
} catch (e) {
	app.set('environmentWritable', false);
}

/*
	Services initialization.
*/
async.series({
	/*
		Bunyan logger.
	*/
	logger: function (next) {
		// Log only to a ringbuffer, except in development mode.
		app.log = bunyan.createLogger({
			name: app.get('package').name,
			serializers: bunyan.stdSerializers,
			streams: [
				{
					level: app.get('env') === 'development' ? 'trace' : 'info',
					type: 'raw',
					stream: ringbuffer
				},
				app.get('env') === 'development' ? {
					level: 'trace',
					stream: process.stdout
				} : undefined
			].filter(function (a) {return !!a;})
		});
		app.log.info({
			environment: app.get('env'),
			writable: app.get('environmentWritable')
		}, 'Starting up.');
		app.log.trace('Logger initialized.');
		next();
	},
	/*
		Database.
	*/
	database: function (next) {
		if (app.get('environmentWritable')) {
			// In a writable environment, use leveldb on disk.
			levelup('./leveldb', {
				valueEncoding: 'json'
			}, function (e, db) {
				if (e) return next(e);
				app.db = db;
				app.log.trace({
					store: 'LevelDOWN'
				}, 'Level initialized using disk I/O.');
				next();
			});
		} else {
			// In a read-only environment, use MemDOWN ram store instead.
			levelup('', {
				db: MemDOWN,
				valueEncoding: 'json'
			}, function (e, db) {
				if (e) return next(e);
				app.db = db;
				app.log.trace({
					store: 'MemDOWN'
				}, 'Level initialized using volatile RAM.');
{'fraction_non_alphanumeric': 0.1914535666218035, 'fraction_numerical': 0.005551816958277255, 'mean_word_length': 3.4398805078416728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6727145', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2041, 'n_words': 503}