Fix logging option (#1302)

This commit is contained in:
Niklas von Hertzen 2017-12-11 20:23:43 +08:00
parent 250208dc99
commit d87fef11a4
3 changed files with 9 additions and 5 deletions

View File

@ -17,6 +17,7 @@ These are all of the available configuration options.
| canvas | `null` | Existing `canvas` element to use as a base for drawing on | canvas | `null` | Existing `canvas` element to use as a base for drawing on
| foreignObjectRendering | `false` | Whether to use ForeignObject rendering if the browser supports it | foreignObjectRendering | `false` | Whether to use ForeignObject rendering if the browser supports it
| imageTimeout | `15000` | Timeout for loading an image (in milliseconds). Set to `0` to disable timeout. | imageTimeout | `15000` | Timeout for loading an image (in milliseconds). Set to `0` to disable timeout.
| logging | `true` | Enable logging for debug purposes
| proxy | `null` | Url to the [proxy](/proxy/) which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded. | proxy | `null` | Url to the [proxy](/proxy/) which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.
| removeContainer | `true` | Whether to cleanup the cloned DOM elements html2canvas creates temporarily | removeContainer | `true` | Whether to cleanup the cloned DOM elements html2canvas creates temporarily
| scale | `window.devicePixelRatio` | The scale to use for rendering. Defaults to the browsers device pixel ratio. | scale | `window.devicePixelRatio` | The scale to use for rendering. Defaults to the browsers device pixel ratio.

View File

@ -2,21 +2,23 @@
'use strict'; 'use strict';
export default class Logger { export default class Logger {
enabled: boolean;
start: number; start: number;
id: ?string; id: ?string;
constructor(id: ?string, start: ?number) { constructor(enabled: boolean, id: ?string, start: ?number) {
this.enabled = enabled;
this.start = start ? start : Date.now(); this.start = start ? start : Date.now();
this.id = id; this.id = id;
} }
child(id: string) { child(id: string) {
return new Logger(id, this.start); return new Logger(this.enabled, id, this.start);
} }
// eslint-disable-next-line flowtype/no-weak-types // eslint-disable-next-line flowtype/no-weak-types
log(...args: any) { log(...args: any) {
if (window.console && window.console.log) { if (this.enabled && window.console && window.console.log) {
Function.prototype.bind Function.prototype.bind
.call(window.console.log, window.console) .call(window.console.log, window.console)
.apply( .apply(
@ -31,7 +33,7 @@ export default class Logger {
// eslint-disable-next-line flowtype/no-weak-types // eslint-disable-next-line flowtype/no-weak-types
error(...args: any) { error(...args: any) {
if (window.console && window.console.error) { if (this.enabled && window.console && window.console.error) {
Function.prototype.bind Function.prototype.bind
.call(window.console.error, window.console) .call(window.console.error, window.console)
.apply( .apply(

View File

@ -15,6 +15,7 @@ export type Options = {
canvas: ?HTMLCanvasElement, canvas: ?HTMLCanvasElement,
foreignObjectRendering: boolean, foreignObjectRendering: boolean,
imageTimeout: number, imageTimeout: number,
logging: boolean,
proxy: ?string, proxy: ?string,
removeContainer: ?boolean, removeContainer: ?boolean,
scale: number, scale: number,
@ -37,7 +38,7 @@ const html2canvas = (element: HTMLElement, conf: ?Options): Promise<*> => {
} }
const config = conf || {}; const config = conf || {};
const logger = new Logger(); const logger = new Logger(typeof config.logging === 'boolean' ? config.logging : true);
if (__DEV__ && typeof config.onrendered === 'function') { if (__DEV__ && typeof config.onrendered === 'function') {
logger.error( logger.error(