Advertisement
Drakim

Haxe debug functions

Dec 24th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.26 KB | None | 0 0
  1. import haxe.macro.Expr;
  2. import haxe.macro.Context;
  3.  
  4. class Debug {
  5.   /**
  6.   * When debugMode is false, the functions below will do absolutely nothing, so you can safely use them without any performance cost in the release
  7.   */
  8.   static public var debugMode(default,never):Bool = #if(debug) true #else false #end ;
  9.   /**
  10.   * Simple error throwing function that also appends the current context so you know where it's called from.
  11.   * Example: Debug.error('Sound Engine is not initialized yet, cannot play sound.');
  12.   */
  13.   @:macro public static function error(_text:Expr):Expr {
  14.     if(Debug.debugMode) {
  15.       var errorMessage:ExprOf<String> = Context.makeExpr(_text + '\nfrom ' + Context.getLocalClass() + '.' + Context.getLocalMethod(),Context.currentPos());
  16.       return macro trace($errorMessage);
  17.     } else {
  18.       return macro Void;
  19.     }
  20.   }
  21.   /**
  22.   * Simple assert function that allows you to check various conditions, and throw an error message if conditions are not met.
  23.   * Example: Debug.assert(x < image.width,'Pixel x value is outside image bounds, cannot set pixel');
  24.   */
  25.   @:macro public static function assert(_condition:Expr,_text:Expr):Expr {
  26.     if(Debug.debugMode) {
  27.       var errorMessage:ExprOf<String> = Context.makeExpr(_text + '\nfrom ' + Context.getLocalClass() + '.' + Context.getLocalMethod(),Context.currentPos());
  28.       return macro if(!$_condition) {trace($errorMessage);}
  29.     } else {
  30.       return macro Void;
  31.     }
  32.   }
  33.   /**
  34.   * Simple trace function. Unlike the regular trace function this one dissapears if debugMode is false.
  35.   * Example: Debug.trace('Level loaded successfully');
  36.   */
  37.   @:macro public static function trace(_text:ExprOf<String>):Expr {
  38.     if(Debug.debugMode) {
  39.       return macro trace($_text);
  40.     } else {
  41.       return macro Void;
  42.     }
  43.   }
  44.   /**
  45.   * Simple trace clearing function. Unlike the regular trace clear function this one dissapears if debugMode is false.
  46.   * Example: Debug.traceClear();
  47.   */
  48.   @:macro public static function traceClear():Expr {
  49.     if(Debug.debugMode) {
  50.       return macro haxe.Log.clear();
  51.     } else {
  52.       return macro Void;
  53.     }
  54.   }
  55.   /**
  56.   * This function runs code only in debugMode. Unlike doing it manually by checking if(debugMode==true) your code is not included in non-debugMode
  57.   * Example: Debug.ifDebug(function(){
  58.   *   Core.name = 'Debug build, do not distribute!';
  59.   *   activateDebugRecording();
  60.   * })();
  61.   */
  62.   @:macro public static function ifDebug(_code:Expr):Expr {
  63.     if(Debug.debugMode) {
  64.       return _code;
  65.     } else {
  66.       return macro Void;
  67.     }
  68.   }
  69.   /**
  70.   * In combination with the next routine allows you to meassure how many miliseconds that has passed
  71.   */
  72.   @:macro public static function timerStart():Expr {
  73.     if(Debug.debugMode) {
  74.       return macro Debug.timer=Core.getTime();
  75.     } else {
  76.       return macro Void;
  77.     }
  78.   }
  79.   /**
  80.   * In combination with the previous routine allows you to meassure how many miliseconds that has passed
  81.   */
  82.   @:macro public static function timerEnd():Expr {
  83.     if(Debug.debugMode) {
  84.       return macro Debug.timer=Core.getTime()-Debug.timer;
  85.     } else {
  86.       return macro Void;
  87.     }
  88.   }
  89.   /**
  90.   * Read this value after using Debug.timerEnd();
  91.   */
  92.   public static var timer:Int;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement