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.
*/
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) {

View File

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

View File

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

View File

@ -9,7 +9,7 @@
*
*/
import * as Log from '../util/logging.js';
import { Log } from '../util/logging.js';
export default class HextileDecoder {
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";
export default class TightDecoder {

View File

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

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

View File

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