mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
Non locking ::fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite
This commit is contained in:
@@ -93,7 +93,7 @@ void file_helper::write(const memory_buf_t &buf) const {
|
||||
if (fd_ == nullptr) return;
|
||||
const size_t msg_size = buf.size();
|
||||
const auto *data = buf.data();
|
||||
if (std::fwrite(data, 1, msg_size, fd_) != msg_size) {
|
||||
if (!os::fwrite_bytes(data, msg_size, fd_)) {
|
||||
throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
|
||||
}
|
||||
}
|
||||
|
@@ -326,6 +326,17 @@ std::string getenv(const char *field) {
|
||||
// Return true on success
|
||||
bool fsync(FILE *fp) { return ::fsync(fileno(fp)) == 0; }
|
||||
|
||||
|
||||
// Non locking ::fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite
|
||||
bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp)
|
||||
{
|
||||
#if defined(SPDLOG_FWRITE_UNLOCKED)
|
||||
return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#else
|
||||
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace os
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
@@ -304,6 +304,15 @@ std::string getenv(const char *field) {
|
||||
// Return true on success
|
||||
bool fsync(FILE *fp) { return FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp)))) != 0; }
|
||||
|
||||
// Non locking fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite
|
||||
bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp)
|
||||
{
|
||||
#if defined(SPDLOG_FWRITE_UNLOCKED)
|
||||
return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#else
|
||||
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#endif
|
||||
}
|
||||
} // namespace os
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
@@ -87,12 +87,12 @@ void ansicolor_sink<Mutex>::flush_() {
|
||||
|
||||
template <typename Mutex>
|
||||
void ansicolor_sink<Mutex>::print_ccode_(const string_view_t color_code) {
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_);
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
void ansicolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_);
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
|
@@ -1,11 +1,12 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/sinks/stdout_sinks.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
#include "spdlog/sinks/stdout_sinks.h"
|
||||
#include "spdlog/details/os.h"
|
||||
|
||||
// clang-format off
|
||||
#ifdef _WIN32
|
||||
@@ -23,7 +24,6 @@
|
||||
|
||||
// clang-format on
|
||||
namespace spdlog {
|
||||
|
||||
namespace sinks {
|
||||
|
||||
template <typename Mutex>
|
||||
@@ -61,7 +61,7 @@ void stdout_sink_base<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||
#else
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
|
||||
details::os::fwrite_bytes(formatted.data(), formatted.size(), file_);
|
||||
#endif // _WIN32
|
||||
::fflush(file_); // flush every line to terminal
|
||||
}
|
||||
|
Reference in New Issue
Block a user