How to write log and catch signals