Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Nov 7, 2013
0 parents commit 008e116
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules
9 changes: 9 additions & 0 deletions .jshintrc
@@ -0,0 +1,9 @@
{
"asi": true,
"laxbreak": true,
"expr": true,
"strict": false,
"node": true,
"browser": true,
"devel": true
}
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
- "0.6"
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Feross Aboukhadijeh

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
@@ -0,0 +1,40 @@
call-log
==========

[![Build Status](https://travis-ci.org/feross/call-log.png?branch=master)](https://travis-ci.org/feross/call-log)

[![browser support](https://ci.testling.com/feross/call-log.png)](https://ci.testling.com/feross/call-log)

Instrument an object or class so that anytime a method is invoked, it gets logged to the console.

## Installation

`npm install call-log`

## Usage

```js
var callLog = require('call-log')

function Cat () {}
Cat.prototype.meow = function (sound) { sound }
Cat.prototype.scratch = function () {}

Cat.static1 = function () {}

// Add instrumentation to Cat
callLog(Cat)

var cat = new Cat()
cat.meow()
cat.meow('MEOAAAAWWW!')

// Prints:
// "Called: meow()"
// "Called: meow(MEOAAAAWWW!)"

```

## MIT License

Copyright (c) [Feross Aboukhadijeh](http://feross.org)
40 changes: 40 additions & 0 deletions index.js
@@ -0,0 +1,40 @@
var is = require('core-util-is') // added in Node 0.12

/**
* Instrument an object or class so that anytime a method is invoked, it gets
* logged to the console.
*
* @param {function} constructor
*/
module.exports = function (constructor) {
Object.keys(constructor).forEach(function (methodName) {
if (!is.isFunction(constructor[methodName])) {
return
}

var originalMethod = constructor[methodName]
constructor[methodName] = function () {
var args = Array.prototype.slice.call(arguments)
console.log('Called: ' + methodName + '(' + args + ')')
originalMethod.apply(null, args)
}
})

var proto = constructor.prototype
if (proto !== undefined) {
Object.keys(proto).forEach(function (methodName) {
var propDesc = Object.getOwnPropertyDescriptor(proto, methodName)
if (!propDesc.configurable || ('get' in propDesc) || ('set' in propDesc) ||
!is.isFunction(proto[methodName])) {
return
}

var originalMethod = proto[methodName]
proto[methodName] = function () {
var args = Array.prototype.slice.call(arguments)
console.log('Called: ' + methodName + '(' + args + ')')
originalMethod.apply(this, args)
}
})
}
}
44 changes: 44 additions & 0 deletions package.json
@@ -0,0 +1,44 @@
{
"name": "call-log",
"version": "0.1.1",
"description": "Parse a magnet URI and return an object of keys/values",
"main": "index.js",
"dependencies": {
"core-util-is": "*"
},
"devDependencies": {
"tape": "*"
},
"scripts": {
"test": "tape test/*.js"
},
"testling": {
"files": "test/*.js",
"browsers": [
"ie10",
"ff20",
"chrome20",
"safari6"
]
},
"repository": {
"type": "git",
"url": "git://github.com/feross/call-log.git"
},
"keywords": [
"magnet",
"uri",
"urn",
"p2p",
"peer-to-peer",
"cryptolinks",
"bittorrent",
"webtorrent"
],
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/feross/call-log/issues"
},
"homepage": "https://github.com/feross/call-log"
}
55 changes: 55 additions & 0 deletions test/basic.js
@@ -0,0 +1,55 @@
var callLog = require('../')
var test = require('tape')

function hookStdout (callback) {
var oldWrite = process.stdout.write

process.stdout.write = (function(write) {
return function (string, encoding, fd) {
write.apply(process.stdout, arguments)
callback(string, encoding, fd)
process.stdout.write = oldWrite
}
})(process.stdout.write)

}

function Cat () {}
Cat.prototype.meow = function (sound) { sound }
Cat.prototype.scratch = function () {}

Cat.static1 = function () {}
Cat.static2 = function (cb) { cb() }

callLog(Cat)

test('instrumentation works', function (t) {
t.plan(6)
var cat = new Cat()

hookStdout(function (string) {
t.equal(string, 'Called: meow()\n')
})
cat.meow()

hookStdout(function (string) {
t.equal(string, 'Called: meow(MEOAAAAWWW!)\n')
})
cat.meow('MEOAAAAWWW!')

hookStdout(function (string) {
t.equal(string, 'Called: scratch()\n')
})
cat.scratch()

hookStdout(function (string) {
t.equal(string, 'Called: static1()\n')
})
Cat.static1()

hookStdout(function (string) {
t.equal(string, 'Called: static2(function () { t.ok(true) })\n')
})
Cat.static2(function () { t.ok(true) })

})

0 comments on commit 008e116

Please sign in to comment.