Mercurial > pub > ImplabJs
changeset 24:f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
author | cin |
---|---|
date | Thu, 07 Dec 2017 19:19:29 +0300 |
parents | 1d72fddc319a |
children | c2a7d21175ce |
files | src/djol/WFSSource.js src/implab/di/ActivationContext.js src/implab/log/listeners/console.js src/implab/log/trace.js src/implab/text/format.js src/implab/text/template-compile.js |
diffstat | 6 files changed, 298 insertions(+), 148 deletions(-) [+] |
line wrap: on
line diff
--- a/src/djol/WFSSource.js Wed Dec 06 14:19:45 2017 +0300 +++ b/src/djol/WFSSource.js Thu Dec 07 19:19:29 2017 +0300 @@ -1,7 +1,16 @@ -define([ "ol", "dojo/request", "dojo/_base/array", "implab/safe"], - -function(ol, request, array, safe) { - return function(wfs, featurePrefix, featureType, featureNS, queryArgs) { +define([ + "ol", + "dojo/request", + "dojo/_base/array", + "implab/safe", + "implab/log/trace!" +], function (ol, + request, + array, + safe, + trace +) { + return function (wfs, featurePrefix, featureType, featureNS, queryArgs) { if (arguments.length == 1) { featurePrefix = wfs.featurePrefix; featureType = wfs.featureType; @@ -16,36 +25,38 @@ safe.argumentNotEmptyString(featureType, "featureType"); var format = new ol.format.WFS({ - featureNS : featureNS, - featureType : featureType + featureNS: featureNS, + featureType: featureType }); var layerName = featurePrefix + ":" + featureType; function loader(extent, resolution, projection) { var query = { - service : 'WFS', - version : '1.1.0', - request : 'GetFeature', - typename : layerName, - srsname : projection.getCode() + service: 'WFS', + version: '1.1.0', + request: 'GetFeature', + typename: layerName, + srsname: projection.getCode() }; safe.mixin(query, queryArgs); - + if (extent && isFinite(extent[0])) query.bbox = extent.join(',') + "," + projection.getCode(); - + return request(wfs, { - query : query, - handleAs : 'xml' - }).then(function(data) { + query: query, + handleAs: 'xml' + }).then(function (data) { // в загрузчике нельзя вызывать метод source.clear() поскольку // это приводит к рекурсии var features = format.readFeatures(data); - var map = {}, del = [], add = []; + var map = {}, + del = [], + add = []; - array.forEach(features, function(x) { + array.forEach(features, function (x) { // HACK исправляем идентификаторы, чтобы они совпадали с // реальными @@ -69,14 +80,14 @@ } }); - source.forEachFeatureInExtent(extent, function(x) { + source.forEachFeatureInExtent(extent, function (x) { if (!(x.getId() in map)) del.push(x); }); source.addFeatures(add); - array.forEach(del, function(x) { + array.forEach(del, function (x) { source.removeFeature(x); }); @@ -90,23 +101,23 @@ var cls = ol.source.ServerVector || ol.source.Vector; var revision = 0; var source = new cls({ - loader : loader, + loader: loader, //revision: revision - wrapX : false - // , - // strategy : options.strategy || ol.loadingstrategy.all, - // projection : options.projection + wrapX: false + // , + // strategy : options.strategy || ol.loadingstrategy.all, + // projection : options.projection }); source.set("revision", revision); - source.reload = function(extent,resolution, projection, q) { + source.reload = function (extent, resolution, projection, q) { if (arguments.length >= 4) queryArgs = q; if (!extent) extent = [-Infinity, -Infinity, Infinity, Infinity]; - return loader(extent,resolution,projection); + return loader(extent, resolution, projection); }; - - console.log(wfs, layerName); + + trace.log("{0} -> {1}", layerName, wfs); return source; }; }); \ No newline at end of file
--- a/src/implab/di/ActivationContext.js Wed Dec 06 14:19:45 2017 +0300 +++ b/src/implab/di/ActivationContext.js Thu Dec 07 19:19:29 2017 +0300 @@ -1,139 +1,138 @@ define([ - "../declare", - "../safe", - "./Descriptor", - "./ValueDescriptor" - ], + "../declare", + "../safe", + "./Descriptor", + "./ValueDescriptor", + "../log/trace!" +], function (declare, safe, Descriptor, Value, trace) { + var Context = declare(null, { - function (declare, safe, Descriptor, Value) { - var Context = declare(null, { + _cache: null, - _cache: null, + _services: null, - _services: null, - - _stack: null, + _stack: null, - _visited: null, + _visited: null, - container: null, + container: null, - _trace: false, + _trace: true, - constructor: function (container, services, cache, visited) { - safe.argumentNotNull(container, "container"); - safe.argumentNotNull(services, "services"); + constructor: function (container, services, cache, visited) { + safe.argumentNotNull(container, "container"); + safe.argumentNotNull(services, "services"); - this._visited = visited || {}; - this._stack = []; - this._cache = cache || {}; - this._services = services; - this.container = container; - }, + this._visited = visited || {}; + this._stack = []; + this._cache = cache || {}; + this._services = services; + this.container = container; + }, - getService: function (name, def) { - var d = this._services[name]; + getService: function (name, def) { + var d = this._services[name]; - if (!d) - if (arguments.length > 1) - return def; - else - throw new Error("Service '" + name + "' not found"); + if (!d) + if (arguments.length > 1) + return def; + else + throw new Error("Service '" + name + "' not found"); - return d.activate(this, name); - }, + return d.activate(this, name); + }, - /** - * registers services local to the the activation context - * - * @name{string} the name of the service - * @service{string} the service descriptor to register - */ - register: function (name, service) { - safe.argumentNotEmptyString(name, "name"); + /** + * registers services local to the the activation context + * + * @name{string} the name of the service + * @service{string} the service descriptor to register + */ + register: function (name, service) { + safe.argumentNotEmptyString(name, "name"); - if (!(service instanceof Descriptor)) - service = new Value(service, true); - this._services[name] = service; - }, + if (!(service instanceof Descriptor)) + service = new Value(service, true); + this._services[name] = service; + }, - clone: function () { - return new Context( - this.container, - Object.create(this._services), - this._cache, - this._visited - ); + clone: function () { + return new Context( + this.container, + Object.create(this._services), + this._cache, + this._visited + ); - }, + }, - has: function (id) { - return id in this._cache; - }, + has: function (id) { + return id in this._cache; + }, - get: function (id) { - return this._cache[id]; - }, + get: function (id) { + return this._cache[id]; + }, - store: function (id, value) { - return (this._cache[id] = value); - }, + store: function (id, value) { + return (this._cache[id] = value); + }, - parse: function (data, name) { - var me = this; - if (safe.isPrimitive(data)) - return data; + parse: function (data, name) { + var me = this; + if (safe.isPrimitive(data)) + return data; - if (data instanceof Descriptor) { - return data.activate(this, name); - } else if (data instanceof Array) { - me.enter(name); - var v = data.map(function (x, i) { - return me.parse(x, "." + i); - }); - me.leave(); - return v; - } else { - me.enter(name); - var result = {}; - for (var p in data) - result[p] = me.parse(data[p], "." + p); - me.leave(); - return result; - } - }, + if (data instanceof Descriptor) { + return data.activate(this, name); + } else if (data instanceof Array) { + me.enter(name); + var v = data.map(function (x, i) { + return me.parse(x, "." + i); + }); + me.leave(); + return v; + } else { + me.enter(name); + var result = {}; + for (var p in data) + result[p] = me.parse(data[p], "." + p); + me.leave(); + return result; + } + }, - visit: function (id) { - var count = this._visited[id] || 0; - this._visited[id] = count + 1; - return count; - }, + visit: function (id) { + var count = this._visited[id] || 0; + this._visited[id] = count + 1; + return count; + }, - getStack: function () { - return this._stack.slice().reverse(); - }, + getStack: function () { + return this._stack.slice().reverse(); + }, - enter: function (name, d, localize) { - if (this._trace) - console.log("enter " + name + " " + (d || "") + - (localize ? " localize" : "")); - this._stack.push({ - name: name, - service: d, - scope: this._services - }); - if (localize) - this._services = Object.create(this._services); - }, + enter: function (name, d, localize) { + if (this._trace) + trace.log("enter " + name + " " + (d || "") + + (localize ? " localize" : "")); + this._stack.push({ + name: name, + service: d, + scope: this._services + }); + if (localize) + this._services = Object.create(this._services); + }, - leave: function () { - var ctx = this._stack.pop(); - this._services = ctx.scope; + leave: function () { + var ctx = this._stack.pop(); + this._services = ctx.scope; - if (this._trace) - console.log("leave " + ctx.name + " " + (ctx.service || "")); - } - }); + if (this._trace) + trace.log("leave " + ctx.name + " " + (ctx.service || "")); + } + }); - return Context; - }); \ No newline at end of file + return Context; +}); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/implab/log/listeners/console.js Thu Dec 07 19:19:29 2017 +0300 @@ -0,0 +1,25 @@ +define([], function () { + if (console && console.log) + return function (ch, name, msg) { + + var args = [ch + ":"]; + + switch (name) { + case "warn": + case "error": + case "log": + break; + default: + args.push(name + ":"); + name = "log"; + } + + + if (msg instanceof Array) + args.push.apply(args, msg); + else + args.push(msg); + + console[name].apply(console, args); + }; +}); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/implab/log/trace.js Thu Dec 07 19:19:29 2017 +0300 @@ -0,0 +1,116 @@ +define(["../text/format"], function (format) { + 'use strict'; + + var listeners = []; + var channels = {}; + + var Trace = function (name) { + this.name = name; + this._subscribers = []; + }; + + Trace.prototype.debug = function () { + if (Trace.level >= 4) + this.notify("debug", format.apply(null, arguments)); + }; + + Trace.prototype.log = function () { + if (Trace.level >= 3) + this.notify("log", format.apply(null, arguments)); + }; + + Trace.prototype.warn = function () { + if (Trace.level >= 2) + this.notify("warn", format.apply(null, arguments)); + + }; + + Trace.prototype.error = function () { + if (Trace.level >= 1) + this.notify("error", format.apply(null, arguments)); + }; + + Trace.prototype.notify = function (name, msg) { + var me = this; + me._subscribers.forEach(function (cb) { + cb(me, name, msg); + }); + }; + + Trace.prototype.subscribe = function (cb) { + this._subscribers.push(cb); + }; + + Trace.prototype.toString = function () { + return this.name; + }; + + Trace.createChannel = function (type, name, cb) { + var chId = name; + if (channels[chId]) + return channels[chId]; + + var channel = new type(chId); + channels[chId] = channel; + + Trace._onNewChannel(chId, channel); + cb(channel); + }; + + Trace._onNewChannel = function (chId, ch) { + listeners.forEach(function (listener) { + listener(chId, ch); + }); + }; + + Trace.on = function (filter, cb) { + if (arguments.length == 1) { + cb = filter; + filter = undefined; + } + var d, test; + if (filter instanceof RegExp) { + test = function (chId) { + return filter.test(chId); + }; + } else if (filter instanceof Function) { + test = filter; + } else if (filter) { + test = function (chId) { + return chId == filter; + }; + } + + if (test) { + d = function(chId, ch) { + if(test(chId)) + ch.subscribe(cb); + }; + } else { + d = function(chId, ch) { + ch.subscribe(cb); + }; + } + listeners.push(d); + + for(var chId in channels) + d(chId,channels[chId]); + }; + + Trace.load = function (id, require, cb) { + if (id) + Trace.createChannel(Trace, id, cb); + else if (require.module && require.module.mid) + Trace.createChannel(Trace, require.module.mid, cb); + else + require(['module'], function (module) { + Trace.createChannel(Trace, module && module.id, cb); + }); + }; + + Trace.dynamic = true; + + Trace.level = 4; + + return Trace; +}); \ No newline at end of file
--- a/src/implab/text/format.js Wed Dec 06 14:19:45 2017 +0300 +++ b/src/implab/text/format.js Thu Dec 07 19:19:29 2017 +0300 @@ -22,7 +22,7 @@ } else { return v; } - }); + },2); } if (safe.isNumber(value)) {
--- a/src/implab/text/template-compile.js Wed Dec 06 14:19:45 2017 +0300 +++ b/src/implab/text/template-compile.js Thu Dec 07 19:19:29 2017 +0300 @@ -1,6 +1,6 @@ define( - ["dojo/request", "./format"], - function (request, format) { + ["dojo/request", "./format", "../log/trace!"], + function (request, format, trace) { // разбивает строку шаблона на токены, возвращает контекст для // дальнейшей обработки в visitTemplate @@ -51,10 +51,9 @@ return compiled(obj || {}, format, data); }; } catch (e) { - if (console && console.error) { - console.error(text); - console.log(data); - } + trace.error([e]); + trace.log([text, data]); + throw e; } } }