Use Thread to log message at exit
Clean up duplicate code
(cherry picked from commit 68ec8ac)
Merge pull request #74 from silentshade/fix_shutdown_error

Use Thread to log message at exit