1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-01-16 09:47:56 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Gabi Melman
dccb766095 Fix warning about enum usage 2020-09-27 19:08:24 +03:00
gabime
c97983a91c Fix linux build 2020-09-27 18:42:27 +03:00
gabime
680fb07fd5 Updatd WriteFile usage 2020-09-27 18:34:01 +03:00
gabime
cfd0ea197c Simplify WriteFile() usage under windows 2020-09-27 18:32:08 +03:00
gabime
48d4ed9bc0 Fix #1675 2020-09-27 18:27:41 +03:00
5 changed files with 50 additions and 20 deletions

View File

@ -45,12 +45,12 @@ public:
return;
}
if (padinfo_.side_ == padding_info::left)
if (padinfo_.side_ == padding_info::pad_side::left)
{
pad_it(remaining_pad_);
remaining_pad_ = 0;
}
else if (padinfo_.side_ == padding_info::center)
else if (padinfo_.side_ == padding_info::pad_side::center)
{
auto half_pad = remaining_pad_ / 2;
auto reminder = remaining_pad_ & 1;
@ -1285,15 +1285,15 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri
switch (*it)
{
case '-':
side = padding_info::right;
side = padding_info::pad_side::right;
++it;
break;
case '=':
side = padding_info::center;
side = padding_info::pad_side::center;
++it;
break;
default:
side = details::padding_info::left;
side = details::padding_info::pad_side::left;
break;
}

View File

@ -22,7 +22,7 @@ namespace details {
// padding information.
struct padding_info
{
enum pad_side
enum class pad_side
{
left,
right,
@ -42,7 +42,7 @@ struct padding_info
return enabled_;
}
size_t width_ = 0;
pad_side side_ = left;
pad_side side_ = pad_side::left;
bool truncate_ = false;
bool enabled_ = false;
};

View File

@ -11,6 +11,15 @@
#include <spdlog/pattern_formatter.h>
#include <memory>
#ifdef _WIN32
// under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675)
// so instead we use ::FileWrite
#include <spdlog/details/windows_include.h>
#include <fileapi.h> // WriteFile (..)
#include <io.h> // _get_osfhandle(..)
#include <stdio.h> // _fileno(..)
#endif // WIN32
namespace spdlog {
namespace sinks {
@ -20,7 +29,16 @@ SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
: mutex_(ConsoleMutex::mutex())
, file_(file)
, formatter_(details::make_unique<spdlog::pattern_formatter>())
{}
{
#ifdef _WIN32
// get windows handle from the FILE* object
handle_ = (HANDLE)::_get_osfhandle(::_fileno(file_));
if (handle_ == INVALID_HANDLE_VALUE)
{
throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
}
#endif // WIN32
}
template<typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg)
@ -28,8 +46,18 @@ 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
#ifdef _WIN32
auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0;
bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
if (!ok)
{
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
}
#else
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
::fflush(file_); // flush every line to terminal
#endif // WIN32
}
template<typename ConsoleMutex>

View File

@ -8,6 +8,10 @@
#include <spdlog/sinks/sink.h>
#include <cstdio>
#ifdef _WIN32
#include <spdlog/details/windows_include.h>
#endif
namespace spdlog {
namespace sinks {
@ -36,6 +40,9 @@ protected:
mutex_t &mutex_;
FILE *file_;
std::unique_ptr<spdlog::formatter> formatter_;
#ifdef _WIN32
HANDLE handle_;
#endif // WIN32
};
template<typename ConsoleMutex>

View File

@ -151,17 +151,12 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_
return;
}
DWORD total_written = 0;
do
DWORD bytes_written = 0;
bool ok = ::WriteFile(out_handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
if (!ok)
{
DWORD bytes_written = 0;
bool ok = ::WriteFile(out_handle_, formatted.data() + total_written, size - total_written, &bytes_written, nullptr) != 0;
if (!ok || bytes_written == 0)
{
throw_spdlog_ex("wincolor_sink: write_to_file_ failed. GetLastError(): " + std::to_string(::GetLastError()));
}
total_written += bytes_written;
} while (total_written < size);
throw_spdlog_ex("wincolor_sink: ::WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
}
}
// wincolor_stdout_sink