r21325: delete children in reverse order since the array is manipulated during the...
[Samba.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / source / class / qx / dev / log / Appender.js
blobb1645a87fd078a74bbb8fafced93ac5c099d5315
1 /* ************************************************************************
3    qooxdoo - the new era of web development
5    http://qooxdoo.org
7    Copyright:
8      2006 by STZ-IDA, Germany, http://www.stz-ida.de
10    License:
11      LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
13    Authors:
14      * Til Schneider (til132)
16 ************************************************************************ */
18 /* ************************************************************************
20 #module(core)
21 #module(log)
23 ************************************************************************ */
25 /**
26  * An appender. Does the actual logging.
27  */
28 qx.OO.defineClass("qx.dev.log.Appender", qx.dev.log.LogEventProcessor,
29 function() {
30   qx.dev.log.LogEventProcessor.call(this);
31 });
34 /** Whether the logger name and log level should be included in the formatted log message. */
35 qx.OO.addProperty({ name:"useLongFormat", type:"boolean", defaultValue:true, allowNull:false });
38 // overridden
39 qx.Proto.handleLogEvent = function(evt) {
40   if (this.decideLogEvent(evt) != qx.dev.log.Filter.DENY) {
41     this.appendLogEvent(evt);
42   }
46 /**
47  * Appends a log event to the log.
48  *
49  * @param evt {Map} The event to append.
50  */
51 qx.Proto.appendLogEvent = function(evt) {
52   throw new Error("appendLogEvent is abstract");
56 /**
57  * Formats a log event.
58  *
59  * @param evt {Map} The log event to format.
60  * @return {string} The formatted event.
61  */
62 qx.Proto.formatLogEvent = function(evt) {
63   var Logger = qx.dev.log.Logger;
65   var text = "";
67   // Append the time stamp
68   var time = new String(new Date().getTime() - qx._LOADSTART);
69   while (time.length < 6) {
70     time = "0" + time;
71   }
72   text += time;
74   // Append the level
75   if (this.getUseLongFormat()) {
76     switch (evt.level) {
77       case Logger.LEVEL_DEBUG: text += " DEBUG: "; break;
78       case Logger.LEVEL_INFO:  text += " INFO:  "; break;
79       case Logger.LEVEL_WARN:  text += " WARN:  "; break;
80       case Logger.LEVEL_ERROR: text += " ERROR: "; break;
81       case Logger.LEVEL_FATAL: text += " FATAL: "; break;
82     }
83   } else {
84     text += ": ";
85   }
87   // Append the indent
88   var indent = "";
89   for (var i = 0; i < evt.indent; i++) {
90     indent += "  ";
91   }
92   text += indent;
94   // Append the logger name and instance
95   if (this.getUseLongFormat()) {
96     text += evt.logger.getName();
97     if (evt.instanceId != null) {
98       text += "[" + evt.instanceId + "]";
99     }
100     text += ": ";
101   }
103   // Append the message
104   if (typeof evt.message == "string") {
105     text += evt.message;
106   } else {
107     // The message is an object -> Log a dump of the object
108     var obj = evt.message;
109     if (obj == null) {
110       text += "Object is null";
111     } else {
112       text += "--- Object: " + obj + " ---\n";
113       var attrArr = new Array();
114       try {
115         for (var attr in obj) {
116           attrArr.push(attr);
117         }
118       } catch (exc) {
119         text += indent + "  [not readable: " + exc + "]\n";
120       }
121       attrArr.sort();
122       for (var i = 0; i < attrArr.length; i++) {
123         try {
124           text += indent + "  " + attrArr[i] + "=" + obj[attrArr[i]] + "\n";
125         }
126         catch (exc) {
127           text += indent + "  " + attrArr[i] + "=[not readable: " + exc + "]\n";
128         }
129       }
130       text += indent + "--- End of object ---";
131     }
132   }
134   // Append the throwable
135   if (evt.throwable != null) {
136     var thr = evt.throwable;
138     if (thr.name == null) {
139       text += ": " + thr;
140     } else {
141       text += ": " + thr.name;
142     }
143     if (thr.message != null) {
144       text += " - " + thr.message;
145     }
146     if (thr.number != null) {
147       text += " (#" + thr.number + ")";
148     }
150     if (thr.stack != null) {
151       text += "\n" + this._beautyStackTrace(thr.stack);
152     }
153   }
155   return text;
160  * Beautifies a stack trace.
162  * @param stack {string} the stack trace to beautify.
163  * @return {string} the beautified stack trace.
164  */
165 qx.Proto._beautyStackTrace = function(stack) {
166   // e.g. "()@http://localhost:8080/webcomponent-test-SNAPSHOT/webcomponent/js/com/ptvag/webcomponent/common/log/Logger:253"
167   var lineRe = /@(.+):(\d+)$/gm;
168   var hit;
169   var out = "";
170   var scriptDir = "/script/";
171   while ((hit = lineRe.exec(stack)) != null) {
172     var url = hit[1];
174     var jsPos = url.indexOf(scriptDir);
175     var className = (jsPos == -1) ? url : url.substring(jsPos + scriptDir.length).replace(/\//g, ".");
177     var lineNumber = hit[2];
178     out += "  at " + className + ":" + lineNumber + "\n";
179   }
180   return out;