Logging errors


Is there a neat way to log errors in a program?

My Mega 2560 logs sensor readings on an SD card and emails that file to me once a day.

I perform a bunch of functions, which don't always work, like getting an NTP Sync, occasional I2C error etc...

Writing a separate file seems the obvious solution, but are there any programming tricks, or do I just have to perform the if->SD.write at every instance?

If there isn't, my thought was to dig up every Serial.print (" error" ) and add a routine that puts a time-stamp to it along with the "error"...