tripal.notice.api.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 Tripal's 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. * Please be sure to set the $options array as desired. For example, by default
  30. * this function sends all messages to the Drupal watchdog. If a long running
  31. * job uses this function and prints status messages you may not want to have
  32. * those go to the watchdog as it can dramatically affect performance.
  33. *
  34. * If the environment variable 'TRIPAL_DEBUG' is set to 1 then this function
  35. * will add backtrace information to the message.
  36. *
  37. * @param $type
  38. * The category to which this message belongs. Can be any string, but the
  39. * general practice is to use the name of the module.
  40. * @param $severity
  41. * The severity of the message; one of the following values:
  42. * - TRIPAL_CRITICAL: Critical conditions.
  43. * - TRIPAL_ERROR: Error conditions.
  44. * - TRIPAL_WARNING: Warning conditions.
  45. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  46. * - TRIPAL_INFO: Informational messages.
  47. * - TRIPAL_DEBUG: Debug-level messages.
  48. * @param $message
  49. * The message to store in the log. Keep $message translatable by not
  50. * concatenating dynamic values into it! Variables in the message should be
  51. * added by using placeholder strings alongside the variables argument to
  52. * declare the value of the placeholders. See t() for documentation on how
  53. * $message and $variables interact.
  54. * @param $variables
  55. * Array of variables to replace in the message on display or NULL if message
  56. * is already translated or not possible to translate.
  57. * @param $options
  58. * An array of options. Some available options include:
  59. * - print: prints the error message to the terminal screen. Useful when
  60. * display is the command-line
  61. * - drupal_set_message: set to TRUE then send the message to the
  62. * drupal_set_message function.
  63. * - watchdog: set to FALSE to disable logging to Drupal's watchdog.
  64. * - job: The jobs management object for the job if this function is run
  65. * as a job. Adding the job object here ensures that any status or error
  66. * messages are also logged with the job.
  67. *
  68. * @ingroup tripal_notify_api
  69. */
  70. function tripal_report_error($type, $severity, $message, $variables = [], $options = []) {
  71. $suppress = getenv('TRIPAL_SUPPRESS_ERRORS');
  72. if (strtolower($suppress) === 'true') {
  73. return;
  74. }
  75. // Get human-readable severity string
  76. $severity_string = '';
  77. switch ($severity) {
  78. case TRIPAL_CRITICAL:
  79. $severity_string = 'CRITICAL';
  80. break;
  81. case TRIPAL_ERROR:
  82. $severity_string = 'ERROR';
  83. break;
  84. case TRIPAL_WARNING:
  85. $severity_string = 'WARNING';
  86. break;
  87. case TRIPAL_NOTICE:
  88. $severity_string = 'NOTICE';
  89. break;
  90. case TRIPAL_INFO:
  91. $severity_string = 'INFO';
  92. break;
  93. case TRIPAL_DEBUG:
  94. $severity_string = 'DEBUG';
  95. break;
  96. }
  97. // If we are not set to return debugging information and the severity string
  98. // is debug then don't report the error.
  99. if (($severity == TRIPAL_DEBUG) AND (getenv('TRIPAL_DEBUG') != 1)) {
  100. return FALSE;
  101. }
  102. // Get the backtrace and include in the error message, but only if the
  103. // TRIPAL_DEBUG environment variable is set.
  104. if (getenv('TRIPAL_DEBUG') == 1) {
  105. $backtrace = debug_backtrace();
  106. $message .= "\nBacktrace:\n";
  107. $i = 1;
  108. for ($i = 1; $i < count($backtrace); $i++) {
  109. $function = $backtrace[$i];
  110. $message .= " $i) " . $function['function'] . "\n";
  111. }
  112. }
  113. // Send to watchdog if the user wants.
  114. if (array_key_exists('watchdog', $options) and $options['watchdog'] !== FALSE) {
  115. try {
  116. watchdog($type, $message, $variables, $severity);
  117. } catch (Exception $e) {
  118. print "CRITICAL (TRIPAL): Unable to add error message with watchdog: " . $e->getMessage() . "\n.";
  119. $options['print'] = TRUE;
  120. }
  121. }
  122. // Format the message for printing (either to the screen, log or both).
  123. if (sizeof($variables) > 0) {
  124. $print_message = str_replace(array_keys($variables), $variables, $message);
  125. }
  126. else {
  127. $print_message = $message;
  128. }
  129. // If print option supplied then print directly to the screen.
  130. if (isset($options['print'])) {
  131. print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
  132. }
  133. if (isset($options['drupal_set_message'])) {
  134. if (in_array($severity, [TRIPAL_CRITICAL, TRIPAL_ERROR])) {
  135. $status = 'error';
  136. }
  137. elseif ($severity == TRIPAL_WARNING) {
  138. $status = 'warning';
  139. }
  140. else {
  141. $status = 'status';
  142. }
  143. drupal_set_message($print_message, $status);
  144. }
  145. // Print to the Tripal error log but only if the severity is not info.
  146. if (($severity != TRIPAL_INFO)) {
  147. tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
  148. }
  149. if (array_key_exists('job', $options) and is_a($options['job'], 'TripalJob')) {
  150. $options['job']->logMessage($message, $variables, $severity);
  151. }
  152. }
  153. /**
  154. * Display messages to tripal administrators.
  155. *
  156. * This can be used instead of drupal_set_message when you want to target
  157. * tripal administrators.
  158. *
  159. * @param $message
  160. * The message to be displayed to the tripal administrators.
  161. * @param $importance
  162. * The level of importance for this message. In the future this will be used
  163. * to allow administrators to filter some of these messages. It can be one of
  164. * the following:
  165. * - TRIPAL_CRITICAL: Critical conditions.
  166. * - TRIPAL_ERROR: Error conditions.
  167. * - TRIPAL_WARNING: Warning conditions.
  168. * - TRIPAL_NOTICE: Normal but significant conditions.
  169. * - TRIPAL_INFO: (default) Informational messages.
  170. * - TRIPAL_DEBUG: Debug-level messages.
  171. * @param $options
  172. * Any options to apply to the current message. Supported options include:
  173. * - return_html: return HTML instead of setting a drupal message. This can
  174. * be used to place a tripal message in a particular place in the page.
  175. * The default is FALSE.
  176. *
  177. * @ingroup tripal_notify_api
  178. */
  179. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = []) {
  180. global $user;
  181. // Only show the message to the users with 'view dev helps' permission.
  182. if (!user_access('view dev helps')) {
  183. return '';
  184. }
  185. // Set defaults.
  186. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  187. // Get human-readable severity string.
  188. $importance_string = '';
  189. switch ($importance) {
  190. case TRIPAL_CRITICAL:
  191. $importance_string = 'CRITICAL';
  192. break;
  193. case TRIPAL_ERROR:
  194. $importance_string = 'ERROR';
  195. break;
  196. case TRIPAL_WARNING:
  197. $importance_string = 'WARNING';
  198. break;
  199. case TRIPAL_NOTICE:
  200. $importance_string = 'NOTICE';
  201. break;
  202. case TRIPAL_INFO:
  203. $importance_string = 'INFO';
  204. break;
  205. case TRIPAL_DEBUG:
  206. $importance_string = 'DEBUG';
  207. break;
  208. }
  209. // Mark-up the Message.
  210. $full_message =
  211. '<div class="tripal-site-admin-message">' .
  212. '<span class="tripal-severity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>' .
  213. $message .
  214. '</div>';
  215. // Handle whether to return the HTML & let the caller deal with it
  216. // or to use drupal_set_message to put it near the top of the page & let the
  217. // theme deal with it.
  218. if ($options['return_html']) {
  219. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  220. }
  221. else {
  222. drupal_set_message($full_message, 'tripal-site-admin-only');
  223. }
  224. }
  225. /**
  226. * File-based error logging for Tripal.
  227. *
  228. * Consider using the tripal_report_error function rather than
  229. * calling this function directly, as that function calls this one for non
  230. * INFO messages and has greater functionality.
  231. *
  232. * @param $message
  233. * The message to be logged. Need not contain date/time information.
  234. * @param $log_type
  235. * The type of log. Should be one of 'error' or 'job' although other types
  236. * are supported.
  237. * @param $options
  238. * An array of options where the following keys are supported:
  239. * - first_progress_bar: this should be used for the first log call for a
  240. * progress bar.
  241. * - is_progress_bar: this option should be used for all but the first print
  242. * of a progress bar to allow it all to be printed on the same line
  243. * without intervening date prefixes.
  244. *
  245. * @return
  246. * The number of bytes that were written to the file, or FALSE on failure.
  247. *
  248. * @ingroup tripal_notify_api
  249. */
  250. function tripal_log($message, $type = 'error', $options = []) {
  251. global $base_url;
  252. $prefix = '[site ' . $base_url . '] [TRIPAL ' . strtoupper(check_plain($type)) . '] ';
  253. if (!isset($options['is_progress_bar'])) {
  254. $message = $prefix . str_replace("\n", "", trim($message));
  255. }
  256. if (isset($options['first_progress_bar'])) {
  257. $message = $prefix . trim($message);
  258. }
  259. return error_log($message);
  260. }