<?php
namespace Bitrix\Main\Diag;

class Debug
{
	public static function Dump($var, $varName = "", $return = false)
	{
		if ($return)
			ob_start();

		$flComplex = (is_array($var) || is_object($var));

		if ($varName != "")
		{
			echo $varName;

			if ($flComplex)
				echo ":".($return ? "\n" : "<br />");
			else
				echo "=";
		}

		if ($flComplex && !$return)
			echo "<pre>";

		var_dump($var);

		if ($flComplex && !$return)
			echo "</pre>";
		echo ($return ? "\n" : "<br />");

		if ($return)
			return ob_get_clean();

		return null;
	}

	public static function DumpToFile($var, $varName = "", $fileName = "")
	{
		if (empty($fileName))
			$fileName = "__bx_log.log";

		$data = self::Dump($var, $varName, true);

		$tempFile = fopen($_SERVER["DOCUMENT_ROOT"]."/".$fileName, "a");
		fwrite($tempFile, $data."\n");
		fclose($tempFile);
	}
}
