Factor out a common pattern. This also means we can set a breakpoint to find out where we're erroring. (I don't know why setting a breakpoint on fprintf doesn't work, but on at least one system it doesn't.)