diff --git includes/backend.inc includes/backend.inc index 4e5e372..162f2d8 100644 --- includes/backend.inc +++ includes/backend.inc @@ -60,6 +60,7 @@ * Identify the JSON encoded output from a command. */ define('DRUSH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<>>%s<< $data['output']))); } - else { - print ($data['output']); - } - } /** @@ -240,9 +223,34 @@ function _drush_proc_open($cmd, $data = NULL, $context = NULL) { $info = stream_get_meta_data($pipes[1]); stream_set_blocking($pipes[1], TRUE); stream_set_timeout($pipes[1], 1); - $string = ''; + $output = ''; + $end_of_output = FALSE; + $log_regex = sprintf(DRUSH_BACKEND_LOG_DELIMITER, '(.*)'); while (!feof($pipes[1]) && !$info['timed_out']) { - $string .= fgets($pipes[1], 4096); + $string = fgets($pipes[1], 4096); + if (preg_match('/DRUSH_BACKEND_OUTPUT_START/', $string)) { + $end_of_output = TRUE; + } + if (!$end_of_output) { + // If the line is a log line, log it immediately, so the user can see + // it. + if (preg_match("/$log_regex/s", $string, $match)) { + $entry = (array) json_decode($match[1]); + $message = is_array($entry['message']) ? implode("\n", $entry['message']) : $entry['message']; + if (!is_null($entry['error'])) { + drush_set_error($entry['error'], $message); + } + else { + _drush_log_backend($entry); + } + $string = preg_replace("/$log_regex/s", '', $string); + } + else { + // Pass output through. + fwrite(STDOUT, $string); + } + } + $output .= $string; $info = stream_get_meta_data($pipes[1]); flush(); }; @@ -251,7 +259,9 @@ function _drush_proc_open($cmd, $data = NULL, $context = NULL) { stream_set_blocking($pipes[2], TRUE); stream_set_timeout($pipes[2], 1); while (!feof($pipes[2]) && !$info['timed_out']) { - $string .= fgets($pipes[2], 4096); + $string = fgets($pipes[2], 4096); + $output .= $string; + fwrite(STDERR, $string); $info = stream_get_meta_data($pipes[2]); flush(); }; @@ -259,7 +269,7 @@ function _drush_proc_open($cmd, $data = NULL, $context = NULL) { fclose($pipes[1]); fclose($pipes[2]); $code = proc_close($process); - return array('cmd' => $cmd, 'output' => $string, 'code' => $code); + return array('cmd' => $cmd, 'output' => $output, 'code' => $code); } return FALSE; } diff --git includes/drush.inc includes/drush.inc index 4957f63..6d9b5df 100644 --- includes/drush.inc +++ includes/drush.inc @@ -977,8 +977,6 @@ function drush_do_command_redispatch($command, $args = array(), $remote_host = N * All other types of messages will be assumed to be notices. */ function drush_log($message, $type = 'notice', $error = null) { - $log =& drush_get_context('DRUSH_LOG', array()); - $callback = drush_get_context('DRUSH_LOG_CALLBACK', '_drush_print_log'); $entry = array( 'type' => $type, 'message' => $message, @@ -986,7 +984,22 @@ function drush_log($message, $type = 'notice', $error = null) { 'memory' => memory_get_usage(), ); $entry['error'] = $error; + return _drush_log_backend($entry); +} + +/** + * Add a log message to the log history. For use for backend_invoke. + * @param entry + * The log entry. + */ +function _drush_log_backend($entry) { + $log =& drush_get_context('DRUSH_LOG', array()); + $callback = drush_get_context('DRUSH_LOG_CALLBACK', '_drush_print_log'); $log[] = $entry; + if (drush_get_context('DRUSH_BACKEND')) { + printf(DRUSH_BACKEND_LOG_DELIMITER, json_encode($entry)); + } + return $callback($entry); } diff --git includes/environment.inc includes/environment.inc index 08850f5..6cc008f 100644 --- includes/environment.inc +++ includes/environment.inc @@ -654,7 +654,7 @@ function _drush_bootstrap_drush() { // When running in backend mode, all output is buffered, and returned // as a property of a JSON encoded associative array. - if ($backend || $quiet) { + if ($quiet) { ob_start(); }