Skip to content

Commit

Permalink
Use internal console log functions
Browse files Browse the repository at this point in the history
The global console object can not reliably be assumed to be the wrapped
console object with all the individual logging functions, it may also be
the native console object that only has the "print" function. 

Let the Console object export individual log functions to use internally
that are guaranteed to work at all times - even if the application code
messes with the console object.

In tests the functions simply redirect to the native console directly.

Change-Id: I9c9b01a9e22a8a0a5ab4561f5843b6dad41032e9
  • Loading branch information
tbuschto committed Aug 29, 2017
1 parent 007e7e5 commit 53f4874
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 54 deletions.
7 changes: 4 additions & 3 deletions src/tabris/Animation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import NativeObject from './NativeObject';
import {warn} from './Console';

const ANIMATABLE_PROPERTIES = ['opacity', 'transform'];

Expand Down Expand Up @@ -59,15 +60,15 @@ export function animate(properties, options) {
this._encodeProperty(this._getTypeDef(property), properties[property]);
this._storeProperty(property, animatedProps[property], options);
} catch (ex) {
console.warn(this + ': Ignored invalid animation property value for "' + property + '"');
warn(this + ': Ignored invalid animation property value for "' + property + '"');
}
} else {
console.warn(this + ': Ignored invalid animation property "' + property + '"');
warn(this + ': Ignored invalid animation property "' + property + '"');
}
}
for (let option in options) {
if (!(option in PROPERTIES) && option !== 'name') {
console.warn(this + ': Ignored invalid animation option "' + option + '"');
warn(this + ': Ignored invalid animation option "' + option + '"');
}
}
return new Promise((resolve, reject) => {
Expand Down
3 changes: 2 additions & 1 deletion src/tabris/CanvasContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {colorStringToArray, colorArrayToString} from './util-colors';
import {fontStringToObject, fontObjectToString} from './util-fonts';
import ImageData from './ImageData';
import GC from './GC';
import {warn} from './Console';

export default class CanvasContext {

Expand Down Expand Up @@ -293,7 +294,7 @@ function defineProperty(context, name) {
context._gc.addOperation(name);
prop.addOperations.call(context, context._state[name]);
} catch (error) {
console.warn('Unsupported value for ' + name + ': ' + value);
warn('Unsupported value for ' + name + ': ' + value);
}
}
});
Expand Down
18 changes: 17 additions & 1 deletion src/tabris/Console.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import {format} from './Formatter';

const defaultConsole = global.console.print
? createConsole(global.console)
: global.console;

if (!defaultConsole.debug) {
// The native node console has no "debug" method
defaultConsole.debug = function(...args) {
defaultConsole.log(...args);
};
}

export const debug = function(...args) { defaultConsole.debug(...args); };
export const info = function(...args) { defaultConsole.info(...args); };
export const log = function(...args) { defaultConsole.log(...args); };
export const warn = function(...args) { defaultConsole.warn(...args); };
export const error = function(...args) { defaultConsole.error(...args); };

