diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 61c0a719..9142ebcf 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -52,9 +52,9 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg // before color range print_range_(formatted, 0, msg.color_range_start); // in color range - print_ccode_(colors_[msg.level]); + print_view_(colors_[msg.level]); print_range_(formatted, msg.color_range_start, msg.color_range_end); - print_ccode_(reset); + print_view_(reset); // after color range print_range_(formatted, msg.color_range_end, formatted.size()); } @@ -62,14 +62,20 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg { print_range_(formatted, 0, formatted.size()); } - fflush(target_file_); + if (fflush(target_file_)) + { + throw_spdlog_ex("ansicolor_sink: fflush() failed", errno); + } } template SPDLOG_INLINE void ansicolor_sink::flush() { std::lock_guard lock(mutex_); - fflush(target_file_); + if (0 != fflush(target_file_)) + { + throw_spdlog_ex("ansicolor_sink: fflush() failed", errno); + } } template @@ -112,15 +118,18 @@ SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) } template -SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) +SPDLOG_INLINE void ansicolor_sink::print_view_(const string_view_t &sv) { - fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); + if (sv.size() != fwrite(sv.data(), sizeof(char), sv.size(), target_file_)) + { + throw_spdlog_ex("ansicolor_sink: fwrite() failed", errno); + } } template SPDLOG_INLINE void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { - fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); + print_view_(string_view_t{formatted.data() + start, end - start}); } template diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 4438f706..8733ab63 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -85,7 +85,7 @@ private: bool should_do_colors_; std::unique_ptr formatter_; std::array colors_; - void print_ccode_(const string_view_t &color_code); + void print_view_(const string_view_t &sv); void print_range_(const memory_buf_t &formatted, size_t start, size_t end); static std::string to_string_(const string_view_t &sv); }; diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index 9122152a..efa147bd 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -60,7 +60,10 @@ SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &m std::lock_guard lock(mutex_); memory_buf_t formatted; formatter_->format(msg, formatted); - ::fflush(file_); // flush in case there is somthing in this file_ already + if (0 != ::fflush(file_)) // flush in case there is something in this file_ already + { + throw_spdlog_ex("stdout_sink_base: fflush() failed", errno); + } auto size = static_cast(formatted.size()); DWORD bytes_written = 0; bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; @@ -72,8 +75,14 @@ SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &m 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 + if (formatted.size() != ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_)) + { + throw_spdlog_ex("stdout_sink_base: fwrite() failed", errno); + } + if (0 != ::fflush(file_)) // flush every line to terminal + { + throw_spdlog_ex("stdout_sink_base: fflush() failed", errno); + } #endif // WIN32 } @@ -81,7 +90,10 @@ template SPDLOG_INLINE void stdout_sink_base::flush() { std::lock_guard lock(mutex_); - fflush(file_); + if (0 != ::fflush(file_)) + { + throw_spdlog_ex("stdout_sink_base: Failed flushing stdout", errno); + } } template