diff --git a/app/ui.js b/app/ui.js index aaabad72..0d291330 100644 --- a/app/ui.js +++ b/app/ui.js @@ -6,7 +6,7 @@ * 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 { isTouchDevice, isSafari, isIOS, isAndroid, dragThreshold } from '../core/util/browser.js'; @@ -1602,7 +1602,10 @@ const UI = { }, 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) { diff --git a/app/webutil.js b/app/webutil.js index 922ddc10..7d7c4886 100644 --- a/app/webutil.js +++ b/app/webutil.js @@ -6,19 +6,6 @@ * 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 export function getQueryVar(name, defVal) { "use strict"; diff --git a/core/base64.js b/core/base64.js index 42ba53b6..741e3607 100644 --- a/core/base64.js +++ b/core/base64.js @@ -4,7 +4,7 @@ // 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 { /* Convert data (an array of integers) to a Base64 string. */ diff --git a/core/decoders/hextile.js b/core/decoders/hextile.js index aa76d2f3..3af6ce2b 100644 --- a/core/decoders/hextile.js +++ b/core/decoders/hextile.js @@ -9,7 +9,7 @@ * */ -import * as Log from '../util/logging.js'; +import { Log } from '../util/logging.js'; export default class HextileDecoder { constructor() { diff --git a/core/decoders/tight.js b/core/decoders/tight.js index bcda04ce..150e89bb 100644 --- a/core/decoders/tight.js +++ b/core/decoders/tight.js @@ -10,7 +10,7 @@ * */ -import * as Log from '../util/logging.js'; +import { Log } from '../util/logging.js'; import Inflator from "../inflator.js"; export default class TightDecoder { diff --git a/core/display.js b/core/display.js index 59ddb340..ea97543c 100644 --- a/core/display.js +++ b/core/display.js @@ -6,7 +6,7 @@ * 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"; let SUPPORTS_IMAGEDATA_CONSTRUCTOR = false; diff --git a/core/input/keyboard.js b/core/input/keyboard.js index 9dbc8d6e..f3934b71 100644 --- a/core/input/keyboard.js +++ b/core/input/keyboard.js @@ -4,7 +4,7 @@ * 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 * as KeyboardUtil from "./util.js"; import KeyTable from "./keysym.js"; diff --git a/core/input/mouse.js b/core/input/mouse.js index c78f2ab8..1c9d5d0d 100644 --- a/core/input/mouse.js +++ b/core/input/mouse.js @@ -4,7 +4,7 @@ * 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 { setCapture, stopEvent, getPointerEvent } from '../util/events.js'; diff --git a/core/rfb.js b/core/rfb.js index 3665583d..403428c0 100644 --- a/core/rfb.js +++ b/core/rfb.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 { dragThreshold } from './util/browser.js'; import EventTargetMixin from './util/eventtarget.js'; @@ -310,6 +310,10 @@ export default class RFB extends EventTargetMixin { // ===== PUBLIC METHODS ===== + static setLogger(logger) { + setLogger(logger); + } + disconnect() { this._updateConnectionState('disconnecting'); this._sock.off('error'); diff --git a/core/util/browser.js b/core/util/browser.js index 026a31ab..ea4b5183 100644 --- a/core/util/browser.js +++ b/core/util/browser.js @@ -6,7 +6,7 @@ * See README.md for usage and integration instructions. */ -import * as Log from './logging.js'; +import { Log } from './logging.js'; // Touch detection export let isTouchDevice = ('ontouchstart' in document.documentElement) || diff --git a/core/util/logging.js b/core/util/logging.js index 4c8943d0..da794ac5 100644 --- a/core/util/logging.js +++ b/core/util/logging.js @@ -10,47 +10,69 @@ * Logging/debug routines */ -let _log_level = 'warn'; +export const LogLevels = { + none: 'none', + debug: 'debug', + info: 'info', + warn: 'warn', + error: 'error' +}; -let Debug = () => {}; -let Info = () => {}; -let Warn = () => {}; -let Error = () => {}; +export class NoopLogger { + Debug() {} + Info() {} + Warn() {} + Error() {} +} -export function init_logging(level) { - if (typeof level === 'undefined') { - level = _log_level; - } else { - _log_level = level; - } +export class ConsoleLogger { + constructor(level) { + this._logLevels = { + debug: 0, + info: 1, + warn: 2, + error: 3 + }; - Debug = Info = Warn = Error = () => {}; + this.logLevel = level; + this._logLevel = this._logLevels[level]; - if (typeof window.console !== "undefined") { - /* eslint-disable no-console, no-fallthrough */ - switch (level) { - case 'debug': - Debug = console.debug.bind(window.console); - 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 + "'"); + if (this._logLevel === undefined) { + throw new Error('Invalid logging level \'' + level + '\''); + } + } + + Debug(...args) { + if (this._logLevel <= this._logLevels.debug) { + // eslint-disable-next-line no-console + console.debug.apply(console, args); + } + } + + Info(...args) { + if (this._logLevel <= this._logLevels.info) { + // 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() { - return _log_level; +export let Log = window.console ? new ConsoleLogger(LogLevels.warn) : new NoopLogger(); + +export function setLogger(logger) { + Log = logger; } - -export { Debug, Info, Warn, Error }; - -// Initialize logging level -init_logging(); diff --git a/core/websock.js b/core/websock.js index 51b9a66f..38436838 100644 --- a/core/websock.js +++ b/core/websock.js @@ -12,7 +12,7 @@ * 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 // doesn't gain a tremendous amount of performance increase in Firefox diff --git a/tests/playback.js b/tests/playback.js index 5bd8103a..c2f437c8 100644 --- a/tests/playback.js +++ b/tests/playback.js @@ -5,7 +5,7 @@ */ import RFB from '../core/rfb.js'; -import * as Log from '../core/util/logging.js'; +import { Log } from '../core/util/logging.js'; // Immediate polyfill if (window.setImmediate === undefined) { diff --git a/tests/test.util.js b/tests/test.util.js index 201acc8b..0f5242a3 100644 --- a/tests/test.util.js +++ b/tests/test.util.js @@ -1,60 +1,60 @@ /* eslint-disable no-console */ 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 () { "use strict"; - describe('logging functions', function () { - beforeEach(function () { - sinon.spy(console, 'log'); - sinon.spy(console, 'debug'); - sinon.spy(console, 'warn'); - sinon.spy(console, 'error'); - sinon.spy(console, 'info'); - }); + describe('logging', function () { + describe('Console Logger', function () { + beforeEach(function () { + sinon.spy(console, 'debug'); + sinon.spy(console, 'warn'); + sinon.spy(console, 'error'); + sinon.spy(console, 'info'); + }); - afterEach(function () { - console.log.restore(); - console.debug.restore(); - console.warn.restore(); - console.error.restore(); - console.info.restore(); - Log.init_logging(); - }); + afterEach(function () { + console.debug.restore(); + console.warn.restore(); + console.error.restore(); + console.info.restore(); + }); - it('should use noop for levels lower than the min level', function () { - Log.init_logging('warn'); - Log.Debug('hi'); - Log.Info('hello'); - expect(console.log).to.not.have.been.called; - }); + it('should use noop for levels lower than the min level', function () { + setLogger(new ConsoleLogger(LogLevels.warn)); + Log.Debug('hi'); + Log.Info('hello'); + expect(console.debug).to.not.have.been.called; + expect(console.info).to.not.have.been.called; + }); - it('should use console.debug for Debug', function () { - Log.init_logging('debug'); - Log.Debug('dbg'); - expect(console.debug).to.have.been.calledWith('dbg'); - }); + it('should use console.debug for Debug', function () { + setLogger(new ConsoleLogger(LogLevels.debug)); + Log.Debug('dbg'); + expect(console.debug).to.have.been.calledWith('dbg'); + }); - it('should use console.info for Info', function () { - Log.init_logging('debug'); - Log.Info('inf'); - expect(console.info).to.have.been.calledWith('inf'); - }); + it('should use console.info for Info', function () { + setLogger(new ConsoleLogger(LogLevels.info)); + Log.Info('inf'); + expect(console.info).to.have.been.calledWith('inf'); + }); - it('should use console.warn for Warn', function () { - Log.init_logging('warn'); - Log.Warn('wrn'); - expect(console.warn).to.have.been.called; - expect(console.warn).to.have.been.calledWith('wrn'); - }); + it('should use console.warn for Warn', function () { + setLogger(new ConsoleLogger(LogLevels.warn)); + Log.Warn('wrn'); + expect(console.warn).to.have.been.called; + expect(console.warn).to.have.been.calledWith('wrn'); + }); - it('should use console.error for Error', function () { - Log.init_logging('error'); - Log.Error('err'); - expect(console.error).to.have.been.called; - expect(console.error).to.have.been.calledWith('err'); + it('should use console.error for Error', function () { + setLogger(new ConsoleLogger(LogLevels.error)); + Log.Error('err'); + expect(console.error).to.have.been.called; + expect(console.error).to.have.been.calledWith('err'); + }); }); });