debug.lib.php 5.4 KB

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