├── .travis.yml ├── img.jpg ├── test ├── common.js ├── basic.js └── events.js ├── LICENSE ├── call-log.min.js ├── package.json ├── index.js └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | -------------------------------------------------------------------------------- /img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feross/call-log/HEAD/img.jpg -------------------------------------------------------------------------------- /test/common.js: -------------------------------------------------------------------------------- 1 | var fns = [] 2 | var log = console.log 3 | global.console.log = function () { 4 | var fn = fns.shift() 5 | if (fn) { 6 | fn.apply(null, arguments) 7 | } else { 8 | log.apply(console, arguments) 9 | } 10 | } 11 | 12 | exports.onNextLog = function (fn) { 13 | fns.push(fn) 14 | } 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Feross Aboukhadijeh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var callLog = require('../') 2 | var common = require('./common') 3 | var test = require('tape') 4 | 5 | function Cat () {} 6 | Cat.prototype.meow = function (sound) { return sound } 7 | Cat.prototype.scratch = function () {} 8 | 9 | Cat.static1 = function () {} 10 | Cat.static2 = function (cb) { cb() } 11 | 12 | Cat.prototype.ignored = 'should be ignored' 13 | Cat.ignored2 = 'should be ignored' 14 | 15 | callLog(Cat) 16 | 17 | test('basic logging works', function (t) { 18 | t.plan(9) 19 | var cat = new Cat() 20 | 21 | common.onNextLog(function (string) { 22 | t.equal(string, 'called meow') 23 | }) 24 | cat.meow() 25 | 26 | common.onNextLog(function (arg1, arg2) { 27 | t.equal(arg1, 'called meow') 28 | t.equal(arg2, 'MEOAAAAWWW!') 29 | }) 30 | var output = cat.meow('MEOAAAAWWW!') 31 | t.equal(output, 'MEOAAAAWWW!') 32 | 33 | common.onNextLog(function (string) { 34 | t.equal(string, 'called scratch') 35 | }) 36 | cat.scratch() 37 | 38 | common.onNextLog(function (string) { 39 | t.equal(string, 'called static1') 40 | }) 41 | Cat.static1() 42 | 43 | common.onNextLog(function (arg1, arg2) { 44 | t.equal(arg1, 'called static2') 45 | t.equal(arg2.toString(), 'function () { t.pass(\'cb called\') }') 46 | }) 47 | Cat.static2(function () { t.pass('cb called') }) 48 | }) 49 | -------------------------------------------------------------------------------- /call-log.min.js: -------------------------------------------------------------------------------- 1 | (function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var n;if(typeof window!=="undefined"){n=window}else if(typeof global!=="undefined"){n=global}else if(typeof self!=="undefined"){n=self}else{n=this}n.CallLog=e()}})(function(){var e,n,o;return function(){function p(i,f,u){function l(o,e){if(!f[o]){if(!i[o]){var n="function"==typeof require&&require;if(!e&&n)return n(o,!0);if(c)return c(o,!0);var r=new Error("Cannot find module '"+o+"'");throw r.code="MODULE_NOT_FOUND",r}var t=f[o]={exports:{}};i[o][0].call(t.exports,function(e){var n=i[o][1][e];return l(n||e)},t,t.exports,p,i,f,u)}return f[o].exports}for(var c="function"==typeof require&&require,e=0;e call-log.min.js" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/feross/call-log.git" 21 | }, 22 | "keywords": [ 23 | "call log", 24 | "instrumentation", 25 | "print calls", 26 | "print function calls", 27 | "function calls" 28 | ], 29 | "author": { 30 | "name": "Feross Aboukhadijeh", 31 | "email": "feross@feross.org", 32 | "url": "https://feross.org" 33 | }, 34 | "license": "MIT", 35 | "bugs": { 36 | "url": "https://github.com/feross/call-log/issues" 37 | }, 38 | "homepage": "https://github.com/feross/call-log", 39 | "funding": [ 40 | { 41 | "type": "github", 42 | "url": "https://github.com/sponsors/feross" 43 | }, 44 | { 45 | "type": "patreon", 46 | "url": "https://www.patreon.com/feross" 47 | }, 48 | { 49 | "type": "consulting", 50 | "url": "https://feross.org/support" 51 | } 52 | ] 53 | } 54 | -------------------------------------------------------------------------------- /test/events.js: -------------------------------------------------------------------------------- 1 | var callLog = require('../') 2 | var common = require('./common') 3 | var EventEmitter = require('events').EventEmitter 4 | var inherits = require('inherits') 5 | var test = require('tape') 6 | 7 | inherits(Dog, EventEmitter) 8 | 9 | function Dog () { 10 | EventEmitter.call(this) 11 | } 12 | 13 | Dog.prototype.woof = function () { 14 | this.emit('woof') 15 | } 16 | 17 | Dog.prototype.wag = function (speed) { 18 | this.emit('wag', speed) 19 | } 20 | 21 | callLog(Dog) 22 | 23 | test('eventemitter logging works', function (t) { 24 | var asserts = [] 25 | 26 | var dog = new Dog() 27 | 28 | common.onNextLog(function (arg1) { 29 | asserts.push(function () { t.equal(arg1, 'called woof') }) 30 | }) 31 | common.onNextLog(function (arg1, arg2) { 32 | asserts.push(function () { t.equal(arg1, 'called emit') }) 33 | asserts.push(function () { t.equal(arg2, 'woof') }) 34 | }) 35 | dog.woof() 36 | 37 | common.onNextLog(function (arg1, arg2) { 38 | asserts.push(function () { t.equal(arg1, 'called wag') }) 39 | asserts.push(function () { t.equal(arg2, 'fast') }) 40 | }) 41 | common.onNextLog(function (arg1, arg2, arg3) { 42 | asserts.push(function () { t.equal(arg1, 'called emit') }) 43 | asserts.push(function () { t.equal(arg2, 'wag') }) 44 | asserts.push(function () { t.equal(arg3, 'fast') }) 45 | }) 46 | dog.wag('fast') 47 | 48 | t.plan(asserts.length) 49 | asserts.forEach(function (assert) { 50 | assert() 51 | }) 52 | }) 53 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! call-log. MIT License. Feross Aboukhadijeh */ 2 | /** 3 | * Instrument an object or class so that anytime a method is invoked, it gets 4 | * logged to the console. 5 | * 6 | * @param {function} constructor 7 | */ 8 | module.exports = function (constructor) { 9 | Object.keys(constructor).forEach(function (methodName) { 10 | if (typeof constructor[methodName] !== 'function') { 11 | return 12 | } 13 | 14 | var originalMethod = constructor[methodName] 15 | constructor[methodName] = function () { 16 | var args = Array.prototype.slice.call(arguments) 17 | args.unshift('called ' + methodName) 18 | console.log.apply(console, args) 19 | return originalMethod.apply(this, arguments) 20 | } 21 | }) 22 | 23 | var proto = constructor.prototype 24 | if (proto !== undefined) { 25 | for (var methodName in proto) { 26 | var propDesc = Object.getOwnPropertyDescriptor(proto, methodName) 27 | if (typeof proto[methodName] !== 'function' || (propDesc && 28 | (!propDesc.configurable || ('get' in propDesc) || ('set' in propDesc)))) { 29 | continue 30 | } 31 | 32 | ;(function (methodName) { 33 | var originalMethod = proto[methodName] 34 | proto[methodName] = function () { 35 | var args = Array.prototype.slice.call(arguments) 36 | args.unshift('called ' + methodName) 37 | console.log.apply(console, args) 38 | return originalMethod.apply(this, arguments) 39 | } 40 | })(methodName) 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # call-log [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] 2 | 3 | [travis-image]: https://img.shields.io/travis/feross/call-log/master.svg 4 | [travis-url]: https://travis-ci.org/feross/call-log 5 | [npm-image]: https://img.shields.io/npm/v/call-log.svg 6 | [npm-url]: https://npmjs.org/package/call-log 7 | [downloads-image]: https://img.shields.io/npm/dm/call-log.svg 8 | [downloads-url]: https://npmjs.org/package/call-log 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg 10 | [standard-url]: https://standardjs.com 11 | 12 | #### Instrument a JavaScript class (or object) so that anytime a method function is called it gets logged to the console. 13 | 14 | ![cat](https://raw.githubusercontent.com/feross/call-log/master/img.jpg) 15 | 16 | ## install 17 | 18 | ``` 19 | npm install call-log 20 | ``` 21 | 22 | This module works in the browser with [browserify](http://browserify.org/). 23 | 24 | **Note:** If you're **NOT** using browserify, then use the included standalone file 25 | `call-log.min.js`. This exports a `CallLog` constructor on `window`. 26 | 27 | ## usage 28 | 29 | ```js 30 | var callLog = require('call-log') 31 | 32 | function Cat () {} 33 | Cat.prototype.meow = function (sound) { return sound } 34 | 35 | // Add instrumentation to Cat 36 | callLog(Cat) 37 | 38 | // Use the cat 39 | var cat = new Cat() 40 | cat.meow() 41 | cat.meow('MEOAAAAWWW!') 42 | 43 | // Prints: 44 | // "called meow" 45 | // "called meow", "MEOAAAAWWW!" 46 | 47 | ``` 48 | 49 | ## license 50 | 51 | MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org) 52 | 53 | --------------------------------------------------------------------------------