Skip to content

Instantly share code, notes, and snippets.

@kobake
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobake/0840d79e2c2c0067c8f5 to your computer and use it in GitHub Desktop.
Save kobake/0840d79e2c2c0067c8f5 to your computer and use it in GitHub Desktop.
関数コールの監視その2
// コールを監視するかどうか
function IsWatchFunction(name){
if(name == 'setTimeout') return false;
if(name == 'clearTimeout') return false;
if(name == 'DOMEvent') return false;
if(name == 'typeOf') return false;
if(name == 'instanceOf') return false;
if(name == 'getCallStack') return false;
if(name == 'logging') return false;
if(name == 'watchFunctions') return false;
if(name == 'IsWatchFunction') return false;
if(name == 'IsWatchDeepFunction') return false;
return true;
}
// コールスタックまで表示するかどうか
function IsWatchDeepFunction(name){
return name == 'getComputedStyle';
}
// window配下の関数すべてにログ関数を仕込む
// ※仕込む必要ないやつは名前判定でスキップする
function watchFunctions(loggingFn) {
var name, fn;
for (name in window) {
fn = window[name];
if (typeof fn === 'function') {
// ログ出力したくない関数をここでスキップ
if(!IsWatchFunction(name))continue;
// 関数ラップ
window[name] = (function(name, fn) {
var args = arguments;
return function() {
loggingFn.apply(this, args);
return fn.apply(this, arguments);
}
})(name, fn);
}
}
}
// コールスタック取得
function getCallStack()
{
stack = (new Error).stack;
stack = stack.replace(/Error\n( +)at getCallStack[^\n]+\n/, 'DebugInfo\n');
return stack;
}
// ログ出力関数
function logging(name, fn) {
console.log("calling " + name);
if(IsWatchDeepFunction(name)){
console.log("--call stack--");
console.log(getCallStack());
console.log("");
}
}
// 登録実行
watchFunctions(logging);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment