Check for i/o errors in console sinks.

pull/1920/head
Paul Kunysch 4 years ago
parent ffb1b03943
commit 36f9fb48b7

@ -52,9 +52,9 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::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<ConsoleMutex>::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<typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::flush()
{
std::lock_guard<mutex_t> lock(mutex_);
fflush(target_file_);
if (0 != fflush(target_file_))
{
throw_spdlog_ex("ansicolor_sink: fflush() failed", errno);
}
}
template<typename ConsoleMutex>
@ -112,15 +118,18 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
}
template<typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code)
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::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<typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::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<typename ConsoleMutex>

@ -85,7 +85,7 @@ private:
bool should_do_colors_;
std::unique_ptr<spdlog::formatter> formatter_;
std::array<std::string, level::n_levels> 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);
};

@ -60,7 +60,10 @@ SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &m
std::lock_guard<mutex_t> 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<DWORD>(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<ConsoleMutex>::log(const details::log_msg &m
std::lock_guard<mutex_t> 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<typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::flush()
{
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_);
if (0 != ::fflush(file_))
{
throw_spdlog_ex("stdout_sink_base: Failed flushing stdout", errno);
}
}
template<typename ConsoleMutex>

Loading…
Cancel
Save