Skip to content

Commit a325f24

Browse files
committed
Lib: Add log, warn, and error methods and remove old console.logs
1 parent bf41bab commit a325f24

23 files changed

+79
-58
lines changed

src/.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"Uint8Array": true
1010
},
1111
"rules": {
12-
"strict": [2, "global"]
12+
"strict": [2, "global"],
13+
"no-console": [1]
1314
}
1415
}

src/components/drawing/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ drawing.fillGroupStyle = function(s) {
122122
shape.call(Color.fill, d[0].trace.fillcolor);
123123
}
124124
catch(e) {
125-
console.log(e, s);
125+
Lib.error(e, s);
126126
shape.remove();
127127
}
128128
});

src/components/rangeslider/range_plot.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var d3 = require('d3');
1212

13+
var Lib = require('../../lib');
1314
var Symbols = require('../drawing/symbol_defs');
1415
var Drawing = require('../drawing');
1516

@@ -53,7 +54,7 @@ module.exports = function rangePlot(gd, w, h) {
5354
pointPairs = [];
5455

5556
if(allowedTypes.indexOf(trace.type) < 0) {
56-
console.log('Trace type ' + trace.type + ' not supported for range slider!');
57+
Lib.warn('Trace type ' + trace.type + ' not supported for range slider!');
5758
continue;
5859
}
5960

@@ -161,7 +162,7 @@ function makeScatter(trace, pointPairs, w, h) {
161162
break;
162163

163164
default:
164-
console.log('Fill type ' + trace.fill + ' not supported for range slider! (yet...)');
165+
Lib.warn('Fill type ' + trace.fill + ' not supported for range slider! (yet...)');
165166
break;
166167
}
167168

src/components/shapes/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function getShapeLayer(gd, index) {
351351
shapeLayer = gd._fullLayout._shapeUpperLayer;
352352

353353
if(!shape) {
354-
console.log('getShapeLayer: undefined shape: index', index);
354+
Lib.log('getShapeLayer: undefined shape: index', index);
355355
}
356356
else if(shape.layer === 'below') {
357357
shapeLayer = (shape.xref === 'paper' && shape.yref === 'paper') ?
@@ -491,7 +491,7 @@ shapes.convertPath = function(pathIn, x2p, y2p) {
491491

492492
if(paramNumber > nParams) {
493493
paramString = paramString.replace(/[\s,]*X.*/, '');
494-
console.log('ignoring extra params in segment ' + segment);
494+
Lib.log('Ignoring extra params in segment ' + segment);
495495
}
496496

497497
return segmentType + paramString;

src/lib/dates.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
var d3 = require('d3');
1313
var isNumeric = require('fast-isnumeric');
1414

15+
var Lib = require('../lib');
16+
1517

1618
/**
1719
* dateTime2ms - turn a date object or string s of the form
@@ -122,7 +124,7 @@ function lpad(val, digits) {
122124
*/
123125
exports.ms2DateTime = function(ms, r) {
124126
if(typeof(d3) === 'undefined') {
125-
console.log('d3 is not defined');
127+
Lib.error('d3 is not defined.');
126128
return;
127129
}
128130

src/lib/geo_location_utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var countryRegex = require('country-regex');
13-
var Lib = require('./');
13+
var Lib = require('../lib');
1414

1515

1616
// make list of all country iso3 ids from at runtime
@@ -32,8 +32,8 @@ exports.locationToFeature = function(locationmode, location, features) {
3232
if(feature.id === locationId) return feature;
3333
}
3434

35-
console.warn([
36-
'location with id', locationId,
35+
Lib.warn([
36+
'Location with id', locationId,
3737
'does not have a matching topojson feature at this resolution.'
3838
].join(' '));
3939
};
@@ -53,5 +53,5 @@ function countryNameToISO3(countryName) {
5353
if(regex.test(countryName.toLowerCase())) return iso3;
5454
}
5555

56-
console.warn('unrecognized country name: ' + countryName + '.');
56+
Lib.warn('Unrecognized country name: ' + countryName + '.');
5757
}

src/lib/index.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,36 @@ lib.pauseEvent = function(e) {
9999
* ------------------------------------------
100100
*/
101101

102-
// set VERBOSE to true to get a lot more logging and tracing
103-
lib.VERBOSE = false;
104-
// console.log that only runs if VERBOSE is on
102+
/* eslint-disable no-console */
105103
lib.log = function() {
106-
if(lib.VERBOSE) console.log.apply(console, arguments);
107104
if(config.logging > 1) {
108-
console.log.apply(console, arguments);
105+
var messages = ['LOG:'];
106+
107+
for(var i = 0; i < arguments.length; i++) {
108+
messages.push(arguments[i]);
109+
}
110+
111+
if(console.trace) {
112+
console.trace.apply(console, messages);
113+
} else {
114+
console.log.apply(console, messages);
115+
}
109116
}
110117
};
111118

112119
lib.warn = function() {
113120
if(config.logging > 0) {
114-
console.warn.apply(console, arguments);
121+
var messages = ['WARN:'];
122+
123+
for(var i = 0; i < arguments.length; i++) {
124+
messages.push(arguments[i]);
125+
}
126+
127+
if(console.trace) {
128+
console.trace.apply(console, messages);
129+
} else {
130+
console.log.apply(console, messages);
131+
}
115132
}
116133
};
117134

@@ -120,6 +137,7 @@ lib.error = function() {
120137
console.error.apply(console, arguments);
121138
}
122139
};
140+
/* eslint-enable no-console */
123141

124142
// constrain - restrict a number v to be between v0 and v1
125143
lib.constrain = function(v, v0, v1) {
@@ -432,7 +450,6 @@ lib.addStyleRule = function(selector, styleString) {
432450
else if(styleSheet.addRule) {
433451
styleSheet.addRule(selector, styleString, 0);
434452
}
435-
else console.warn('addStyleRule failed');
436453
else lib.warn('addStyleRule failed');
437454
};
438455

src/lib/search.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
var isNumeric = require('fast-isnumeric');
1313

14+
var Lib = require('../lib');
15+
1416

1517
/**
1618
* findBin - find the bin for val - note that it can return outside the
@@ -45,7 +47,7 @@ exports.findBin = function(val, bins, linelow) {
4547
if(test(bins[n], val)) n1 = n + 1;
4648
else n2 = n;
4749
}
48-
if(c > 90) console.log('Long binary search...');
50+
if(c > 90) Lib.log('Long binary search...');
4951
return n1 - 1;
5052
}
5153
};

src/lib/svg_text_utils.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
var Plotly = require('../plotly');
1515
var d3 = require('d3');
1616

17+
var Lib = require('../lib');
1718
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
1819

1920
var util = module.exports = {};
@@ -36,7 +37,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
3637
childNode = childNode.nextSibling;
3738
}
3839
if(dom.querySelector('parsererror')) {
39-
console.log(dom.querySelector('parsererror div').textContent);
40+
Lib.log(dom.querySelector('parsererror div').textContent);
4041
return null;
4142
}
4243
return d3.select(this.node().lastChild);
@@ -202,7 +203,7 @@ function texToSVG(_texString, _config, _callback) {
202203
var glyphDefs = d3.select('body').select('#MathJax_SVG_glyphs');
203204

204205
if(tmpDiv.select('.MathJax_SVG').empty() || !tmpDiv.select('svg').node()) {
205-
console.log('There was an error in the tex syntax.', _texString);
206+
Lib.log('There was an error in the tex syntax.', _texString);
206207
_callback();
207208
}
208209
else {

src/plot_api/plot_api.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Plotly.plot = function(gd, data, layout, config) {
6363
// if there's no data or layout, and this isn't yet a plotly plot
6464
// container, log a warning to help plotly.js users debug
6565
if(!data && !layout && !Lib.isPlotDiv(gd)) {
66-
console.log('Warning: calling Plotly.plot as if redrawing ' +
66+
Lib.warn('Calling Plotly.plot as if redrawing ' +
6767
'but this container doesn\'t yet have a plot.', gd);
6868
}
6969

@@ -539,7 +539,7 @@ function cleanLayout(layout) {
539539
}
540540

541541
if(layout.annotations !== undefined && !Array.isArray(layout.annotations)) {
542-
console.log('annotations must be an array');
542+
Lib.warn('Annotations must be an array.');
543543
delete layout.annotations;
544544
}
545545
var annotationsLen = (layout.annotations || []).length;
@@ -561,7 +561,7 @@ function cleanLayout(layout) {
561561
}
562562

563563
if(layout.shapes !== undefined && !Array.isArray(layout.shapes)) {
564-
console.log('shapes must be an array');
564+
Lib.warn('Shapes must be an array.');
565565
delete layout.shapes;
566566
}
567567
var shapesLen = (layout.shapes || []).length;
@@ -785,9 +785,7 @@ function cleanData(data, existingData) {
785785

786786
// sanitize rgb(fractions) and rgba(fractions) that old tinycolor
787787
// supported, but new tinycolor does not because they're not valid css
788-
Lib.markTime('finished rest of cleanData, starting color');
789788
Color.clean(trace);
790-
Lib.markTime('finished cleanData color.clean');
791789
}
792790
}
793791

@@ -816,7 +814,7 @@ Plotly.redraw = function(gd) {
816814
gd = getGraphDiv(gd);
817815

818816
if(!Lib.isPlotDiv(gd)) {
819-
console.log('This element is not a Plotly Plot', gd);
817+
Lib.warn('This element is not a Plotly plot.', gd);
820818
return;
821819
}
822820

@@ -891,7 +889,6 @@ function doCalcdata(gd) {
891889
if(!cd[0].t) cd[0].t = {};
892890
cd[0].trace = trace;
893891

894-
Lib.markTime('done with calcdata for ' + i);
895892
calcdata[i] = cd;
896893
}
897894
}
@@ -1548,7 +1545,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
15481545
if(traces === undefined) traces = val; // the 3-arg form
15491546
}
15501547
else {
1551-
console.log('restyle fail', astr, val, traces);
1548+
Lib.warn('Restyle fail.', astr, val, traces);
15521549
return Promise.reject();
15531550
}
15541551

@@ -2092,7 +2089,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
20922089
if(typeof astr === 'string') aobj[astr] = val;
20932090
else if(Lib.isPlainObject(astr)) aobj = astr;
20942091
else {
2095-
console.log('relayout fail', astr, val);
2092+
Lib.warn('Relayout fail.', astr, val);
20962093
return Promise.reject();
20972094
}
20982095

@@ -2283,7 +2280,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
22832280
}
22842281
else undoit[ai] = obji;
22852282
}
2286-
else console.log('???', aobj);
2283+
else Lib.log('???', aobj);
22872284
}
22882285
if((refAutorange(obji, 'x') || refAutorange(obji, 'y')) &&
22892286
!Lib.containsAny(ai, ['color', 'opacity', 'align', 'dash'])) {

src/plot_api/plot_config.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

11+
var Lib = require('../lib');
12+
1213
/**
1314
* This will be transfered over to gd and overridden by
1415
* config args to Plotly.plot.
@@ -83,8 +84,10 @@ module.exports = {
8384
setBackground: defaultSetBackground,
8485

8586
// URL to topojson files used in geo charts
86-
topojsonURL: 'https://cdn.plot.ly/'
87+
topojsonURL: 'https://cdn.plot.ly/',
8788

89+
// Turn all console logging on or off (errors will be thrown)
90+
logging: false
8891
};
8992

9093
// where and how the background gets set can be overridden by context
@@ -93,5 +96,5 @@ function defaultSetBackground(gd, bgColor) {
9396
try {
9497
gd._fullLayout._paper.style('background', bgColor);
9598
}
96-
catch(e) { console.log(e); }
99+
catch(e) { Lib.error(e); }
97100
}

src/plots/cartesian/axes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ axes.doTicks = function(gd, axid, skipTitle) {
14121412
};
14131413
}
14141414
else {
1415-
console.log('unrecognized doTicks axis', axid);
1415+
Lib.warn('Unrecognized doTicks axis:', axid);
14161416
return;
14171417
}
14181418
var axside = ax.side || sides[0],

src/plots/cartesian/clean_datum.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function cleanDatum(c) {
3030
c = c.toString().replace(/['"%,$# ]/g, '');
3131
}
3232
catch(e) {
33-
console.log(e, c);
33+
Lib.error(e, c);
3434
}
3535

3636
return c;

src/plots/cartesian/dragbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
372372
var wheelDelta = -e.deltaY;
373373
if(!isFinite(wheelDelta)) wheelDelta = e.wheelDelta / 10;
374374
if(!isFinite(wheelDelta)) {
375-
console.log('did not find wheel motion attributes', e);
375+
Lib.log('Did not find wheel motion attributes: ', e);
376376
return;
377377
}
378378

src/plots/cartesian/graph_interact.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ function hover(gd, evt, subplot) {
442442
else yvalArray = p2c(yaArray, ypx);
443443

444444
if(!isNumeric(xvalArray[0]) || !isNumeric(yvalArray[0])) {
445-
console.log('Plotly.Fx.hover failed', evt, gd);
445+
Lib.warn('Plotly.Fx.hover failed', evt, gd);
446446
return dragElement.unhoverRaw(gd, evt);
447447
}
448448
}
@@ -529,7 +529,7 @@ function hover(gd, evt, subplot) {
529529
}
530530
}
531531
else {
532-
console.log('unrecognized trace type in hover', trace);
532+
Lib.log('Unrecognized trace type in hover:', trace);
533533
}
534534

535535
// in closest mode, remove any existing (farther) points

src/plots/cartesian/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
'use strict';
1111

12-
var Lib = require('../../lib');
1312
var Plots = require('../plots');
1413

1514
var constants = require('./constants');

src/plots/cartesian/set_convert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ module.exports = function setConvert(ax) {
167167
ax.range[1] = ar1[1];
168168
}
169169
}
170-
catch(e) { console.log(e, ax.range); }
170+
catch(e) { Lib.error(e, ax.range); }
171171
}
172172
}
173173
else if(ax.type === 'category') {

0 commit comments

Comments
 (0)