debug.lib.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the debug library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. * @package chamilo.debug
  7. */
  8. /**
  9. * This is the debug library for Chamilo.
  10. * Include/require it in your code to use its functionality.
  11. * @package chamilo.debug
  12. */
  13. class Debug {
  14. /**
  15. * This function displays the contend of a variable, array or object in a nicely formatted way.
  16. * @param Mixed A variable, array or object
  17. * @return void Prints \<pre\> HTML block to output
  18. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  19. * @version November 2006
  20. */
  21. public function printr($variable) {
  22. echo '<pre>';
  23. print_r($variable);
  24. echo '</pre>';
  25. }
  26. /**
  27. * This function displays all the information of the dokeos $_course array
  28. * This array stores all the information of the current course if the user is in a course.
  29. * This is why this array is used to check weither the user is currently is in the course.
  30. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  31. * @version November 2006
  32. */
  33. public function course() {
  34. global $_course;
  35. self::printr($_course);
  36. }
  37. /**
  38. * This function displays all the information of the dokeos $_user array
  39. * This array stores all the information of the current user.
  40. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  41. * @version November 2006
  42. */
  43. public function user() {
  44. global $_user;
  45. self::printr($_user);
  46. }
  47. /**
  48. * This function displays an overview of the different path constants that can be used with the api_get_path function
  49. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  50. * @version November 2006
  51. * @return void
  52. */
  53. public function debug_paths() {
  54. echo 'WEB_PATH :'.api_get_path(WEB_PATH).'<br />';
  55. echo 'SYS_PATH :'.api_get_path(SYS_PATH).'<br />';
  56. echo 'REL_PATH :'.api_get_path(REL_PATH).'<br />';
  57. echo 'WEB_COURSE_PATH :'.api_get_path(WEB_COURSE_PATH).'<br />';
  58. echo 'SYS_COURSE_PATH :'.api_get_path(SYS_COURSE_PATH).'<br />';
  59. echo 'REL_COURSE_PATH :'.api_get_path(REL_COURSE_PATH).'<br />';
  60. echo 'REL_CLARO_PATH :'.api_get_path(REL_CODE_PATH).'<br />';
  61. echo 'WEB_CODE_PATH :'.api_get_path(WEB_CODE_PATH).'<br />';
  62. echo 'SYS_CODE_PATH :'.api_get_path(SYS_CODE_PATH).'<br />';
  63. echo 'SYS_LANG_PATH :'.api_get_path(SYS_LANG_PATH).'<br />';
  64. echo 'WEB_IMG_PATH :'.api_get_path(WEB_IMG_PATH).'<br />';
  65. echo 'PLUGIN_PATH :'.api_get_path(PLUGIN_PATH).'<br />';
  66. echo 'SYS_ARCHIVE_PATH :'.api_get_path(SYS_ARCHIVE_PATH).'<br />';
  67. echo 'INCLUDE_PATH :'.api_get_path(INCLUDE_PATH).'<br />';
  68. echo 'LIBRARY_PATH :'.api_get_path(LIBRARY_PATH).'<br />';
  69. echo 'CONFIGURATION_PATH :'.api_get_path(CONFIGURATION_PATH).'<br />';
  70. }
  71. /**
  72. * Dump variable contents on screen in a nice format
  73. * @param mixed Variable to dump
  74. * @param string Variable name to print
  75. * @return void
  76. */
  77. public function print_var($var, $varName = "@") {
  78. GLOBAL $DEBUG;
  79. if ($DEBUG)
  80. {
  81. echo "<blockquote>\n";
  82. echo "<b>[$varName]</b>";
  83. echo "<hr noshade size=\"1\" style=\"color:blue\">";
  84. echo "<pre style=\"color:red\">\n";
  85. var_dump($var);
  86. echo "</pre>\n";
  87. echo "<hr noshade size=\"1\" style=\"color:blue\">";
  88. echo "</blockquote>\n";
  89. }
  90. else
  91. {
  92. echo "<!-- DEBUG is OFF -->";
  93. echo "DEBUG is OFF";
  94. }
  95. }
  96. /**
  97. * Log the given string into the default log if mode confirms it
  98. * @param string String to be logged
  99. * @param bool Whether to force the log even in production mode or not
  100. * @return bool True on success, false on failure
  101. */
  102. public function log_s($msg, $force_log = false) {
  103. $server_type = api_get_setting('server_type');
  104. if ($server_type == 'production' && !$force_log) {
  105. //not logging in production mode
  106. return false;
  107. }
  108. $backtrace = debug_backtrace(); // Retrieving information about the caller statement.
  109. $backtrace_string = self::_get_backtrace_raw_string($backtrace);
  110. return error_log($msg.$backtrace_string);
  111. }
  112. /**
  113. * Log the given variables' dump into the default log if mode confirms it
  114. * @param string String to be logged
  115. * @param bool Whether to force the log even in production mode or not
  116. * @return bool True on success, false on failure
  117. */
  118. public function log_v($variable, $force_log = false) {
  119. $server_type = api_get_setting('server_type');
  120. if ($server_type == 'production' && !$force_log) {
  121. //not logging in production mode
  122. return null;
  123. }
  124. $backtrace = debug_backtrace(); // Retrieving information about the caller statement.
  125. $backtrace_string = self::_get_backtrace_raw_string($backtrace);
  126. return error_log(print_r($variable,1).$backtrace_string);
  127. }
  128. /**
  129. * Get a string formatted with all backtrace info
  130. * @param array Backtrace data
  131. * @return string Backtrace formatted string
  132. */
  133. private function _get_backtrace_raw_string($backtrace=array()) {
  134. $file = $line = $type = $function = $class = '';
  135. if (isset($backtrace[0])) {
  136. $caller = & $backtrace[0];
  137. } else {
  138. $caller = array();
  139. }
  140. if (isset($backtrace[1])) {
  141. $owner = & $backtrace[1];
  142. } else {
  143. $owner = array();
  144. }
  145. $file = $caller['file'];
  146. $line = $caller['line'];
  147. $type = $owner['type'];
  148. $function = $owner['function'];
  149. $class = $owner['class'];
  150. $info = ' CHAMILO LOG INFO :: FILE: ' . (empty($file) ? ' unknown ' : $file) . ' LINE: ' . (empty($line) ? ' unknown ' : $line).' ' ;
  151. if (empty($type)) {
  152. if (!empty($function)) {
  153. $info .= 'FUNCTION: ' . $function;
  154. }
  155. } else {
  156. if (!empty($class) && !empty($function)) {
  157. $info .= 'CLASS: ' . $class;
  158. $info .= 'METHOD: ' . $function;
  159. }
  160. }
  161. return $info;
  162. }
  163. }