diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index 230720f4..25192607 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -48,15 +48,14 @@ SPDLOG_INLINE stdout_sink_base::stdout_sink_base(FILE *file) template SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &msg) { +#ifdef _WIN32 if (handle_ == INVALID_HANDLE_VALUE) { return; } - std::lock_guard lock(mutex_); memory_buf_t formatted; formatter_->format(msg, formatted); -#ifdef _WIN32 ::fflush(file_); // flush in case there is somthing in this file_ already auto size = static_cast(formatted.size()); DWORD bytes_written = 0; @@ -66,6 +65,9 @@ SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &m throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError())); } #else + std::lock_guard lock(mutex_); + memory_buf_t formatted; + formatter_->format(msg, formatted); ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_); ::fflush(file_); // flush every line to terminal #endif // WIN32