Add log function to centralize printing.
Handle if not running in main thread.