fprintf is not thread safe. Thanks @Ryan and @Xuepeng for the discussing to make the logging thread safe.