Created
February 16, 2019 08:11
-
-
Save Boorj/f364671c9adc4ad93b9b2f1d868b5b0b to your computer and use it in GitHub Desktop.
debug_log - utility for printing to debug output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* debug_log | |
* @param {*} $data | |
* @param {string} $d = "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "bold" | "dim" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough" | |
* */ | |
function debug_log($data, $type = ''){ | |
$patterns = [ | |
"black" => "[30mstr[39m", | |
"red" => "[31mstr[39m", | |
"green" => "[32mstr[39m", | |
"yellow" => "[33mstr[39m", | |
"blue" => "[34mstr[39m", | |
"magenta" => "[35mstr[39m", | |
"cyan" => "[36mstr[39m", | |
"white" => "[37mstr[39m", | |
"gray" => "[90mstr[39m", | |
"bold" => "[1mstr[22m", | |
"dim" => "[2mstr[22m", | |
"italic" => "[3mstr[23m", | |
"underline" => "[4mstr[24m", | |
"inverse" => "[7mstr[27m", | |
"hidden" => "[8mstr[28m", | |
"strikethrough" => "[9mstr[29m", | |
]; | |
if (!is_string($data) && !is_numeric($data)) { | |
$data = json_encode($data, JSON_PRETTY_PRINT); | |
} | |
if ($type && isset($patterns[$type])) { | |
$pattern = $patterns[$type]; | |
$data = str_replace('str', $data, $pattern); | |
} | |
file_put_contents('php://stderr', $data); | |
file_put_contents('php://stderr', "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment