From 68d2362666f7349636bc597e203e278e7b5d4a1a Mon Sep 17 00:00:00 2001 From: Benjamin Kiessling Date: Fri, 2 Mar 2012 23:34:04 +0100 Subject: Add new logging module --- config.js.example | 3 +- main.js | 14 ++++--- model_fs.js | 115 ++++++++++++++++++++++++++++++------------------------ 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/config.js.example b/config.js.example index 38c3cc6..99e61f3 100644 --- a/config.js.example +++ b/config.js.example @@ -6,6 +6,7 @@ exports.Config = { templateFile: 'template.html', descriptionFile: 'desc.js', eventFile: 'events.js', - ignoreDotFiles: 'false', + fetchInterval: 21600000, + logLevel: 'info', port: 8080 }; diff --git a/main.js b/main.js index 4b25c4e..1f26faa 100755 --- a/main.js +++ b/main.js @@ -1,5 +1,8 @@ #! /usr/bin/env node +var path = require('path'); +var logging = require('node-logging'); +var express = require('express'); var args = require('optimist') .usage('Usage: $0 -c configuration [-h]') .default('c', './config') @@ -12,23 +15,22 @@ if(args.h) { return; } -var path = require('path'); - process.on('uncaughtException', function(err) { console.error('uncaughtException: ' + (err.stack || err.message || err.toString())); }); process.chdir(__dirname); + var config = require('./config'); +logging.setLevel(config.logLevel || 'info'); -var express = require('express'); var app = express(); - -app.use(express.logger()); +app.use(logging.requestLogger); app.use(express.staticCache()); app.use(express.static(path.join(__dirname, config.Config.rootDir))); - config.Config.app = app; +config.Config.logger = logging; + var model = require('./model_' + config.modelBackend); model.start(config.Config); diff --git a/model_fs.js b/model_fs.js index 1a2dfbd..91234ba 100644 --- a/model_fs.js +++ b/model_fs.js @@ -11,20 +11,23 @@ var files = []; exports.start = function(config) { configuration = config; - if(typeof configuration.ignoreDotFiles !== 'undefined' && configuration.ignoreDotFiles == 'false') { + if(configuration.ignoreDotFiles == 'false') { watcher = require('watch-tree').watchTree(configuration.rootDir, {}); } else { watcher = require('watch-tree').watchTree(configuration.rootDir, {ignore: '.*'}); } - watcher.on('fileDeleted', exports.rebuildFiles); - watcher.on('fileCreated', exports.rebuildFiles); - watcher.on('fileModified', exports.rebuildFiles); - exports.rebuildFiles(); + if(typeof configuration.fetchInterval == 'number' && configuration.fetchInterval > 0) { + setInterval(fetchAllEvents, configuration.fetchInterval); + } + watcher.on('fileDeleted', rebuildFiles); + watcher.on('fileCreated', rebuildFiles); + watcher.on('fileModified', rebuildFiles); + rebuildFiles(); + setTimeout(fetchAllEvents, 2000); }; -exports.rebuildFiles = function() { - - console.log('rebuilding...'); +var rebuildFiles = function() { + configuration.logger.inf('Rebuilding slideset database...'); walker = walk.walk(configuration.rootDir, { followLinks: false }); var newSlides = {}; @@ -86,59 +89,69 @@ exports.rebuildFiles = function() { }); } collectDescs(slideset); - fetchEvents(slideset); + } + }); +}; + +var fetchAllEvents = function() { + configuration.logger.inf('Fetching all events...'); + exports.getAllSlides(function(Slides) { + for(var slideset in Slides) { + if(!Slides.hasOwnProperty(slideset)) { continue; } + fetchEvents(Slides[slideset]); } }); }; /* collect events and adds them to the slideset */ var fetchEvents = function(slideset) { + configuration.logger.inf('Fetching ' + slideset); fs.readFile(path.join(__dirname, configuration.rootDir, slideset, configuration.eventFile), function(err, data) { - if(err) { return; } - try { - var events = JSON.parse(data.toString()); - } catch(e) { - return; - } - if(events['type'] == 'text') { - setEvents(slideset, events['events'], function(err) { - if(err) { throw err; } - }); - } else if(events['type'] == 'ical') { - ical.fromURL(events['url'], {}, function(err, bloat) { - if(err) { throw err; } - var bDate = new Date(); - var fDate = new Date(); - bDate.setMinutes(bDate.getMinutes() - events['before']); - fDate.setMinutes(bDate.getMinutes() + events['after']); - var data = ical.events_between(bDate, fDate); - var compEv = []; - for (var k in data) { - if (!data.hasOwnProperty(k)) { continue; } - var ev = data[k]; - if(ev.start && ev.summary) { - var dateStr = ev.start.toLocaleDateString(); - ev.start = ev.start.toLocaleTimeString(); - ev.end = ev.end.toLocaleTimeString(); - delete ev.rrule; - delete ev.uid; - delete ev.type; - for(var i in ev) { - if(!ev.hasOwnProperty(i)) { continue; } - if(typeof ev[i] != 'string') { continue; } - ev[i] = ev[i].replace(/\\,/g, ','); - } - if(typeof compEv[dateStr] == "undefined") { - compEv[dateStr] = []; + if(err) { return; } + try { + var events = JSON.parse(data.toString()); + } catch(e) { + return; + } + if(events['type'] == 'text') { + setEvents(slideset, events['events'], function(err) { + if(err) { throw err; } + }); + } else if(events['type'] == 'ical') { + ical.fromURL(events['url'], {}, function(err, bloat) { + if(err) { throw err; } + var bDate = new Date(); + var fDate = new Date(); + bDate.setMinutes(bDate.getMinutes() - events['before']); + fDate.setMinutes(bDate.getMinutes() + events['after']); + var data = ical.events_between(bDate, fDate); + var compEv = []; + for (var k in data) { + if (!data.hasOwnProperty(k)) { continue; } + var ev = data[k]; + if(ev.start && ev.summary) { + var dateStr = ev.start.toLocaleDateString(); + ev.start = ev.start.toLocaleTimeString(); + ev.end = ev.end.toLocaleTimeString(); + delete ev.rrule; + delete ev.uid; + delete ev.type; + for(var i in ev) { + if(!ev.hasOwnProperty(i)) { continue; } + if(typeof ev[i] != 'string') { continue; } + ev[i] = ev[i].replace(/\\,/g, ','); + } + if(typeof compEv[dateStr] == "undefined") { + compEv[dateStr] = []; + } + compEv[dateStr].push(ev); } - compEv[dateStr].push(ev); } - } - setEvents(slideset, compEv, function(err) { - if(err) { throw err; } + setEvents(slideset, compEv, function(err) { + if(err) { throw err; } + }); }); - }); - } + } }); }; -- cgit v1.2.1