tracer.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.aspect.tracer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.aspect.tracer"] = true;
  8. dojo.provide("dojox.lang.aspect.tracer");
  9. (function(){
  10. var aop = dojox.lang.aspect;
  11. var Tracer = function(/*Boolean*/ grouping){
  12. this.method = grouping ? "group" : "log";
  13. if(grouping){
  14. this.after = this._after;
  15. }
  16. };
  17. dojo.extend(Tracer, {
  18. before: function(/*arguments*/){
  19. var context = aop.getContext(), joinPoint = context.joinPoint,
  20. args = Array.prototype.join.call(arguments, ", ");
  21. console[this.method](context.instance, "=>", joinPoint.targetName + "(" + args + ")");
  22. },
  23. afterReturning: function(retVal){
  24. var joinPoint = aop.getContext().joinPoint;
  25. if(typeof retVal != "undefined"){
  26. console.log(joinPoint.targetName + "() returns:", retVal);
  27. }else{
  28. console.log(joinPoint.targetName + "() returns");
  29. }
  30. },
  31. afterThrowing: function(excp){
  32. console.log(aop.getContext().joinPoint.targetName + "() throws:", excp);
  33. },
  34. _after: function(excp){
  35. console.groupEnd();
  36. }
  37. });
  38. aop.tracer = function(/*Boolean*/ grouping){
  39. // summary:
  40. // Returns an object, which can be used to trace calls with Firebug's console.
  41. // Prints argument, a return value, or an exception.
  42. //
  43. // grouping:
  44. // The flag to group output. If true, indents embedded console messages.
  45. return new Tracer(grouping); // Object
  46. };
  47. })();
  48. }