Add support for custom loggers

This commit is contained in:
Juanjo Diaz 2019-02-17 20:42:12 +02:00
parent b00a608af7
commit 8ce3545caf
14 changed files with 120 additions and 104 deletions

View File

@ -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) {

View File

@ -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";

View File

@ -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. */

View File

@ -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() {

View File

@ -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 {

View File

@ -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;

View File

@ -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";

View File

@ -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';

View File

@ -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');

View File

@ -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) ||

View File

@ -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) { export class ConsoleLogger {
if (typeof level === 'undefined') { constructor(level) {
level = _log_level; this._logLevels = {
} else { debug: 0,
_log_level = level; info: 1,
} warn: 2,
error: 3
};
Debug = Info = Warn = Error = () => {}; this.logLevel = level;
this._logLevel = this._logLevels[level];
if (typeof window.console !== "undefined") { if (this._logLevel === undefined) {
/* eslint-disable no-console, no-fallthrough */ throw new Error('Invalid logging level \'' + level + '\'');
switch (level) { }
case 'debug': }
Debug = console.debug.bind(window.console);
case 'info': Debug(...args) {
Info = console.info.bind(window.console); if (this._logLevel <= this._logLevels.debug) {
case 'warn': // eslint-disable-next-line no-console
Warn = console.warn.bind(window.console); console.debug.apply(console, args);
case 'error': }
Error = console.error.bind(window.console); }
case 'none':
break; Info(...args) {
default: if (this._logLevel <= this._logLevels.info) {
throw new window.Error("invalid logging type '" + level + "'"); // eslint-disable-next-line no-console
console.info.apply(console, args);
}
}
Warn(...args) {
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);
} }
/* eslint-enable no-console, no-fallthrough */
} }
} }
export function get_logging() { export let Log = window.console ? new ConsoleLogger(LogLevels.warn) : new NoopLogger();
return _log_level;
export function setLogger(logger) {
Log = logger;
} }
export { Debug, Info, Warn, Error };
// Initialize logging level
init_logging();

View File

@ -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

View File

@ -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) {

View File

@ -1,60 +1,60 @@
/* 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 () {
beforeEach(function () { describe('Console Logger', function () {
sinon.spy(console, 'log'); beforeEach(function () {
sinon.spy(console, 'debug'); sinon.spy(console, 'debug');
sinon.spy(console, 'warn'); sinon.spy(console, 'warn');
sinon.spy(console, 'error'); sinon.spy(console, 'error');
sinon.spy(console, 'info'); sinon.spy(console, 'info');
}); });
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');
});
}); });
}); });