--- port.orig	2009-03-01 15:53:09.000000000 -0700
+++ port	2009-03-02 01:49:14.000000000 -0700
@@ -39,6 +39,20 @@
 package require macports
 package require Pextlib 1.0
 
+trace add execution exit enter exit_cleanup
+
+proc exit_cleanup {commandstring op} {
+    global exit_status ::debuglog ::debuglogname
+    if {[info exists ::debuglog]} {
+        close $::debuglog
+        if {$exit_status == 0} {
+            file delete -force $::debuglogname
+        } else {
+            puts "See the debug log at $::debuglogname"
+        }
+    }
+}
+
 
 # Standard procedures
 proc print_usage {args} {
@@ -157,6 +171,15 @@
     }
 }
 
+proc ui_channels {priority} {
+    global ::debuglog ::debuglogname
+    set default_channel [macports::ui_channels_default $priority]
+    if {![info exists ::debuglog]} {
+        set ::debuglogname [mktemp /tmp/macports_debug.XXXXXX]
+        set ::debuglog [open $::debuglogname w]
+    }
+    return [concat $default_channel $::debuglog]
+}
 
 # Form a composite version as is sometimes used for registry functions
 proc composite_version {version variations {emptyVersionOkay 0}} {
@@ -3604,6 +3627,7 @@
 set global_options_base [array get global_options]
 
 # First process any remaining args as action(s)
+global exit_status
 set exit_status 0
 if { [llength $remaining_args] > 0 } {
 

