mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-16 09:47:56 +00:00
Compare commits
5 Commits
3bed78356e
...
dccb766095
Author | SHA1 | Date | |
---|---|---|---|
|
dccb766095 | ||
|
c97983a91c | ||
|
680fb07fd5 | ||
|
cfd0ea197c | ||
|
48d4ed9bc0 |
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user