=== added directory 'datasync_backtrace'
=== added file 'datasync_backtrace/datasync_backtrace.info'
--- datasync_backtrace/datasync_backtrace.info	1970-01-01 00:00:00 +0000
+++ datasync_backtrace/datasync_backtrace.info	2009-10-09 21:26:13 +0000
@@ -0,0 +1,8 @@
+; $Id$
+name = DataSync Backtrace
+description = Backtrace saving and display feature for DataSync
+package = DataSync
+dependencies[] = datasync
+core = 6.x
+php = 5.1
+

=== added file 'datasync_backtrace/datasync_backtrace.install'
--- datasync_backtrace/datasync_backtrace.install	1970-01-01 00:00:00 +0000
+++ datasync_backtrace/datasync_backtrace.install	2009-10-12 15:51:08 +0000
@@ -0,0 +1,68 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Database schema definition
+ */
+
+/**
+ * Implementation of hook_install().
+ */
+function datasync_backtrace_install() {
+  drupal_install_schema('datasync_backtrace');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function datasync_backtrace_uninstall() {
+  drupal_uninstall_schema('datasync_backtrace');
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function datasync_backtrace_schema() {
+  $schema = array();
+
+  $schema['datasync_backtrace'] = array(
+    'fields' => array(
+      'backtrace_id' => array(
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE
+      ),
+      'job_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => t('DataSync job id')
+      ),
+      'job_phase' => array(
+        'type' => 'varchar',
+        'length' => 20,
+        'not null' => TRUE,
+        'default' => '',
+        'description' => t('DataSync phase where it happened')
+      ),
+      'php_error_code' => array(
+        'type' => 'int',
+        'size' => 'medium',
+        'unsigned' => TRUE,
+        'not null' => FALSE,
+        'default' => 0,
+        'description' => t('PHP error code')
+      ),
+      'php_backtrace' => array(
+        'type' => 'blob',
+        'not null' => FALSE,
+        'description' => t('PHP exception backtrace')
+      ),
+    ),
+    'indexes' => array( 'job_id' => array('job_id') ),
+    'primary key' => array('backtrace_id'),
+  );
+
+  return $schema;
+}

=== added file 'datasync_backtrace/datasync_backtrace.module'
--- datasync_backtrace/datasync_backtrace.module	1970-01-01 00:00:00 +0000
+++ datasync_backtrace/datasync_backtrace.module	2009-10-12 15:33:26 +0000
@@ -0,0 +1,123 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Hooks and API for datasync_backtrace
+ */
+
+/**
+ * Implementation of hook_menu().
+ */
+function datasync_backtrace_menu() {
+  $items = array();
+  $items['datasync/backtrace/%datasync_backtrace'] = array(
+    'title' => 'DataSync Backtrace',
+    'access arguments' => array('see datasync backtraces'),
+    'page callback' => 'datasync_backtrace_view_page',
+    'page arguments' => array(4),
+    'type' => MENU_CALLBACK,
+    'weight' => -6,
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_theme().
+ */
+function datasync_backtrace_theme() {
+  $items = array();
+  $path = drupal_get_path('module', 'datasync_backtrace') . '/theme';
+  $items['datasync_backtrace'] = array(
+    'arguments' => array('backtrace' => NULL),
+    'template' => 'datasync-backtrace',
+    'path' => $path
+  );
+  return $items;
+}
+
+/**
+ *  Preprocess function for DataSync backtrace.
+ */
+function template_preprocess_datasync_backtrace(&$vars) {
+  $backtrace = &$vars['backtrace'];
+  $vars['job_id'] = $backtrace->job_id;
+  $vars['job_phase'] = $backtrace->job_phase;
+  $vars['php_error_code'] = $backtrace->php_error_code;
+  $vars['php_backtrace'] = $backtrace->php_backtrace;
+}
+
+/**
+ * Implementation of hook_perms().
+ */
+function datasync_backtrace_perms() {
+  return array('see datasync backtraces');
+}
+
+/**
+ * Implementation of hook_datasync_job_removed().
+ */
+function datasync_backtrace_datasync_job_removed($job) {
+  db_query("DELETE FROM {datasync_backtrace} WHERE job_id = %d", array($job->job_id));
+}
+
+/**
+ * Implementation of hook_datasync_exception_thrown().
+ */
+function datasync_backtrace_datasync_exception_thrown($data, $btr) {
+  /*
+   * TODO DataSync hooks are not well documented, I'll keep this reminder here.
+   * 
+   * $data is array($message, $variables, $error_code, $job) where
+   *  - $message is Exception message
+   *  - $error_code is PHP error code
+   *  - $job is datasync_job class instance
+   * 
+   * $btr is PHP backtrace
+   */
+
+  if (is_array($data) && ! empty($data)) {
+    list($message, $variables, $error_code, $job) = $data;
+
+    // Case no job was running (phase is probably 'do_phase')
+    if (! is_object($job)) {
+      $job = (object) $job;
+      $job->job_id = -1;
+    }
+
+    $object = array(
+      'job_id' => $job->job_id,
+      'job_phase' => $job->phase,
+      'php_error_code' => $error_code,
+      'php_backtrace' => var_export($btr, TRUE)
+    );
+    $object = (object) $object;
+
+    $write = drupal_write_record('datasync_backtrace', $object); 
+
+    if ($write == SAVED_NEW) {
+      $link = l(t('View backtrace'), 'datasync/backtrace/' . $object->backtrace_id);
+      watchdog('datasync_backtrace', $message, $variables, WATCHDOG_DEBUG, $link);
+    }
+    else {
+      watchdog('datasync_backtrace', $message, $variables, WATCHDOG_DEBUG);
+    }
+  }
+  else {
+    watchdog('datasync_backtrace', 'Exception thrown but no data given', array(), WATCHDOG_DEBUG);
+  }
+}
+
+/**
+ * View backtrace page
+ * 
+ * @param object $backtrace
+ *   datasync_acktrace row object loaded from database
+ */
+function datasync_backtrace_view_page($backtrace) {
+  return theme('datasync_backtrace', $backtrace);
+}
+
+function datasync_backtrace_load($backtrace_id) {
+  return db_fetch_object(db_query("SELECT * FROM {datasync_backtrace} WHERE backtrace_id = %d", array($backtrace_id)));
+}

=== added directory 'datasync_backtrace/theme'
=== added file 'datasync_backtrace/theme/datasync-backtrace.tpl.php'
--- datasync_backtrace/theme/datasync-backtrace.tpl.php	1970-01-01 00:00:00 +0000
+++ datasync_backtrace/theme/datasync-backtrace.tpl.php	2009-10-12 15:35:08 +0000
@@ -0,0 +1,13 @@
+<?php /* $Id$ */ ?>
+<div class="datasync-backtrace">
+  <div class="datasync-backtrace-info">
+    <h3><?php print t("Information"); ?></h3>
+    <strong><?php print t('Job ID'); ?></strong>: <?php print $job_id; ?><br/>
+    <strong><?php print t('Phase when error occurred'); ?></strong>: <?php print $job_phase; ?><br/>
+    <strong><?php print t('PHP error code'); ?></strong>: <?php print $php_error_code; ?><br/>
+  </div>
+  <div class="datasync-backtrace-data">
+    <h3><?php print t("PHP backtrace"); ?></h3>
+    <pre><?php print $php_backtrace; ?></pre>
+  </div>
+</div>
\ No newline at end of file

