tripal.notice.api.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for Tripal
  5. *
  6. * The Tripal API currently provides generic functionality for all Tripal
  7. * extension modules.
  8. */
  9. /**
  10. * @defgroup tripal_api Tripal API
  11. * @{
  12. * Provides an application programming interface (API) for Tripal
  13. *
  14. * The Tripal API currently provides generic functionality for all Tripal
  15. * extension modules..
  16. * @}
  17. */
  18. // Globals used by Tripals Error catching functions
  19. // Should match those defined by watchdog
  20. define('TRIPAL_CRITICAL',2);
  21. define('TRIPAL_ERROR',3);
  22. define('TRIPAL_WARNING',4);
  23. define('TRIPAL_NOTICE',5);
  24. define('TRIPAL_INFO',6);
  25. define('TRIPAL_DEBUG',7);
  26. /**
  27. * Provide better error notice for Tripal.
  28. *
  29. * If the environment variable 'TRIPAL_DEBUG' is set to 1 then this function
  30. * will add backtrace information to the message.
  31. *
  32. * @param $type
  33. * The catagory to which this message belongs. Can be any string, but the
  34. * general practice is to use the name of the module.
  35. * @param $severity
  36. * The severity of the message; one of the following values:
  37. * - TRIPAL_CRITICAL: Critical conditions.
  38. * - TRIPAL_ERROR: Error conditions.
  39. * - TRIPAL_WARNING: Warning conditions.
  40. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  41. * - TRIPAL_INFO: Informational messages.
  42. * - TRIPAL_DEBUG: Debug-level messages.
  43. * @param $message
  44. * The message to store in the log. Keep $message translatable by not
  45. * concatenating dynamic values into it! Variables in the message should be
  46. * added by using placeholder strings alongside the variables argument to
  47. * declare the value of the placeholders. See t() for documentation on how
  48. * $message and $variables interact.
  49. * @param $variables
  50. * Array of variables to replace in the message on display or NULL if message
  51. * is already translated or not possible to translate.
  52. * @param $options
  53. * An array of options. Some available options include:
  54. * - print: prints the error message to the terminal screen. Useful when
  55. * display is the command-line
  56. *
  57. * @ingroup tripal_api
  58. */
  59. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  60. // Get human-readable severity string
  61. $severity_string = '';
  62. switch ($severity) {
  63. case TRIPAL_CRITICAL:
  64. $severity_string = 'CRITICAL';
  65. break;
  66. case TRIPAL_ERROR:
  67. $severity_string = 'ERROR';
  68. break;
  69. case TRIPAL_WARNING:
  70. $severity_string = 'WARNING';
  71. break;
  72. case TRIPAL_NOTICE:
  73. $severity_string = 'NOTICE';
  74. break;
  75. case TRIPAL_INFO:
  76. $severity_string = 'INFO';
  77. break;
  78. case TRIPAL_DEBUG:
  79. $severity_string = 'DEBUG';
  80. break;
  81. }
  82. // If we are not set to return debugging information and the severity string
  83. // is debug then don't report the error.
  84. if (($severity == TRIPAL_DEBUG) AND (getenv('TRIPAL_DEBUG') != 1)) {
  85. return FALSE;
  86. }
  87. // Get the backtrace and include in the error message, but only if the
  88. // TRIPAL_DEBUG environment variable is set.
  89. if (getenv('TRIPAL_DEBUG') == 1) {
  90. $backtrace = debug_backtrace();
  91. $message .= "\nBacktrace:\n";
  92. $i = 1;
  93. for ($i = 1; $i < count($backtrace); $i++) {
  94. $function = $backtrace[$i];
  95. $message .= " $i) " . $function['function'] . "\n";
  96. }
  97. }
  98. // Send to watchdog.
  99. try {
  100. watchdog($type, $message, $variables, $severity);
  101. }
  102. catch (Exception $e) {
  103. print "CRITICAL (TRIPAL): Unable to register error message with watchdog: " . $e->getMessage(). "\n.";
  104. $options['print'] = TRUE;
  105. }
  106. // Format the message for printing (either to the screen, log or both).
  107. if (sizeof($variables) > 0) {
  108. $print_message = str_replace(array_keys($variables), $variables, $message);
  109. }
  110. else {
  111. $print_message = $message;
  112. }
  113. // If print option supplied then print directly to the screen.
  114. if (isset($options['print'])) {
  115. print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
  116. }
  117. // Print to the Tripal error log but only if the severity is not info.
  118. if (($severity != TRIPAL_INFO)) {
  119. tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
  120. }
  121. }
  122. /**
  123. * Display messages to tripal administrators.
  124. *
  125. * This can be used instead of drupal_set_message when you want to target
  126. * tripal administrators.
  127. *
  128. * @param $message
  129. * The message to be displayed to the tripal administrators
  130. * @param $importance
  131. * The level of importance for this message. In the future this will be used
  132. * to allow administrators to filter some of these messages. It can be one of
  133. * the following:
  134. * - TRIPAL_CRITICAL: Critical conditions.
  135. * - TRIPAL_ERROR: Error conditions.
  136. * - TRIPAL_WARNING: Warning conditions.
  137. * - TRIPAL_NOTICE: Normal but significant conditions.
  138. * - TRIPAL_INFO: (default) Informational messages.
  139. * - TRIPAL_DEBUG: Debug-level messages.
  140. * @param $options
  141. * Any options to apply to the current message. Supported options include:
  142. * - return_html: return HTML instead of setting a drupal message. This can
  143. * be used to place a tripal message in a particular place in the page.
  144. * The default is FALSE.
  145. */
  146. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  147. global $user;
  148. // Only show the message to the users with 'view dev helps' permission.
  149. if (!user_access('view dev helps')) {
  150. return '';
  151. }
  152. // set defaults
  153. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  154. // Get human-readable severity string
  155. $importance_string = '';
  156. switch ($importance) {
  157. case TRIPAL_CRITICAL:
  158. $importance_string = 'CRITICAL';
  159. break;
  160. case TRIPAL_ERROR:
  161. $importance_string = 'ERROR';
  162. break;
  163. case TRIPAL_WARNING:
  164. $importance_string = 'WARNING';
  165. break;
  166. case TRIPAL_NOTICE:
  167. $importance_string = 'NOTICE';
  168. break;
  169. case TRIPAL_INFO:
  170. $importance_string = 'INFO';
  171. break;
  172. case TRIPAL_DEBUG:
  173. $importance_string = 'DEBUG';
  174. break;
  175. }
  176. // Mark-up the Message
  177. $full_message =
  178. '<div class="tripal-site-admin-message">' .
  179. '<span class="tripal-severity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>' .
  180. $message .
  181. '</div>';
  182. // Handle whether to return the HTML & let the caller deal with it
  183. // or to use drupal_set_message to put it near the top of the page & let the theme deal with it
  184. if ($options['return_html']) {
  185. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  186. }
  187. else {
  188. drupal_set_message($full_message, 'tripal-site-admin-only');
  189. }
  190. }
  191. /**
  192. * File-based Logging for Tripal
  193. *
  194. * @param $message
  195. * The message to be logged. Need not contain date/time information
  196. * @param $log_type
  197. * The type of log. Should be one of 'error' or 'job' although other types
  198. * are supported.
  199. * @param $options
  200. * An array of options where the following keys are supported:
  201. * - first_progress_bar: this sohuld be used for the first log call for a
  202. * progress bar
  203. * - is_progress_bar: this option should be used for all but the first print
  204. * of a progress bar to allow it all to be printed on the same line
  205. * without intervening date prefixes
  206. * @return
  207. * The number of bytes that were written to the file, or FALSE on failure
  208. */
  209. function tripal_log($message, $type = 'error', $options = array()) {
  210. global $base_url;
  211. $prefix = '[site ' . $base_url . '] [TRIPAL ' . strtoupper(check_plain($type)) . '] ';
  212. if (!isset($options['is_progress_bar'])) {
  213. $message = $prefix . str_replace("\n", "", trim($message));
  214. }
  215. if (isset($options['first_progress_bar'])) {
  216. $message = $prefix . trim($message);
  217. }
  218. return error_log($message);
  219. }