/[drupal]/contributions/modules/civicrm_error/civicrm_error.module
ViewVC logotype

Contents of /contributions/modules/civicrm_error/civicrm_error.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Fri Jul 20 15:44:34 2007 UTC (2 years, 4 months ago) by dalin
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of civicrm_error module.  Will send you a detailed backtrace of civicrm errors in an email.
1 <?php
2
3 // $Id$
4
5 /**
6 * Custom error function
7 * Set CiviCRM » Administer CiviCRM » Global Settings » Debugging » Fatal Error Handler
8 * To use this function
9 */
10 function civicrm_error_handler() {
11
12 $site = variable_get('site_name', 'drupal');
13
14 // generate output
15 $output .= 'There was a CiviCRM error at '. $site .".\n";
16
17 // error details
18 if (function_exists('error_get_last')) {
19 $output .= "\n\n***ERROR***\n";
20 $output .= print_r(error_get_last(), true);
21 }
22
23 // user info
24 global $user;
25 $output .= "\n\n***USER***\n";
26 $output .= _civicrm_error_parse_array($user);
27
28 // $_SERVER
29 global $_SERVER;
30 $output .= "\n\n***SERVER***\n";
31 $output .= _civicrm_error_parse_array($_SERVER);
32
33 // backtrace
34 $backtrace = debug_backtrace();
35 $output .= "\n\n***BACKTRACE***\n";
36 foreach ($backtrace as $call) {
37 $output .= "**next call**\n";
38 $output .= _civicrm_error_parse_array($call);
39 }
40
41 // send email
42 $subject = 'CiviCRM error at '. $site;
43 $to = variable_get('civicrm_error_to', variable_get("site_mail", ini_get("sendmail_from")));
44 drupal_mail('civicrm_error', $to, $subject, $output);
45
46 }
47
48 /**
49 * Helper function to return a pretty print of the given array
50 */
51 function _civicrm_error_parse_array($array) {
52 foreach((array)$array as $key => $value) {
53 if (is_array($value) || is_object($value)) {
54 $value = print_r($value, true);
55 }
56 $key = str_pad($key .':', 20, ' ');
57 $output .= $key . (string)_civicrm_error_check_length($value) ." \n";
58 }
59 return $output ."\n";
60 }
61
62 /**
63 * Helper function to add elipses and return spaces if null
64 */
65 function _civicrm_error_check_length($item) {
66 if (is_null($item)) {
67 return ' ';
68 }
69 if (strlen($item) > 2000) {
70 $item = substr($item, 0, 2000) .'...';
71 }
72 return $item;
73 }
74
75 /**
76 * Implementation of hook_menu().
77 */
78 function civicrm_error_menu($may_cache) {
79 $items = array();
80 if ($may_cache) {
81 $items[] = array(
82 'path' => 'admin/logs/civicrm_error',
83 'title' => t('CiviCRM Error Handler'),
84 'description' => t('Email critical CiviCRM errors.'),
85 'callback' => 'drupal_get_form',
86 'callback arguments' => 'civicrm_error_settings',
87 'access' => user_access('administer site configuration'),
88 'type' => MENU_NORMAL_ITEM,
89 );
90 }
91 return $items;
92 }
93
94
95 // Settings page
96 function civicrm_error_settings() {
97 $form['civicrm_error_to'] = array(
98 '#type' => 'textfield',
99 '#title' => t('Email'),
100 '#maxlength' => 255,
101 '#default_value' => variable_get('civicrm_error_to', variable_get("site_mail", ini_get("sendmail_from"))),
102 '#description' => t("Select an email address to send all CiviCRM errors to."),
103 '#required' => TRUE,
104 );
105 return system_settings_form($form);
106 }

  ViewVC Help
Powered by ViewVC 1.1.2