Added a version of print_tree that dumps to a variable, so that you can control how/when it gets printed. Useful for including in exceptions