restructure threading logic

- background thread gets started as before but main logging function
  'logit()' stays in the main thread
- add an cleanup function via atexit() to stop background thread