tripal.notice.api.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for improved user
  5. * notifications. These API functions can be used to set messages for
  6. * end-users, administrators, or simple logging.
  7. */
  8. /**
  9. * @defgroup tripal_notify_api Notify
  10. * @ingroup tripal_api
  11. * @{
  12. * Provides an application programming interface (API) for improved user
  13. * notivications. These API functions can be used to set messages for
  14. * end-users, administrators, or simple logging.
  15. *
  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. * - drupal_set_message: set to TRUE to call drupal_set_message with the
  57. * same error.
  58. *
  59. * @ingroup tripal_notify_api
  60. */
  61. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  62. $suppress = getenv('TRIPAL_SUPPRESS_ERRORS');
  63. if(strtolower($suppress) === 'true') return;
  64. // Get human-readable severity string
  65. $severity_string = '';
  66. switch ($severity) {
  67. case TRIPAL_CRITICAL:
  68. $severity_string = 'CRITICAL';
  69. break;
  70. case TRIPAL_ERROR:
  71. $severity_string = 'ERROR';
  72. break;
  73. case TRIPAL_WARNING:
  74. $severity_string = 'WARNING';
  75. break;
  76. case TRIPAL_NOTICE:
  77. $severity_string = 'NOTICE';
  78. break;
  79. case TRIPAL_INFO:
  80. $severity_string = 'INFO';
  81. break;
  82. case TRIPAL_DEBUG:
  83. $severity_string = 'DEBUG';
  84. break;
  85. }
  86. // If we are not set to return debugging information and the severity string
  87. // is debug then don't report the error.
  88. if (($severity == TRIPAL_DEBUG) AND (getenv('TRIPAL_DEBUG') != 1)) {
  89. return FALSE;
  90. }
  91. // Get the backtrace and include in the error message, but only if the
  92. // TRIPAL_DEBUG environment variable is set.
  93. if (getenv('TRIPAL_DEBUG') == 1) {
  94. $backtrace = debug_backtrace();
  95. $message .= "\nBacktrace:\n";
  96. $i = 1;
  97. for ($i = 1; $i < count($backtrace); $i++) {
  98. $function = $backtrace[$i];
  99. $message .= " $i) " . $function['function'] . "\n";
  100. }
  101. }
  102. // Send to watchdog.
  103. try {
  104. watchdog($type, $message, $variables, $severity);
  105. }
  106. catch (Exception $e) {
  107. print "CRITICAL (TRIPAL): Unable to add error message with watchdog: " . $e->getMessage(). "\n.";
  108. $options['print'] = TRUE;
  109. }
  110. // Format the message for printing (either to the screen, log or both).
  111. if (sizeof($variables) > 0) {
  112. $print_message = str_replace(array_keys($variables), $variables, $message);
  113. }
  114. else {
  115. $print_message = $message;
  116. }
  117. // If print option supplied then print directly to the screen.
  118. if (isset($options['print'])) {
  119. print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
  120. }
  121. if (isset($options['drupal_set_message'])) {
  122. if (in_array($severity, [TRIPAL_CRITICAL, TRIPAL_ERROR])) {
  123. $status = 'error';
  124. }
  125. elseif ($severity == TRIPAL_WARNING) {
  126. $status = 'warning';
  127. }
  128. else {
  129. $status = 'status';
  130. }
  131. drupal_set_message($print_message, $status);
  132. }
  133. // Print to the Tripal error log but only if the severity is not info.
  134. if (($severity != TRIPAL_INFO)) {
  135. tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
  136. }
  137. }
  138. /**
  139. * Display messages to tripal administrators.
  140. *
  141. * This can be used instead of drupal_set_message when you want to target
  142. * tripal administrators.
  143. *
  144. * @param $message
  145. * The message to be displayed to the tripal administrators.
  146. * @param $importance
  147. * The level of importance for this message. In the future this will be used
  148. * to allow administrators to filter some of these messages. It can be one of
  149. * the following:
  150. * - TRIPAL_CRITICAL: Critical conditions.
  151. * - TRIPAL_ERROR: Error conditions.
  152. * - TRIPAL_WARNING: Warning conditions.
  153. * - TRIPAL_NOTICE: Normal but significant conditions.
  154. * - TRIPAL_INFO: (default) Informational messages.
  155. * - TRIPAL_DEBUG: Debug-level messages.
  156. * @param $options
  157. * Any options to apply to the current message. Supported options include:
  158. * - return_html: return HTML instead of setting a drupal message. This can
  159. * be used to place a tripal message in a particular place in the page.
  160. * The default is FALSE.
  161. *
  162. * @ingroup tripal_notify_api
  163. */
  164. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  165. global $user;
  166. // Only show the message to the users with 'view dev helps' permission.
  167. if (!user_access('view dev helps')) {
  168. return '';
  169. }
  170. // Set defaults.
  171. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  172. // Get human-readable severity string.
  173. $importance_string = '';
  174. switch ($importance) {
  175. case TRIPAL_CRITICAL:
  176. $importance_string = 'CRITICAL';
  177. break;
  178. case TRIPAL_ERROR:
  179. $importance_string = 'ERROR';
  180. break;
  181. case TRIPAL_WARNING:
  182. $importance_string = 'WARNING';
  183. break;
  184. case TRIPAL_NOTICE:
  185. $importance_string = 'NOTICE';
  186. break;
  187. case TRIPAL_INFO:
  188. $importance_string = 'INFO';
  189. break;
  190. case TRIPAL_DEBUG:
  191. $importance_string = 'DEBUG';
  192. break;
  193. }
  194. // Mark-up the Message.
  195. $full_message =
  196. '<div class="tripal-site-admin-message">' .
  197. '<span class="tripal-severity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>' .
  198. $message .
  199. '</div>';
  200. // Handle whether to return the HTML & let the caller deal with it
  201. // or to use drupal_set_message to put it near the top of the page & let the
  202. // theme deal with it.
  203. if ($options['return_html']) {
  204. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  205. }
  206. else {
  207. drupal_set_message($full_message, 'tripal-site-admin-only');
  208. }
  209. }
  210. /**
  211. * File-based logging for Tripal.
  212. *
  213. * @param $message
  214. * The message to be logged. Need not contain date/time information.
  215. * @param $log_type
  216. * The type of log. Should be one of 'error' or 'job' although other types
  217. * are supported.
  218. * @param $options
  219. * An array of options where the following keys are supported:
  220. * - first_progress_bar: this sohuld be used for the first log call for a
  221. * progress bar.
  222. * - is_progress_bar: this option should be used for all but the first print
  223. * of a progress bar to allow it all to be printed on the same line
  224. * without intervening date prefixes.
  225. * @return
  226. * The number of bytes that were written to the file, or FALSE on failure.
  227. *
  228. * @ingroup tripal_notify_api
  229. */
  230. function tripal_log($message, $type = 'error', $options = array()) {
  231. global $base_url;
  232. $prefix = '[site ' . $base_url . '] [TRIPAL ' . strtoupper(check_plain($type)) . '] ';
  233. if (!isset($options['is_progress_bar'])) {
  234. $message = $prefix . str_replace("\n", "", trim($message));
  235. }
  236. if (isset($options['first_progress_bar'])) {
  237. $message = $prefix . trim($message);
  238. }
  239. return error_log($message);
  240. }