export function createConsole(nativeConsole) {
let console = {};
for (let name of ['debug', 'info', 'log', 'warn', 'error']) {
Expand All @@ -9,4 +26,3 @@ export function createConsole(nativeConsole) {
}
return console;
}

5 changes: 3 additions & 2 deletions src/tabris/Document.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Event, {addDOMEventTargetMethods} from './Event';
import {log, error} from './Console';

export function addDOMDocument(target) {

Expand Down Expand Up @@ -62,8 +63,8 @@ function handleElementInserted(parent, child, target) {
try {
result = tabris._client.loadAndExecute(child.src, '', '');
} catch (ex) {
console.error('Error loading ' + child.src + ':', ex);
console.log(ex.stack);
error('Error loading ' + child.src + ':', ex);
log(ex.stack);
if (typeof child.onerror === 'function') {
child.onerror.call(target, ex);
}
Expand Down
11 changes: 6 additions & 5 deletions src/tabris/Layout.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import {omit} from './util';
import {types} from './property-types';
import {warn} from './Console';

export default {

checkConsistency(layoutData) {
let result = layoutData;
if ('centerX' in result) {
if (('left' in result) || ('right' in result)) {
console.warn('Inconsistent layoutData: centerX overrides left and right');
warn('Inconsistent layoutData: centerX overrides left and right');
result = omit(result, ['left', 'right']);
}
}
if ('baseline' in result) {
if (('top' in result) || ('bottom' in result) || ('centerY' in result)) {
console.warn('Inconsistent layoutData: baseline overrides top, bottom, and centerY');
warn('Inconsistent layoutData: baseline overrides top, bottom, and centerY');
result = omit(result, ['top', 'bottom', 'centerY']);
}
} else if ('centerY' in result) {
if (('top' in result) || ('bottom' in result)) {
console.warn('Inconsistent layoutData: centerY overrides top and bottom');
warn('Inconsistent layoutData: centerY overrides top and bottom');
result = omit(result, ['top', 'bottom']);
}
}
if ('left' in result && 'right' in result && 'width' in result) {
console.warn('Inconsistent layoutData: left and right are set, ignore width');
warn('Inconsistent layoutData: left and right are set, ignore width');
result = omit(result, ['width']);
}
if ('top' in result && 'bottom' in result && 'height' in result) {
console.warn('Inconsistent layoutData: top and bottom are set, ignore height');
warn('Inconsistent layoutData: top and bottom are set, ignore height');
result = omit(result, ['height']);
}
return result;
Expand Down
11 changes: 6 additions & 5 deletions src/tabris/NativeObject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {types} from './property-types';
import {warn} from './Console';
import EventObject from './EventObject';
import Events from './Events';

Expand Down Expand Up @@ -63,7 +64,7 @@ export default class NativeObject extends EventsClass {

$getProperty(name) {
if (this._isDisposed) {
console.warn('Cannot get property "' + name + '" on disposed object');
warn('Cannot get property "' + name + '" on disposed object');
return;
}
let getter = this.$getPropertyGetter(name) || this._getStoredProperty;
Expand All @@ -73,15 +74,15 @@ export default class NativeObject extends EventsClass {

$setProperty(name, value) {
if (this._isDisposed) {
console.warn('Cannot set property "' + name + '" on disposed object');
warn('Cannot set property "' + name + '" on disposed object');
return;
}
let typeDef = this._getTypeDef(name);
let encodedValue;
try {
encodedValue = this._encodeProperty(typeDef, value);
} catch (ex) {
console.warn(this + ': Ignored unsupported value for property "' + name + '": ' + ex.message);
warn(this + ': Ignored unsupported value for property "' + name + '": ' + ex.message);
return;
}
let setter = this.$getPropertySetter(name) || this._storeProperty;
Expand Down Expand Up @@ -236,7 +237,7 @@ function setExistingProperty(name, value) {
if (name in this) {
this[name] = value;
} else {
console.warn('Unknown property "' + name + '"');
warn('Unknown property "' + name + '"');
}
}

Expand Down Expand Up @@ -281,7 +282,7 @@ function wrapCoder(fn, args) {
}

function readOnlySetter(name) {
console.warn(`Can not set read-only property "${name}"`);
warn(`Can not set read-only property "${name}"`);
}

function defaultSetter(name, value) {
Expand Down
9 changes: 5 additions & 4 deletions src/tabris/Tabris.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Events from './Events';
import NativeBridge from './NativeBridge';
import ProxyStore from './ProxyStore';
import {error, log} from './Console';

export default class Tabris {

Expand Down Expand Up @@ -38,14 +39,14 @@ export default class Tabris {
try {
returnValue = proxy._trigger(event, param);
} catch (error) {
console.error(error);
console.log(error.stack);
error(error);
log(error.stack);
}
}
this.trigger('flush');
} catch (ex) {
console.error(ex);
console.log(ex.stack);
error(ex);
log(ex.stack);
}
return returnValue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/tabris/WebSocket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NativeObject from './NativeObject';
import Event, {addDOMEventTargetMethods, defineEventHandlerProperties} from './Event';
import {omit} from './util';
import {warn} from './Console';

const CONNECTING = 0;
const OPEN = 1;
Expand Down Expand Up @@ -85,7 +86,7 @@ export default class WebSocket {
}

set bufferedAmount(bufferedAmount) {
console.warn('Can not set read-only property "bufferedAmount"');
warn('Can not set read-only property "bufferedAmount"');
}

get bufferedAmount() {
Expand Down
3 changes: 2 additions & 1 deletion src/tabris/property-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {colorArrayToString, colorStringToArray} from './util-colors';
import {fontObjectToString, fontStringToObject} from './util-fonts';
import NativeObject from './NativeObject';
import WidgetCollection from './WidgetCollection';
import {warn} from './Console';

export let types = {

Expand Down Expand Up @@ -128,7 +129,7 @@ export let types = {
}
});
if ('scale' in value && ('width' in value || 'height' in value)) {
console.warn('Image scale is ignored if width or height are given');
warn('Image scale is ignored if width or height are given');
}
return imageToArray(value);
},
Expand Down
8 changes: 2 additions & 6 deletions src/tabris/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {error} from './Console';

export function checkVersion(tabrisVersionString, clientVersionString) {
if (!clientVersionString) {
Expand All @@ -6,12 +7,7 @@ export function checkVersion(tabrisVersionString, clientVersionString) {
let tabrisVersion = tabrisVersionString.split('.');
let clientVersion = clientVersionString.split('.');
if (tabrisVersion[0] !== clientVersion[0] || tabrisVersion[1] !== clientVersion[1]) {
printError(`Version mismatch: JavaScript module "tabris" (version ${tabrisVersionString}) ` +
error(`Version mismatch: JavaScript module "tabris" (version ${tabrisVersionString}) ` +
`is incompatible with the native tabris platform (version ${clientVersionString}).`);
}
}

// At this point the wrapped console object may not be available
function printError(msg) {
console.print ? console.print('error', msg) : console.error(msg);
}
7 changes: 4 additions & 3 deletions src/tabris/widgets/CollectionView.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NativeObject from '../NativeObject';
import Widget from '../Widget';
import Composite from './Composite';
import {warn} from '../Console';

const EVENT_TYPES = ['refresh', 'select', 'scroll'];

Expand Down Expand Up @@ -130,8 +131,8 @@ export default class CollectionView extends Composite {
}
cell._parent = this;
this._addChild(cell);
cell._setParent = () => console.warn('Cannot re-parent collection view cell');
cell.dispose = () => console.warn('Cannot dispose of collection view cell');
cell._setParent = () => warn('Cannot re-parent collection view cell');
cell.dispose = () => warn('Cannot dispose of collection view cell');
return cell;
}

Expand Down Expand Up @@ -241,7 +242,7 @@ function encodeCellHeight(value) {
if (isNumber(value)) {
return Math.max(-1, value);
}
console.warn('Invalid cell height: ' + value);
warn('Invalid cell height: ' + value);
}

let triggerChangeFirstVisibleIndex = createDelegate('firstVisibleIndex');
Expand Down
3 changes: 2 additions & 1 deletion src/tabris/widgets/TabFolder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NativeObject from '../NativeObject';
import Composite from './Composite';
import Tab from './Tab';
import {warn} from '../Console';

const EVENT_TYPES = ['select', 'scroll'];

Expand Down Expand Up @@ -49,7 +50,7 @@ NativeObject.defineProperties(TabFolder.prototype, {
selection: {
set(name, tab) {
if (this._children.indexOf(tab) < 0) {
console.warn('Can not set TabFolder selection to ' + tab);
warn('Can not set TabFolder selection to ' + tab);
return;
}
this._nativeSet('selection', tab.cid);
Expand Down
Loading

0 comments on commit 53f4874

Please sign in to comment.