Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import haxe.macro.Expr;
- import haxe.macro.Context;
- class Debug {
- /**
- * When debugMode is false, the functions below will do absolutely nothing, so you can safely use them without any performance cost in the release
- */
- static public var debugMode(default,never):Bool = #if(debug) true #else false #end ;
- /**
- * Simple error throwing function that also appends the current context so you know where it's called from.
- * Example: Debug.error('Sound Engine is not initialized yet, cannot play sound.');
- */
- @:macro public static function error(_text:Expr):Expr {
- if(Debug.debugMode) {
- var errorMessage:ExprOf<String> = Context.makeExpr(_text + '\nfrom ' + Context.getLocalClass() + '.' + Context.getLocalMethod(),Context.currentPos());
- return macro trace($errorMessage);
- } else {
- return macro Void;
- }
- }
- /**
- * Simple assert function that allows you to check various conditions, and throw an error message if conditions are not met.
- * Example: Debug.assert(x < image.width,'Pixel x value is outside image bounds, cannot set pixel');
- */
- @:macro public static function assert(_condition:Expr,_text:Expr):Expr {
- if(Debug.debugMode) {
- var errorMessage:ExprOf<String> = Context.makeExpr(_text + '\nfrom ' + Context.getLocalClass() + '.' + Context.getLocalMethod(),Context.currentPos());
- return macro if(!$_condition) {trace($errorMessage);}
- } else {
- return macro Void;
- }
- }
- /**
- * Simple trace function. Unlike the regular trace function this one dissapears if debugMode is false.
- * Example: Debug.trace('Level loaded successfully');
- */
- @:macro public static function trace(_text:ExprOf<String>):Expr {
- if(Debug.debugMode) {
- return macro trace($_text);
- } else {
- return macro Void;
- }
- }
- /**
- * Simple trace clearing function. Unlike the regular trace clear function this one dissapears if debugMode is false.
- * Example: Debug.traceClear();
- */
- @:macro public static function traceClear():Expr {
- if(Debug.debugMode) {
- return macro haxe.Log.clear();
- } else {
- return macro Void;
- }
- }
- /**
- * This function runs code only in debugMode. Unlike doing it manually by checking if(debugMode==true) your code is not included in non-debugMode
- * Example: Debug.ifDebug(function(){
- * Core.name = 'Debug build, do not distribute!';
- * activateDebugRecording();
- * })();
- */
- @:macro public static function ifDebug(_code:Expr):Expr {
- if(Debug.debugMode) {
- return _code;
- } else {
- return macro Void;
- }
- }
- /**
- * In combination with the next routine allows you to meassure how many miliseconds that has passed
- */
- @:macro public static function timerStart():Expr {
- if(Debug.debugMode) {
- return macro Debug.timer=Core.getTime();
- } else {
- return macro Void;
- }
- }
- /**
- * In combination with the previous routine allows you to meassure how many miliseconds that has passed
- */
- @:macro public static function timerEnd():Expr {
- if(Debug.debugMode) {
- return macro Debug.timer=Core.getTime()-Debug.timer;
- } else {
- return macro Void;
- }
- }
- /**
- * Read this value after using Debug.timerEnd();
- */
- public static var timer:Int;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement