function tailFile (file) { var tail = new Tail(file, {start: getFilesizeInBytes(file)}) tail.on('line', function (line) { parseLine(line, file, log) }) tail.on('error', function (error) { console.log('ERROR: ', error) }) console.log('Watching file:' + file) return tail }
InputFile.prototype.tailFile = function (file) { var tail = null var pos = {start: 0} if (this.fileNamesToWatch.indexOf(file) > -1) { // check if we watch this file already return null } else { } try { pos = this.getTailPosition(file) } catch (error) { // file might not exists, we ignore it and start watching pos = {start: 0} } if (this.scanCounter > 0) { // a new file matched the glob pattern // reading from begin of file logger.log('New file detected: ' + file) pos = {start: 0} } try { if (pos.start == -1) { // there was no postion stored, let's start from the beginning // throw new Error('File ' + file + ' does not exist.') pos.start = 0 } tail = new Tail(file, pos) this.filesToWatch.push(tail) this.fileNamesToWatch.push(file) var context = {sourceName: file, startPos: pos} tail.on('line', function (line) { this.stats[file] = (this.stats[file] || 0) + 1 eventEmitter.emit('data.raw', line, context) }.bind(this)) tail.once('error', function (error) { var errMessage = 'ERROR tailing file ' + file + ': ' + error logger.error(errMessage) eventEmitter.emit('error.plugin.input.file', errMessage, {file: file, error: error}) }.bind(this)) logger.log('Watching file:' + file + ' from position: ' + pos.start) return tail } catch (error) { // log('ERROR tailing file ' + file + ': ' + error) return null } }