Log content escapeing

Also fixed misplacement of close(IN) in my previous patch.
Merge remote-tracking branch 'upstream/master'