Add logging tests, add "log" command

Refactor log.c ready for log file rotation