Improve error logging in file IO