Files
webrtc/server/utils.js
2016-02-09 19:08:07 +09:00

69 lines
1.5 KiB
JavaScript

module.exports = {
print_r : print_r,
ObjDump : ObjDump
};
// function trace(msg) {
// console.log( '['+(new Date().toLocaleString())+'] '+ msg);
// }
var max_print_r_depth = 2
function print_r(obj, t, d) {
// define tab spacing
var tab = t || '';
// define depth
var depth = d || 1;
// check if it's array
var isArr = Object.prototype.toString.call(obj) === '[object Array]';
// use {} for object, [] for array
var str = isArr ? ('Array\n' + tab + '[ \n') : ('Object\n' + tab + '{ \n');
// walk through it's properties
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
var val1 = obj[prop];
var val2 = '';
var type = Object.prototype.toString.call(val1);
switch (type) {
// recursive if object/array
case '[object Array]':
case '[object Object]':
if(depth <= max_print_r_depth)
val2 = print_r(val1, (tab + ' '), depth+1);
else
val2 = type;
break;
case '[object String]':
if(val1.length > 40)
val2 = '\'' + val1.substring(0, 40) + '\'';
else
val2 = '\'' + val1 + '\'';
break;
case '[object Function]':
case '[object Uint8Array]':
val2 = type;
break;
default:
val2 = val1;
}
str += tab + ' ' + prop + ' => ' + val2 + ',\n';
}
}
// remove extra comma for last property
str = str.substring(0, str.length - 2) + '\n' + tab;
return isArr ? (str + ']') : (str + '}');
}
function ObjDump (obj) {
console.log( '['+(new Date().toLocaleString())+'] '+ print_r(obj) );
}