88
99exports = module . exports = _dereq_ ( './debug' ) ;
1010exports . log = log ;
11+ exports . formatArgs = formatArgs ;
1112exports . save = save ;
1213exports . load = load ;
1314exports . useColors = useColors ;
@@ -48,14 +49,14 @@ exports.formatters.j = function(v) {
4849 return JSON . stringify ( v ) ;
4950} ;
5051
52+
5153/**
52- * Invokes `console.log()` when available.
53- * No-op when `console.log` is not a "function".
54+ * Colorize log arguments if enabled.
5455 *
5556 * @api public
5657 */
5758
58- function log ( ) {
59+ function formatArgs ( ) {
5960 var args = arguments ;
6061 var useColors = this . useColors ;
6162
@@ -66,33 +67,43 @@ function log() {
6667 + ( useColors ? '%c ' : ' ' )
6768 + '+' + exports . humanize ( this . diff ) ;
6869
69- if ( useColors ) {
70- var c = 'color: ' + this . color ;
71- args = [ args [ 0 ] , c , '' ] . concat ( Array . prototype . slice . call ( args , 1 ) ) ;
70+ if ( ! useColors ) return args
71+
72+ var c = 'color: ' + this . color ;
73+ args = [ args [ 0 ] , c , '' ] . concat ( Array . prototype . slice . call ( args , 1 ) ) ;
74+
75+ // the final "%c" is somewhat tricky, because there could be other
76+ // arguments passed either before or after the %c, so we need to
77+ // figure out the correct index to insert the CSS into
78+ var index = 0 ;
79+ var lastC = 0 ;
80+ args [ 0 ] . replace ( / % [ a - z % ] / g, function ( match ) {
81+ if ( '%%' === match ) return ;
82+ index ++ ;
83+ if ( '%c' === match ) {
84+ // we only are interested in the *last* %c
85+ // (the user may have provided their own)
86+ lastC = index ;
87+ }
88+ } ) ;
7289
73- // the final "%c" is somewhat tricky, because there could be other
74- // arguments passed either before or after the %c, so we need to
75- // figure out the correct index to insert the CSS into
76- var index = 0 ;
77- var lastC = 0 ;
78- args [ 0 ] . replace ( / % [ a - z % ] / g, function ( match ) {
79- if ( '%%' === match ) return ;
80- index ++ ;
81- if ( '%c' === match ) {
82- // we only are interested in the *last* %c
83- // (the user may have provided their own)
84- lastC = index ;
85- }
86- } ) ;
90+ args . splice ( lastC , 0 , c ) ;
91+ return args ;
92+ }
8793
88- args . splice ( lastC , 0 , c ) ;
89- }
94+ /**
95+ * Invokes `console.log()` when available.
96+ * No-op when `console.log` is not a "function".
97+ *
98+ * @api public
99+ */
90100
101+ function log ( ) {
91102 // This hackery is required for IE8,
92103 // where the `console.log` function doesn't have 'apply'
93104 return 'object' == typeof console
94105 && 'function' == typeof console . log
95- && Function . prototype . apply . call ( console . log , console , args ) ;
106+ && Function . prototype . apply . call ( console . log , console , arguments ) ;
96107}
97108
98109/**
@@ -246,7 +257,11 @@ function debug(namespace) {
246257 return match ;
247258 } ) ;
248259
249- exports . log . apply ( self , args ) ;
260+ if ( 'function' === typeof exports . formatArgs ) {
261+ args = exports . formatArgs . apply ( self , args ) ;
262+ }
263+ var logFn = exports . log || enabled . log || console . log . bind ( console ) ;
264+ logFn . apply ( self , args ) ;
250265 }
251266 enabled . enabled = true ;
252267
0 commit comments