Add support for custom loggers
This commit is contained in:
parent
b00a608af7
commit
8ce3545caf
|
|
@ -6,7 +6,7 @@
|
||||||
* See README.md for usage and integration instructions.
|
* See README.md for usage and integration instructions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from '../core/util/logging.js';
|
import { ConsoleLogger, Log } from '../core/util/logging.js';
|
||||||
import _, { l10n } from './localization.js';
|
import _, { l10n } from './localization.js';
|
||||||
import { isTouchDevice, isSafari, isIOS, isAndroid, dragThreshold }
|
import { isTouchDevice, isSafari, isIOS, isAndroid, dragThreshold }
|
||||||
from '../core/util/browser.js';
|
from '../core/util/browser.js';
|
||||||
|
|
@ -1602,7 +1602,10 @@ const UI = {
|
||||||
},
|
},
|
||||||
|
|
||||||
updateLogging() {
|
updateLogging() {
|
||||||
WebUtil.init_logging(UI.getSetting('logging'));
|
const param = UI.getSetting('logging')
|
||||||
|
|| document.location.href.match(/logging=([A-Za-z0-9._-]*)/)
|
||||||
|
|| undefined;
|
||||||
|
RFB.setLogger(new ConsoleLogger(param));
|
||||||
},
|
},
|
||||||
|
|
||||||
updateDesktopName(e) {
|
updateDesktopName(e) {
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,6 @@
|
||||||
* See README.md for usage and integration instructions.
|
* See README.md for usage and integration instructions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { init_logging as main_init_logging } from '../core/util/logging.js';
|
|
||||||
|
|
||||||
// init log level reading the logging HTTP param
|
|
||||||
export function init_logging(level) {
|
|
||||||
"use strict";
|
|
||||||
if (typeof level !== "undefined") {
|
|
||||||
main_init_logging(level);
|
|
||||||
} else {
|
|
||||||
const param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
|
|
||||||
main_init_logging(param || undefined);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read a query string variable
|
// Read a query string variable
|
||||||
export function getQueryVar(name, defVal) {
|
export function getQueryVar(name, defVal) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
// From: http://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js
|
// From: http://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js
|
||||||
|
|
||||||
import * as Log from './util/logging.js';
|
import { Log } from './util/logging.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
/* Convert data (an array of integers) to a Base64 string. */
|
/* Convert data (an array of integers) to a Base64 string. */
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from '../util/logging.js';
|
import { Log } from '../util/logging.js';
|
||||||
|
|
||||||
export default class HextileDecoder {
|
export default class HextileDecoder {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from '../util/logging.js';
|
import { Log } from '../util/logging.js';
|
||||||
import Inflator from "../inflator.js";
|
import Inflator from "../inflator.js";
|
||||||
|
|
||||||
export default class TightDecoder {
|
export default class TightDecoder {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
* See README.md for usage and integration instructions.
|
* See README.md for usage and integration instructions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from './util/logging.js';
|
import { Log } from './util/logging.js';
|
||||||
import Base64 from "./base64.js";
|
import Base64 from "./base64.js";
|
||||||
|
|
||||||
let SUPPORTS_IMAGEDATA_CONSTRUCTOR = false;
|
let SUPPORTS_IMAGEDATA_CONSTRUCTOR = false;
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from '../util/logging.js';
|
import { Log } from '../util/logging.js';
|
||||||
import { stopEvent } from '../util/events.js';
|
import { stopEvent } from '../util/events.js';
|
||||||
import * as KeyboardUtil from "./util.js";
|
import * as KeyboardUtil from "./util.js";
|
||||||
import KeyTable from "./keysym.js";
|
import KeyTable from "./keysym.js";
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from '../util/logging.js';
|
import { Log } from '../util/logging.js';
|
||||||
import { isTouchDevice } from '../util/browser.js';
|
import { isTouchDevice } from '../util/browser.js';
|
||||||
import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';
|
import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from './util/logging.js';
|
import { Log, setLogger } from './util/logging.js';
|
||||||
import { decodeUTF8 } from './util/strings.js';
|
import { decodeUTF8 } from './util/strings.js';
|
||||||
import { dragThreshold } from './util/browser.js';
|
import { dragThreshold } from './util/browser.js';
|
||||||
import EventTargetMixin from './util/eventtarget.js';
|
import EventTargetMixin from './util/eventtarget.js';
|
||||||
|
|
@ -310,6 +310,10 @@ export default class RFB extends EventTargetMixin {
|
||||||
|
|
||||||
// ===== PUBLIC METHODS =====
|
// ===== PUBLIC METHODS =====
|
||||||
|
|
||||||
|
static setLogger(logger) {
|
||||||
|
setLogger(logger);
|
||||||
|
}
|
||||||
|
|
||||||
disconnect() {
|
disconnect() {
|
||||||
this._updateConnectionState('disconnecting');
|
this._updateConnectionState('disconnecting');
|
||||||
this._sock.off('error');
|
this._sock.off('error');
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
* See README.md for usage and integration instructions.
|
* See README.md for usage and integration instructions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from './logging.js';
|
import { Log } from './logging.js';
|
||||||
|
|
||||||
// Touch detection
|
// Touch detection
|
||||||
export let isTouchDevice = ('ontouchstart' in document.documentElement) ||
|
export let isTouchDevice = ('ontouchstart' in document.documentElement) ||
|
||||||
|
|
|
||||||
|
|
@ -10,47 +10,69 @@
|
||||||
* Logging/debug routines
|
* Logging/debug routines
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let _log_level = 'warn';
|
export const LogLevels = {
|
||||||
|
none: 'none',
|
||||||
|
debug: 'debug',
|
||||||
|
info: 'info',
|
||||||
|
warn: 'warn',
|
||||||
|
error: 'error'
|
||||||
|
};
|
||||||
|
|
||||||
let Debug = () => {};
|
export class NoopLogger {
|
||||||
let Info = () => {};
|
Debug() {}
|
||||||
let Warn = () => {};
|
Info() {}
|
||||||
let Error = () => {};
|
Warn() {}
|
||||||
|
Error() {}
|
||||||
export function init_logging(level) {
|
|
||||||
if (typeof level === 'undefined') {
|
|
||||||
level = _log_level;
|
|
||||||
} else {
|
|
||||||
_log_level = level;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug = Info = Warn = Error = () => {};
|
export class ConsoleLogger {
|
||||||
|
constructor(level) {
|
||||||
|
this._logLevels = {
|
||||||
|
debug: 0,
|
||||||
|
info: 1,
|
||||||
|
warn: 2,
|
||||||
|
error: 3
|
||||||
|
};
|
||||||
|
|
||||||
if (typeof window.console !== "undefined") {
|
this.logLevel = level;
|
||||||
/* eslint-disable no-console, no-fallthrough */
|
this._logLevel = this._logLevels[level];
|
||||||
switch (level) {
|
|
||||||
case 'debug':
|
if (this._logLevel === undefined) {
|
||||||
Debug = console.debug.bind(window.console);
|
throw new Error('Invalid logging level \'' + level + '\'');
|
||||||
case 'info':
|
|
||||||
Info = console.info.bind(window.console);
|
|
||||||
case 'warn':
|
|
||||||
Warn = console.warn.bind(window.console);
|
|
||||||
case 'error':
|
|
||||||
Error = console.error.bind(window.console);
|
|
||||||
case 'none':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new window.Error("invalid logging type '" + level + "'");
|
|
||||||
}
|
|
||||||
/* eslint-enable no-console, no-fallthrough */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get_logging() {
|
Debug(...args) {
|
||||||
return _log_level;
|
if (this._logLevel <= this._logLevels.debug) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.debug.apply(console, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Debug, Info, Warn, Error };
|
Info(...args) {
|
||||||
|
if (this._logLevel <= this._logLevels.info) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.info.apply(console, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize logging level
|
Warn(...args) {
|
||||||
init_logging();
|
if (this._logLevel <= this._logLevels.warn) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.warn.apply(console, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Error(...args) {
|
||||||
|
if (this._logLevel <= this._logLevels.error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error.apply(console, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export let Log = window.console ? new ConsoleLogger(LogLevels.warn) : new NoopLogger();
|
||||||
|
|
||||||
|
export function setLogger(logger) {
|
||||||
|
Log = logger;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
* read binary data off of the receive queue.
|
* read binary data off of the receive queue.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Log from './util/logging.js';
|
import { Log } from './util/logging.js';
|
||||||
|
|
||||||
// this has performance issues in some versions Chromium, and
|
// this has performance issues in some versions Chromium, and
|
||||||
// doesn't gain a tremendous amount of performance increase in Firefox
|
// doesn't gain a tremendous amount of performance increase in Firefox
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import RFB from '../core/rfb.js';
|
import RFB from '../core/rfb.js';
|
||||||
import * as Log from '../core/util/logging.js';
|
import { Log } from '../core/util/logging.js';
|
||||||
|
|
||||||
// Immediate polyfill
|
// Immediate polyfill
|
||||||
if (window.setImmediate === undefined) {
|
if (window.setImmediate === undefined) {
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
|
|
||||||
import * as Log from '../core/util/logging.js';
|
import { Log, ConsoleLogger, LogLevels, setLogger } from '../core/util/logging.js';
|
||||||
|
|
||||||
describe('Utils', function () {
|
describe('Utils', function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe('logging functions', function () {
|
describe('logging', function () {
|
||||||
|
describe('Console Logger', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
sinon.spy(console, 'log');
|
|
||||||
sinon.spy(console, 'debug');
|
sinon.spy(console, 'debug');
|
||||||
sinon.spy(console, 'warn');
|
sinon.spy(console, 'warn');
|
||||||
sinon.spy(console, 'error');
|
sinon.spy(console, 'error');
|
||||||
|
|
@ -16,47 +16,47 @@ describe('Utils', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
console.log.restore();
|
|
||||||
console.debug.restore();
|
console.debug.restore();
|
||||||
console.warn.restore();
|
console.warn.restore();
|
||||||
console.error.restore();
|
console.error.restore();
|
||||||
console.info.restore();
|
console.info.restore();
|
||||||
Log.init_logging();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use noop for levels lower than the min level', function () {
|
it('should use noop for levels lower than the min level', function () {
|
||||||
Log.init_logging('warn');
|
setLogger(new ConsoleLogger(LogLevels.warn));
|
||||||
Log.Debug('hi');
|
Log.Debug('hi');
|
||||||
Log.Info('hello');
|
Log.Info('hello');
|
||||||
expect(console.log).to.not.have.been.called;
|
expect(console.debug).to.not.have.been.called;
|
||||||
|
expect(console.info).to.not.have.been.called;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use console.debug for Debug', function () {
|
it('should use console.debug for Debug', function () {
|
||||||
Log.init_logging('debug');
|
setLogger(new ConsoleLogger(LogLevels.debug));
|
||||||
Log.Debug('dbg');
|
Log.Debug('dbg');
|
||||||
expect(console.debug).to.have.been.calledWith('dbg');
|
expect(console.debug).to.have.been.calledWith('dbg');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use console.info for Info', function () {
|
it('should use console.info for Info', function () {
|
||||||
Log.init_logging('debug');
|
setLogger(new ConsoleLogger(LogLevels.info));
|
||||||
Log.Info('inf');
|
Log.Info('inf');
|
||||||
expect(console.info).to.have.been.calledWith('inf');
|
expect(console.info).to.have.been.calledWith('inf');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use console.warn for Warn', function () {
|
it('should use console.warn for Warn', function () {
|
||||||
Log.init_logging('warn');
|
setLogger(new ConsoleLogger(LogLevels.warn));
|
||||||
Log.Warn('wrn');
|
Log.Warn('wrn');
|
||||||
expect(console.warn).to.have.been.called;
|
expect(console.warn).to.have.been.called;
|
||||||
expect(console.warn).to.have.been.calledWith('wrn');
|
expect(console.warn).to.have.been.calledWith('wrn');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use console.error for Error', function () {
|
it('should use console.error for Error', function () {
|
||||||
Log.init_logging('error');
|
setLogger(new ConsoleLogger(LogLevels.error));
|
||||||
Log.Error('err');
|
Log.Error('err');
|
||||||
expect(console.error).to.have.been.called;
|
expect(console.error).to.have.been.called;
|
||||||
expect(console.error).to.have.been.calledWith('err');
|
expect(console.error).to.have.been.calledWith('err');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// TODO(directxman12): test the conf_default and conf_defaults methods
|
// TODO(directxman12): test the conf_default and conf_defaults methods
|
||||||
// TODO(directxman12): test decodeUTF8
|
// TODO(directxman12): test decodeUTF8
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue