From: Nagao Takeyuki Date: Sun, 13 Sep 2009 06:28:02 +0000 (+0900) Subject: Added: logging package X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=39d5343ec7ef38a0c2cb7b947752d3e4837a9677;p=dvibrowser%2Fjdisp.git Added: logging package Signed-off-by: Nagao Takeyuki --- diff --git a/src/js/bundle/jdisp.jsh b/src/js/bundle/jdisp.jsh index 780f594..ee7d0e2 100644 --- a/src/js/bundle/jdisp.jsh +++ b/src/js/bundle/jdisp.jsh @@ -7,5 +7,6 @@ #include #include #include +#include #endif /* _JDISP_H */ diff --git a/src/js/include/jdisp/logging/BasicLogger.jsh b/src/js/include/jdisp/logging/BasicLogger.jsh new file mode 100644 index 0000000..f9133ab --- /dev/null +++ b/src/js/include/jdisp/logging/BasicLogger.jsh @@ -0,0 +1,78 @@ +#ifndef _JDISP_LOGGING_BASICLOGGER_JS +#define _JDISP_LOGGING_BASICLOGGER_JS + +OOP_CLASS(BasicLogger) = function(id, max, f) +{ + this.lines = new Array(); + if (id) { + this.setTargetId(id); + } + if (max !== null) { + this.maxLines = Number(max); + } else { + this.maxLines = 25; + } + if (f !== null) { + this.setEnabled(f); + } +}; + +OOP_FIELD(BasicLogger, maxLines) = 1024; +OOP_FIELD(BasicLogger, maxLineLength) = 4096; +OOP_FIELD(BasicLogger, digits) = 6; +OOP_FIELD(BasicLogger, lines) = null; +OOP_FIELD(BasicLogger, count) = 0; +OOP_FIELD(BasicLogger, targetId) = null; +OOP_FIELD(BasicLogger, targetNode) = null; +OOP_FIELD(BasicLogger, nodeFetched) = false; +OOP_FIELD(BasicLogger, enabled) = true; +OOP_METHOD(BasicLogger, setEnabled) = function (f) +{ + f = Boolean(f); + this.enabled = f; +}; + +OOP_METHOD(BasicLogger, setMaxLines) = function (n) +{ + if (IS_NUMBER(n) && 0 <= n) { + this.maxLines = n; + } +}; + +OOP_METHOD(BasicLogger, setTargetId) = function (id) +{ + this.targetId = id; + this.nodeFetched = false; + this.update(); +}; + +OOP_METHOD(BasicLogger, append) = function (msg0) +{ + if (!this.enabled) return; + var msg = String(msg0); + msg = msg.slice(0, this.maxLineLength); + this.count++; + var s = "[" + PROJECT_PREFIX.global.zeroPrefix(this.count, this.digits) + "] " + + PROJECT_PREFIX.global.escapeHtml(msg) ; + var lines = this.lines; + var max = this.maxLines; + lines.push(s); + while (lines.length > max) { + lines.shift(); + } + this.update(); +}; + +OOP_METHOD(BasicLogger, update) = function () +{ + if (!this.enabled) return; + if (!this.nodeFetched) { + this.nodeFetched = true; + var n = PROJECT_PREFIX.domUtils.getNode(this.targetId); + this.targetNode = n; + } + var html = this.lines.join("
\r\n"); + PROJECT_PREFIX.domUtils.setHtml(this.targetNode, html); +}; + +#endif /* _JDISP_LOGGING_BASICLOGGER_JS */ diff --git a/src/js/include/jdisp/logging/Counter.jsh b/src/js/include/jdisp/logging/Counter.jsh new file mode 100644 index 0000000..596ca70 --- /dev/null +++ b/src/js/include/jdisp/logging/Counter.jsh @@ -0,0 +1,86 @@ +#ifndef _JDISP_LOGGING_COUNTER_JS +#define _JDISP_LOGGING_COUNTER_JS + +OOP_CLASS(Counter) = function() +{ + this.data = new Object(); +}; + +OOP_FIELD(Counter, data) = new Object(); + +OOP_METHOD(Counter, begin) = function (key) +{ + var rec = this.take(key); + rec.begin++; +}; + +OOP_METHOD(Counter, end) = function (key) +{ + var rec = this.take(key); + rec.end++; + this.tick(key); +}; + +OOP_METHOD(Counter, tick) = function (key) +{ + var rec = this.take(key); + rec.count++; +}; + +OOP_METHOD(Counter, add) = function (key, value) +{ + var rec = this.take(key); + rec.sum += value; + if (!rec.hasValue) { + rec.max = value; + rec.min = value; + } else { + rec.max = Math.max(rec.max, value); + rec.min = Math.min(rec.min, value); + } + rec.hasValue = true; + this.tick(key); +}; + + + +OOP_METHOD(Counter, take) = function (key) +{ + var rec = null; + if (key in this.data) { + rec = this.data[key]; + } else { + rec = new Object(); + rec.begin = 0; + rec.end = 0; + rec.count = 0; + rec.sum = 0; + rec.hasValue = false; + rec.max = 0; + rec.min = 0; + this.data[key] = rec; + } + + return rec; +}; + +OOP_METHOD(Counter, toString) = function () +{ + var lp = new PROJECT_PREFIX.text.ListPrinter('', ',', ''); + for (var key in this.data) { + var rec = this.data[key]; + var statistics = ''; + if (rec.count > 0 && rec.hasValue) { + var avg = rec.sum * 1.0 / rec.count; + statistics = ',min=' + rec.min + + ',avg=' + avg.toFixed(3) + + ',max=' + rec.max ; + } + lp.append ( key + '=(' + + rec.count + ',' + + rec.begin + ',' + rec.end + statistics + ')'); + } + return lp.toString(); +}; + +#endif /* _JDISP_LOGGING_COUNTER_JS */ diff --git a/src/js/include/jdisp/logging/package.jsh b/src/js/include/jdisp/logging/package.jsh new file mode 100644 index 0000000..7550d65 --- /dev/null +++ b/src/js/include/jdisp/logging/package.jsh @@ -0,0 +1,16 @@ +#ifndef _JDISP_LOGGING_PACKAGE_JSH +#define _JDISP_LOGGING_PACKAGE_JSH + +#define OOP_NAMESPACE PROJECT_PREFIX.logging +OOP_DECLARE_NAMESPACE2(PROJECT_PREFIX, logging) + +#include "BasicLogger.jsh" +#include "Counter.jsh" + +PROJECT_PREFIX.logging.stdout = new OOP_CLASS(BasicLogger)(); +PROJECT_PREFIX.logging.counter = new OOP_CLASS(Counter)(); + +#undef OOP_NAMESPACE + +#endif /* _JDISP_LOGGING_PACKAGE_JSH */ +