tripal.notice.api.inc 7.4 KB

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