Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id$
4
*/
5

    
6
/**
7
 * Records a log of events that occur over the course of mapbuilder execution
8
 * @constructor
9
 * @base ModelBase
10
 * @author Mike Adair
11
 * @param modelNode Pointer to the xml node for this model from the config file.
12
 * @param parent    The parent model for the object.
13
 */
14
function Logger(modelNode, parent) {
15
  // Inherit the ModelBase functions and parameters
16
  ModelBase.apply(this, new Array(modelNode, parent));
17
  this.namespace = "xmlns:mb='http://mapbuilder.sourceforge.net/mapbuilder'";
18

    
19
  //create a new document
20
  this.doc = Sarissa.getDomDocument("http://mapbuilder.sourceforge.net/mapbuilder","mb:Logger");//!no prefix on the URL
21
  Sarissa.setXpathNamespaces(this.doc, this.namespace);
22
  this.doc.async = false;
23
  this.doc.validateOnParse=false;  //IE6 SP2 parsing bug
24

    
25
  /**
26
   * appends a new entry in the log file
27
   * @param evenType    the name of the event that occured
28
   * @param listenerId  the ID of the listener object
29
   * @param targetId    the ID of the object passed to the listener function
30
   * @param paramValue  any parameter info supplied to the listener function
31
   */
32
  this.logEvent = function(eventType, listenerId, targetId, paramValue) {
33
    var eventLog = this.doc.createElement("event");
34
    eventLog.setAttribute("time", new Date().getTime());
35
    eventLog.setAttribute("listener", listenerId);
36
    eventLog.setAttribute("target", targetId);
37
    if (paramValue) eventLog.setAttribute("param", paramValue);
38
    eventLog.appendChild(this.doc.createTextNode(eventType));
39
    this.doc.documentElement.appendChild(eventLog);
40
  }
41

    
42
  /**
43
   * clears all entries in the log file
44
   */
45
  this.clearLog = function() {
46
    while (this.doc.documentElement.hasChildNodes() ) {
47
      this.doc.documentElement.removeChild(this.doc.documentElement.firstChild);
48
    }
49
    this.callListeners("refresh");
50
  }
51

    
52
  /**
53
   * save the log by http post to the serializeUrl URL provided
54
   */
55
  this.saveLog = function() {
56
    if (config.serializeUrl) {
57
      var tempDoc = postLoad(config.serializeUrl,logger.doc);
58
      tempDoc.setProperty("SelectionLanguage", "XPath");
59
      Sarissa.setXpathNamespaces(tempDoc, "xmlns:xlink='http://www.w3.org/1999/xlink'");
60
      var onlineResource = tempDoc.selectSingleNode("//OnlineResource");
61
      var fileUrl = onlineResource.attributes.getNamedItem("xlink:href").nodeValue;
62
      alert("event log saved as:" + fileUrl);
63
    } else {
64
      alert("unable to save event log; provide a serializeUrl property in config");
65
    }
66
  }
67

    
68
  /**
69
   * save the log by http post to the serializeUrl URL provided
70
   */
71
  this.refreshLog = function(objRef) {
72
    objRef.callListeners("refresh");
73
  }
74

    
75
  if (parent) parent.addListener("refresh",this.refreshLog, this);
76
  window.onunload = this.saveLog;   //automatically save the log when the page unloads
77
  window.logger = this;             //global reference to the logger model
78
}
(8-8/18)