// Set Of Utilities Functions for using debug printing while developping scripts  		
//
// Author: LoopToGo
//
// To use: include the following in your script:
//		loadScript LoopToGo\libs\debugUtilities.ltgl
//
// Then, create as many tags as you want (and name it as you want)
//		debugTag Warning 1
//		debugTag Error 1
//		debugTag Info 1
//
// Then, use this statements to add debug prints in your script
// 		debug Warning "This is a warning in " $scriptFilename line $scriptLineNumber
// 		debug Error "This is an error in " $scriptFilename line $scriptLineNumber
// 		debug Info "This is an information message in " $scriptFilename line $scriptLineNumber
//
// To disable all the message with a specific tag (note: this is not optimal performance wise, see method below):
//		debugTag Warning 0
//
// To disable all debug prints (this is very effective with almost no performance impact) :
//		alias debug doNothing
//
// To have indented debug message, put this line at the beginning of each script and function:
//		debugIncreaseIndentation
//
// And this line at the end of each script and function
//		debugDecreaseIndentation
//


skipIfAlreadyLoaded // This command will make sure this script (library) is only loaded once when a child script try to load it after its parent script loaded it

// All alias interfaces.
alias debug callFunction debugFunction
alias debugTag callFunction setDebugState 
alias debugEnableTag globalVariable debugTagEnabled 1
alias debugDisableTag globalVariable debugTagEnabled 0
alias debugIncreaseIndentation	globalVariable debugIndentation $debugIndentation + 1
alias debugDecreaseIndentation	globalVariable debugIndentation $debugIndentation - 1

// Overwrite this variable in your code to change the indentation length
globalVariable debugIndentationString	"    "


// Internal code, not to use 

globalVariable debugIndentation	0


function debugFunction

	variable tag gDebug_ & $arg1
	
	variable message ""
	variable indent ""
	
	for i 1 $debugIndentation
		variable indent $indent & $debugIndentationString
	endFor

	
	for i 2 $nbArgs
		variable arg arg&$i
		variable message $message & $$arg & " "
				
	endFor
	
	
	if $$tag			
		if $debugTagEnabled
			print $indent & $arg1 & : $message
		else
			print $indent & $message
		endIf
	endIf

endFunction

function setDebugState
	
	if $nbArgs = 2
		variable tag gDebug_ & $arg1
		globalVariable $tag $arg2
	endIf
	
endFunction


