From cc3f33db7a8d3c4ad373e646b199808e01bc5d9b Mon Sep 17 00:00:00 2001 From: sss Date: Tue, 17 Jan 2023 00:38:19 +0300 Subject: added webrdp public code --- www/js/webrdp-Log.js | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 www/js/webrdp-Log.js (limited to 'www/js/webrdp-Log.js') diff --git a/www/js/webrdp-Log.js b/www/js/webrdp-Log.js new file mode 100644 index 0000000..455a736 --- /dev/null +++ b/www/js/webrdp-Log.js @@ -0,0 +1,134 @@ +/** BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST , smake . + * + */ + +webrdp.o2s = function(obj, depth) { + depth = depth || []; + if (depth.contains(obj)) { + return '{SELF}'; + } + switch (typeof(obj)) { + case 'undefined': + return 'undefined'; + case 'string': + return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"'; + case 'array': + var string = []; + depth.push(obj); + for (var i = 0; i < obj.length; ++i) { + string.push(webrdp.o2s(obj[i], depth)); + } + depth.pop(); + return '[' + string + ']'; + case 'object': + case 'hash': + var string = []; + depth.push(obj); + var isE = (obj instanceof UIEvent); + Object.each(obj, function(v, k) { + if (v instanceof HTMLElement) { + string.push(k + '={HTMLElement}'); + } else if (isE && (('layerX' == k) || ('layerY' == k) ('view' == k))) { + string.push(k + '=!0'); + } else { + try { + var vstr = webrdp.o2s(v, depth); + if (vstr) { + string.push(k + '=' + vstr); + } + } catch (error) { + string.push(k + '=??E??'); + } + } + }); + depth.pop(); + return '{' + string + '}'; + case 'number': + case 'boolean': + return '' + obj; + case 'null': + return 'null'; + } + return null; +}; + +webrdp.Log = new Class({ + initialize: function() { + this.ws = null; + this.history = []; + this.loglevel = null; + }, + _p: function(pfx, a) { + var line = ''; + var i; + for (i = 0; i < a.length; ++i) { + switch (typeof(a[i])) { + case 'string': + case 'number': + case 'boolean': + case 'null': + line += a[i] + ' '; + break; + default: + line += webrdp.o2s(a[i]) + ' '; + break; + } + } + if (0 < line.length) { + this.ws.send(pfx + line); + } + }, + drop: function() { + }, + debug: function() { + if (this.loglevel == "debug") { + var entry = { + "date": new Date(), + "logtype": arguments.callee.name, + "direction": arguments[0], + "data": arguments[1] + } + this.history.push(entry); + } + }, + info: function() { + if (this.ws) { + var a = Array.prototype.slice.call(arguments); + a.unshift('I:'); + this._p.apply(this, a); + } + }, + warn: function() { + if (this.ws) { + var a = Array.prototype.slice.call(arguments); + a.unshift('W:'); + this._p.apply(this, a); + } + }, + err: function() { + if (this.ws) { + var a = Array.prototype.slice.call(arguments); + a.unshift('E:'); + this._p.apply(this, a); + } + }, + show: function(count) { + if (!count) { + var count = 30; + } + var first = this.history.length - count > 0 ? this.history.length - count : 0; + for (var i = first; i < this.history.length; i++) { + var h = this.history[i]; + var header = '[' + h.date.toLocaleTimeString('ru') + '.' + h.date.getMilliseconds() + ']'; + var header = '[' + h.date.toLocaleTimeString('ru') + '.' + h.date.getMilliseconds() + ']'; + header += '[' + h.logtype + '] ' + h.direction + ':'; + console.log(header); + console.hex(h.data); + } + }, + setWS: function(_ws) { + this.ws = _ws; + } +}); -- cgit v1.2.3