createInnerCallback.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. module.exports = function createInnerCallback(callback, options, message, messageOptional) {
  6. var log = options.log;
  7. if(!log) {
  8. if(options.stack !== callback.stack) {
  9. var callbackWrapper = function callbackWrapper() {
  10. return callback.apply(this, arguments);
  11. };
  12. callbackWrapper.stack = options.stack;
  13. callbackWrapper.missing = options.missing;
  14. return callbackWrapper;
  15. }
  16. return callback;
  17. }
  18. function loggingCallbackWrapper() {
  19. var i;
  20. if(message) {
  21. if(!messageOptional || theLog.length > 0) {
  22. log(message);
  23. for(i = 0; i < theLog.length; i++)
  24. log(" " + theLog[i]);
  25. }
  26. } else {
  27. for(i = 0; i < theLog.length; i++)
  28. log(theLog[i]);
  29. }
  30. return callback.apply(this, arguments);
  31. }
  32. var theLog = [];
  33. loggingCallbackWrapper.log = function writeLog(msg) {
  34. theLog.push(msg);
  35. };
  36. loggingCallbackWrapper.stack = options.stack;
  37. loggingCallbackWrapper.missing = options.missing;
  38. return loggingCallbackWrapper;
  39. };