Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Prebid label to console logs #2966

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 30 additions & 40 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ var tNumb = 'Number';
var tObject = 'Object';
var tBoolean = 'Boolean';
var toString = Object.prototype.toString;
let infoLogger = null;
let warnLogger = null;
try {
infoLogger = console.info.bind(window.console);
warnLogger = console.warn.bind(window.console);
} catch (e) {
}
let consoleExists = Boolean(window.console);
let consoleLogExists = Boolean(consoleExists && window.console.log);
let consoleInfoExists = Boolean(consoleExists && window.console.info);
let consoleWarnExists = Boolean(consoleExists && window.console.warn);
let consoleErrorExists = Boolean(consoleExists && window.console.error);

/*
* Substitutes into a string from a given map using the token
Expand Down Expand Up @@ -260,42 +258,43 @@ exports.getTopWindowReferrer = function() {
}
};

exports.logWarn = function (msg, args) {
if (debugTurnedOn() && console.warn) {
if (warnLogger) {
if (!args || args.length === 0) {
args = '';
}

warnLogger('WARNING: ' + msg + ((args === '') ? '' : ' : params : '), args);
}
/**
* Wrappers to console.(log | info | warn | error). Takes N arguments, the same as the native methods
*/
exports.logMessage = function () {
if (debugTurnedOn() && consoleLogExists) {
console.log.apply(console, decorateLog(arguments, 'MESSAGE:'));
}
};

exports.logInfo = function (msg, args) {
if (debugTurnedOn() && hasConsoleLogger()) {
if (infoLogger) {
if (!args || args.length === 0) {
args = '';
}
exports.logInfo = function () {
if (debugTurnedOn() && consoleInfoExists) {
console.info.apply(console, decorateLog(arguments, 'INFO:'));
}
};

infoLogger('INFO: ' + msg + ((args === '') ? '' : ' : params : '), args);
}
exports.logWarn = function () {
if (debugTurnedOn() && consoleWarnExists) {
console.warn.apply(console, decorateLog(arguments, 'WARNING:'));
}
};

exports.logMessage = function (msg) {
if (debugTurnedOn() && hasConsoleLogger()) {
console.log('MESSAGE: ' + msg);
exports.logError = function () {
if (debugTurnedOn() && consoleErrorExists) {
console.error.apply(console, decorateLog(arguments, 'ERROR:'));
}
};

function hasConsoleLogger() {
return (window.console && window.console.log);
function decorateLog(args, prefix) {
args = [].slice.call(args);
prefix && args.unshift(prefix);
args.unshift('display: inline-block; color: #fff; background: #3b88c3; padding: 1px 4px; border-radius: 3px;');
args.unshift('%cPrebid');
return args;
}

function hasConsoleError() {
return (window.console && window.console.error);
function hasConsoleLogger() {
return consoleLogExists;
}

exports.hasConsoleLogger = hasConsoleLogger;
Expand All @@ -312,15 +311,6 @@ var debugTurnedOn = function () {

exports.debugTurnedOn = debugTurnedOn;

/**
* Wrapper to console.error. Takes N arguments to log the same as console.error.
*/
exports.logError = function () {
if (debugTurnedOn() && hasConsoleError()) {
console.error.apply(console, arguments);
}
};

exports.createInvisibleIframe = function _createInvisibleIframe() {
var f = document.createElement('iframe');
f.id = _getUniqueIdentifierStr();
Expand Down