From be163d3a1250d97929f9bff528090bd15b114fc8 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 10 Apr 2020 19:10:21 +0300 Subject: [PATCH] reworked logging and progress... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/core.js | 125 ++++++++++++++++++++++++++++++ ui (gen4)/features/meta.js | 1 + ui (gen4)/features/ui-progress.js | 31 ++++++++ ui (gen4)/package-lock.json | 6 +- ui (gen4)/package.json | 2 +- ui (gen4)/ui.js | 66 ---------------- 6 files changed, 161 insertions(+), 70 deletions(-) diff --git a/ui (gen4)/features/core.js b/ui (gen4)/features/core.js index 3b0fe18b..badbd9b5 100755 --- a/ui (gen4)/features/core.js +++ b/ui (gen4)/features/core.js @@ -14,6 +14,7 @@ * * * Features: +* - logger * - introspection * - lifecycle * base life-cycle events (start/stop/..) @@ -214,6 +215,130 @@ if(typeof(window) != 'undefined'){ /*********************************************************************/ +// Logger... + +// XXX add log filtering... +var LoggerActions = actions.Actions({ + Logger: object.Constructor('BaseLogger', { + __context: null, + get context(){ + return this.__context || this.root.__context }, + + root: null, + get isRoot(){ + return this === this.root }, + + // NOTE: to disable log retention in .log set this to false... + __log: null, + get log(){ + return this.__log === false ? + false + : this.__log ? + this.__log + : this.isRoot ? + (this.__log = this.__log || []) + : this.root.log }, + + __path: null, + get path(){ + return (this.__path = + this.__path == null ? + [] + : this.__path) }, + set path(value){ + this.__path = value }, + + + // log management... + clear: function(){ + this.log + && this.log.splice(0, this.log.length) + return this }, + print: function(){ + return this.log ? + this.log + .map(function([path, status, rest]){ + return path.join(': ') + (path.length > 0 ? ': ' : '') + + status + + (rest.length > 1 ? + ':\n\t' + : rest.length == 1 ? + ': ' + : '') + + rest.join(': ') }) + .join('\n') + : '' }, + + + // main API... + push: function(...msg){ + return msg.length == 0 ? + this + : Object.assign( + this.constructor(), + { + root: this.root, + path: this.path.concat(msg), + }) }, + pop: function(){ + return (this.root === this || this.path.length == 1) ? + this + : Object.assign( + this.constructor(), + { + root: this.root, + path: this.path.slice(0, -1), + }) }, + + emit: function(status, ...rest){ + this.log !== false + && this.log.push([this.path, status, rest]) + this.context + && this.context.handleLogItem + && this.context.handleLogItem(this.path, status, ...rest) + return this }, + + + __call__: function(_, status, ...rest){ + return this.emit(status, ...rest) }, + __init__: function(context){ + this.__context = context + this.root = this + }, + }), + + __logger: null, + get logger(){ + return (this.__logger = + this.__logger + || this.Logger(this)) }, + + // XXX move this to console-logger... (???) + handleLogItem: ['- System/', + function(path, status, ...rest){ + console.log( + path.join(': ') + (path.length > 0 ? ': ' : '') + + status + + (rest.length > 1 ? + ':\n\t' + : rest.length == 1 ? + ': ' + : ''), ...rest) }], +}) + +var Logger = +module.Logger = ImageGridFeatures.Feature({ + title: '', + + tag: 'logger', + depends: [], + + actions: LoggerActions, +}) + + + +//--------------------------------------------------------------------- // Introspection... // Normalize doc strings... diff --git a/ui (gen4)/features/meta.js b/ui (gen4)/features/meta.js index 9f5bc48a..030ee6c8 100755 --- a/ui (gen4)/features/meta.js +++ b/ui (gen4)/features/meta.js @@ -25,6 +25,7 @@ core.ImageGridFeatures.Feature('imagegrid-commandline', [ core.ImageGridFeatures.Feature('imagegrid-minimal', [ 'lifecycle', + 'logger', // XXX 'alias', 'peer', 'fs', diff --git a/ui (gen4)/features/ui-progress.js b/ui (gen4)/features/ui-progress.js index 67190951..7c10a247 100755 --- a/ui (gen4)/features/ui-progress.js +++ b/ui (gen4)/features/ui-progress.js @@ -142,6 +142,37 @@ var ProgressActions = actions.Actions({ // XXX what should we return??? (state, self, controller?) }], + + // handle logger progress... + // XXX revise... + handleLogItem: ['- System/', + function(path, status, ...rest){ + var msg = this.message + + // report progress... + // XXX HACK -- need meaningful status... + if(status == 'queued' + || status == 'found'){ + this.showProgress(msg || ['Progress', status], '+0', '+'+rest.length) + + } else if(status == 'loaded' || status == 'done' || status == 'written' + || status == 'index'){ + this.showProgress(msg || ['Progress', status], '+'+rest.length) + + } else if(status == 'skipping' || status == 'skipped'){ + // XXX if everything is skipped the indicator does not + // get hidden... + //this.showProgress(msg || ['Progress', status], '+0', '-1') + this.showProgress(msg || ['Progress', status], '+'+rest.length) + + // XXX STUB... + } else if(status == 'error' ){ + this.showProgress(['Error'].concat(msg), '+0', '+'+rest.length) + //console.log(msg ? + // ' '+ msg.join(': ') + ':' + // : '', ...arguments) + } + }], }) var Progress = diff --git a/ui (gen4)/package-lock.json b/ui (gen4)/package-lock.json index 1c627060..3075911b 100755 --- a/ui (gen4)/package-lock.json +++ b/ui (gen4)/package-lock.json @@ -956,9 +956,9 @@ } }, "ig-object": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ig-object/-/ig-object-2.0.0.tgz", - "integrity": "sha512-vYt/WKiBhE5jH7R3RtEXLh8lvY+0aH+WUvW0eB2/KkOKPZgkLqE8XkwIDwdWkvHsSpEqNGybU1kVlSwd+omung==" + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/ig-object/-/ig-object-2.1.0.tgz", + "integrity": "sha512-8xqHyRmxavh3MRK6UlGKuBGST7dfvUR8sw8nfLyI9BLptp0v+cnZ5OUBXY/C4vwvSEAYrcIwToYwAaHsA2b1MQ==" }, "image-size": { "version": "0.5.5", diff --git a/ui (gen4)/package.json b/ui (gen4)/package.json index 98c66865..70819803 100755 --- a/ui (gen4)/package.json +++ b/ui (gen4)/package.json @@ -29,7 +29,7 @@ "guarantee-events": "^1.0.0", "ig-actions": "^3.24.2", "ig-features": "^3.4.0", - "ig-object": "^2.0.0", + "ig-object": "^2.1.0", "moment": "^2.24.0", "requirejs": "^2.3.6", "requirejs-plugins": "^1.0.2", diff --git a/ui (gen4)/ui.js b/ui (gen4)/ui.js index 9b658265..59be46ae 100755 --- a/ui (gen4)/ui.js +++ b/ui (gen4)/ui.js @@ -108,72 +108,6 @@ $(function(){ } - // setup logger... - // XXX STUB... - ig.logger = ig.logger || { - root: true, - message: null, - log: null, - - emit: function(e, v){ - var msg = this.message - var log = this.log = this.log || [] - - var i = v instanceof Array ? v.length : 1 - - // report progress... - // XXX HACK -- need meaningful status... - if(e == 'queued' - || e == 'found'){ - ig.showProgress(msg || ['Progress', e], '+0', '+'+i) - - } else if(e == 'loaded' || e == 'done' || e == 'written' - || e == 'index'){ - ig.showProgress(msg || ['Progress', e], '+'+i) - - } else if(e == 'skipping' || e == 'skipped'){ - // XXX if everything is skipped the indicator does not - // get hidden... - //ig.showProgress(msg || ['Progress', e], '+0', '-1') - ig.showProgress(msg || ['Progress', e], '+'+i) - - // XXX STUB... - } else if(e == 'error' ){ - ig.showProgress(['Error'].concat(msg), '+0', '+'+i) - console.log(msg ? - ' '+ msg.join(': ') + ':' - : '', ...arguments) - - } else { - // console... - console.log(msg ? - ' '+ msg.join(': ') + ':' - : '', ...arguments) - } - - // XXX - //log.push([msg, e, v]) - return this - }, - - push: function(msg){ - if(msg == null){ - return this - } - - var logger = Object.create(this) - logger.root = false - logger.message = logger.message == null ? [msg] : logger.message.concat([msg]) - logger.log = this.log = this.log || [] - - return logger - }, - pop: function(){ - return !this.__proto__.root ? this.__proto__ : this - }, - } - - // setup the viewer... ig .load({ viewer: $('.viewer') })