Fix a bug in daemonize().

Our std stream closing was broken.
Add new logging code.
Convert existing code to use logger.h.
Write pid file even in foreground mode.
Add some logging messages.