Skip to content
Browse files

Using blammo for logging

  • Loading branch information...
1 parent 0069a64 commit 7a1ac8c02652b38d949fe63cca45fd019dc6f623 @StevenLooman committed Sep 22, 2012
Showing with 35 additions and 8 deletions.
  1. +10 −0 blammo.xml
  2. +20 −5 lib/saxpath.js
  3. +5 −3 run.js
View
10 blammo.xml
@@ -0,0 +1,10 @@
+<configuration>
+ <appender name="console" type="console_appender">
+ <encoder type="dummy_encoder"/>
+ <layout type="pattern_layout">%timestamp|%pid|%logger|%level|%message</layout>
+ </appender>
+
+ <root level="off">
+ <appender-ref ref="console" />
+ </root>
+</configuration>
View
25 lib/saxpath.js
@@ -1,5 +1,6 @@
-var util = require('util');
+var blammo = require('blammo');
var events = require('events');
+var util = require('util');
var XPathParser = require('./xpath_parser');
var XmlRecorder = require('./xml_recorder');
@@ -25,11 +26,16 @@ var SelfOrDescendantState = require('./self_or_descendant_state');
* When (s2) is unmatched, the recorder is stopped
*/
function SaXPath(saxParser, xpath, recorder) {
+ this.logger = blammo.LoggerFactory.getLogger('saxpath.SaxPath');
+ blammo.Logger.traceEntry(this.logger, this, '_handleOpenTag');
+ blammo.Logger.traceEntry(this.logger, this, '_handleCloseTag');
+
this.saxParser = saxParser;
this.currentDepth = 0;
this.xpathExpr = XPathParser.parse(xpath);
this.activeStates = [ this._parseXPathExpr(this.xpathExpr) ];
+ this.activeStates[0].matches('', 0);
this.recorder = recorder || new XmlRecorder();
// order of binding is important here!
@@ -40,8 +46,6 @@ function SaXPath(saxParser, xpath, recorder) {
this.saxParser.on('closetag', this.onCloseTag.bind(this));
this.saxParser.on('text', this.recorder.onText.bind(this.recorder));
this.saxParser.on('end', this.onEnd.bind(this));
-
- this.activeStates[0].matches('', 0);
}
@@ -89,14 +93,18 @@ SaXPath.prototype._parseXPathExpr = function(expr) {
// bookkeeping
previousState = state;
+
+ this.logger.debug('built state: ', state.toString());
}
return start;
};
SaXPath.prototype._fork = function(state, depth) {
- // stringify states-expression
+ this.logger.debug('creating fork at depth ' + depth + ': ' + state.toString());
+
+ // stringify states-expression, from current state
var str = '';
var cur = state;
while (cur) {
@@ -166,6 +174,8 @@ SaXPath.prototype._handleOpenTag = function(state, node) {
if (state.next) {
// still matching states
if (state.next.matches(node, this.currentDepth)) {
+ this.logger.debug('current state matches: ' + state.next.toString());
+
// do fork here?
if (state.next.doFork) {
forkedState = this._fork(state.next, this.currentDepth);
@@ -176,6 +186,7 @@ SaXPath.prototype._handleOpenTag = function(state, node) {
// start recording if the top of the stack has been reached
if (!state.next) {
+ this.logger.debug('start recording');
this.recorder.start(state);
}
@@ -192,15 +203,19 @@ SaXPath.prototype._handleCloseTag = function(state, tag) {
// current node no longer matches the top of the stack?
if (state.unmatches(tag, this.currentDepth)) {
+ this.logger.debug('current state unmatches: ' + state.toString());
+
// if we are at the top of the state-stack, stop the recorder
if (stopRecorder) {
- var xml = this.recorder.stop(state);
+ this.logger.debug('stop recording');
+ var xml = this.recorder.stop(state);
this.emit('match', xml);
}
// forked here? then remove this state-stack
if (state.isFork) {
+ this.logger.debug('destroy fork');
newState = null;
} else {
// hop to previous state
View
8 run.js
@@ -1,8 +1,9 @@
#!/usr/bin/env node
-var fs = require('fs');
-var sax = require('sax');
-var xps = require('./lib');
+var blammo = require('blammo');
+var fs = require('fs');
+var sax = require('sax');
+var xps = require('./lib');
//var filename = 'test/bookstore.xml';
var filename = 'test/inception.xml';
@@ -14,6 +15,7 @@ function onMatch(xml) {
function main() {
var saxParser = sax.createStream(true);
+
// var streamer = new xps.SaXPath(saxParser, '/bookstore/book');
// var streamer = new xps.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]');
// var streamer = new xps.SaXPath(saxParser, '//book');

0 comments on commit 7a1ac8c

Please sign in to comment.
Something went wrong with that request. Please try again.