Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($log): avoid console.log.apply calls in IE
Browse files Browse the repository at this point in the history
In IE window.console.log and friends are functions that don't have apply or call fns.

For this reason we have to treat them specially and do our best to log at least
something when running in this browser.

Closes #805
  • Loading branch information
IgorMinar committed Mar 20, 2012
1 parent 9171c76 commit 15213ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/service/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,23 @@ function $LogProvider(){
}

function consoleLog(type) {
var console = $window.console || {};
var logFn = console[type] || console.log || noop;
var console = $window.console || {},
logFn = console[type] || console.log || noop;

if (logFn.apply) {
return function() {
var args = [];
forEach(arguments, function(arg){
forEach(arguments, function(arg) {
args.push(formatError(arg));
});
return logFn.apply(console, args);
};
} else {
// we are IE, in which case there is nothing we can do
return logFn;
}

// we are IE which either doesn't have window.console => this is noop and we do nothing,
// or we are IE where console.log doesn't have apply so we log at least first 2 args
return function(arg1, arg2) {
logFn(arg1, arg2);
}
}
}];
Expand Down
35 changes: 29 additions & 6 deletions test/service/logSpec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict';

describe('$log', function() {
var $window;
var logger;
var $window, logger, log, warn, info, error;


function log() { logger+= 'log;'; }
function warn() { logger+= 'warn;'; }
function info() { logger+= 'info;'; }
function error() { logger+= 'error;'; }

beforeEach(module(function($provide){
$window = {};
logger = '';
log = function() { logger+= 'log;'; };
warn = function() { logger+= 'warn;'; };
info = function() { logger+= 'info;'; };
error = function() { logger+= 'error;'; };

$provide.provider('$log', $LogProvider);
$provide.value('$exceptionHandler', angular.mock.rethrow);
$provide.value('$window', $window);
Expand Down Expand Up @@ -58,6 +59,28 @@ describe('$log', function() {
));


it("should work in IE where console.error doesn't have apply method", inject(
function() {
log.apply = log.call =
warn.apply = warn.call =
info.apply = info.call =
error.apply = error.call = null;

$window.console = {log: log,
warn: warn,
info: info,
error: error};
},
function($log) {
$log.log.apply($log);
$log.warn.apply($log);
$log.info.apply($log);
$log.error.apply($log);
expect(logger).toEqual('log;warn;info;error;');
})
);


describe('$log.error', function() {
var e, $log, errorArgs;

Expand Down

0 comments on commit 15213ec

Please sign in to comment.