Skip to content

Commit

Permalink
Removed SPDLOG_WCHAR_FILENAMES support
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Nov 29, 2024
1 parent fefda8d commit 2057f67
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 153 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ jobs:
cmake -G "${{ matrix.config.GENERATOR }}" -A x64 `
-D CMAKE_BUILD_TYPE=${{ matrix.config.BUILD_TYPE }} `
-D BUILD_SHARED_LIBS=${{ matrix.config.BUILD_SHARED }} `
-D SPDLOG_WCHAR_SUPPORT=${{ matrix.config.WCHAR }} `
-D SPDLOG_WCHAR_FILENAMES=${{ matrix.config.WCHAR_FILES }} `
-D SPDLOG_WCHAR_SUPPORT=${{ matrix.config.WCHAR }} `
-D SPDLOG_BUILD_EXAMPLE=${{ matrix.config.BUILD_EXAMPLE }} `
-D SPDLOG_BUILD_TESTS=ON `
-D SPDLOG_BUILD_WARNINGS=${{ matrix.config.FATAL_ERRORS }} `
Expand Down Expand Up @@ -83,15 +82,13 @@ jobs:
BUILD_SHARED: 'ON'
FATAL_ERRORS: 'ON'
WCHAR: 'OFF'
WCHAR_FILES: 'OFF'
BUILD_EXAMPLE: 'ON'
CXX_STANDARD: 17
- GENERATOR: "Visual Studio 16 2019"
BUILD_TYPE: Release
BUILD_SHARED: 'ON'
FATAL_ERRORS: 'ON'
WCHAR: 'OFF'
WCHAR_FILES: 'OFF'
BUILD_EXAMPLE: 'ON'
CXX_STANDARD: 20

Expand All @@ -107,8 +104,7 @@ jobs:
cmake -G "${{ matrix.config.GENERATOR }}" -A x64 `
-D CMAKE_BUILD_TYPE=${{ matrix.config.BUILD_TYPE }} `
-D BUILD_SHARED_LIBS=${{ matrix.config.BUILD_SHARED }} `
-D SPDLOG_WCHAR_SUPPORT=${{ matrix.config.WCHAR }} `
-D SPDLOG_WCHAR_FILENAMES=${{ matrix.config.WCHAR_FILES }} `
-D SPDLOG_WCHAR_SUPPORT=${{ matrix.config.WCHAR }} `
-D SPDLOG_BUILD_EXAMPLE=${{ matrix.config.BUILD_EXAMPLE }} `
-D SPDLOG_BUILD_TESTS=ON `
-D SPDLOG_BUILD_WARNINGS=${{ matrix.config.FATAL_ERRORS }} `
Expand Down
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF)
option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)

# misc tweakme options
if(WIN32)
option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF)
else()
set(SPDLOG_WCHAR_FILENAMES OFF CACHE BOOL "non supported option" FORCE)
endif()

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
option(SPDLOG_CLOCK_COARSE "Use CLOCK_REALTIME_COARSE instead of the regular clock," OFF)
else()
Expand Down
2 changes: 0 additions & 2 deletions cmake/spdlog_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#pragma once

// Support wchar filenames (Windows only)
#cmakedefine SPDLOG_WCHAR_FILENAMES

// No exceptions. Will do std::abort() instead
#cmakedefine SPDLOG_NO_EXCEPTIONS
Expand Down
20 changes: 1 addition & 19 deletions include/spdlog/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@

#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
#if defined(SPDLOG_WCHAR_FILENAMES)
#include "fmt/xchar.h"
#endif

#ifndef SPDLOG_FUNCTION
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
Expand Down Expand Up @@ -71,15 +68,8 @@ namespace sinks {
class sink;
}

#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
using filename_t = std::wstring;
// allow macro expansion to occur in SPDLOG_FILENAME_T
#define SPDLOG_FILENAME_T_INNER(s) L##s
#define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
#else
using filename_t = std::string;
#define SPDLOG_FILENAME_T(s) s
#endif
#define SPDLOG_FILENAME_T(s) s

using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr<sinks::sink>;
Expand Down Expand Up @@ -199,14 +189,6 @@ namespace details {

[[nodiscard]] constexpr spdlog::string_view_t to_string_view(spdlog::string_view_t str) noexcept { return str; }

#if defined(SPDLOG_WCHAR_FILENAMES)
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) noexcept {
return spdlog::wstring_view_t{buf.data(), buf.size()};
}

[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept { return str; }
#endif

template <typename T, typename... Args>
[[nodiscard]] constexpr fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) noexcept {
return fmt;
Expand Down
6 changes: 0 additions & 6 deletions include/spdlog/details/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ SPDLOG_API bool is_color_terminal() noexcept;
// Source: https://github.com/agauniyal/rang/
SPDLOG_API bool in_terminal(FILE *file) noexcept;

#if defined(SPDLOG_WCHAR_FILENAMES) && defined(_WIN32)
SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target);

SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
#endif

// Return directory name from given path or empty string
// "abc/file" => "abc"
// "abc/" => "abc"
Expand Down
10 changes: 3 additions & 7 deletions include/spdlog/sinks/daily_file_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ struct daily_filename_calculator {
*/
struct daily_filename_format_calculator {
static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) {
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
std::wstringstream stream;
#else
std::stringstream stream;
#endif
stream << std::put_time(&now_tm, file_path.c_str());
return stream.str();
std::stringstream stream;
stream << std::put_time(&now_tm, file_path.c_str());
return stream.str();
}
};

Expand Down
93 changes: 0 additions & 93 deletions src/details/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
#include <share.h>
#endif

#if defined(SPDLOG_WCHAR_FILENAMES)
#include <cassert>
#include <limits>
#endif

#include <direct.h> // for _mkdir/_wmkdir

// clang-format on
Expand Down Expand Up @@ -67,11 +62,7 @@ std::tm gmtime() noexcept {
}

bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) {
#ifdef SPDLOG_WCHAR_FILENAMES
*fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
#else
*fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
#endif
#if defined(SPDLOG_PREVENT_CHILD_FD)
if (*fp != nullptr) {
auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(::_fileno(*fp)));
Expand All @@ -85,31 +76,19 @@ bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) {
}

int remove(const filename_t &filename) noexcept {
#if defined(SPDLOG_WCHAR_FILENAMES)
return ::_wremove(filename.c_str());
#else
return std::remove(filename.c_str());
#endif
}

int remove_if_exists(const filename_t &filename) noexcept { return path_exists(filename) ? remove(filename) : 0; }

int rename(const filename_t &filename1, const filename_t &filename2) noexcept {
#if defined(SPDLOG_WCHAR_FILENAMES)
return ::_wrename(filename1.c_str(), filename2.c_str());
#else
return std::rename(filename1.c_str(), filename2.c_str());
#endif
}

// Return true if path exists (file or directory)
bool path_exists(const filename_t &filename) noexcept {
struct _stat buffer;
#ifdef SPDLOG_WCHAR_FILENAMES
return (::_wstat(filename.c_str(), &buffer) == 0);
#else
return (::_stat(filename.c_str(), &buffer) == 0);
#endif
}

#ifdef _MSC_VER
Expand Down Expand Up @@ -180,89 +159,17 @@ size_t thread_id() noexcept {
// See https://github.com/gabime/spdlog/issues/609
void sleep_for_millis(unsigned int milliseconds) noexcept { ::Sleep(milliseconds); }

// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
#if defined(SPDLOG_WCHAR_FILENAMES)
std::string filename_to_str(const filename_t &filename) {
memory_buf_t buf;
wstr_to_utf8buf(filename, buf);
return SPDLOG_BUF_TO_STRING(buf);
}
#else
std::string filename_to_str(const filename_t &filename) { return filename; }
#endif

int pid() noexcept { return static_cast<int>(::GetCurrentProcessId()); }

bool is_color_terminal() noexcept { return true; }

// Determine if the terminal attached
bool in_terminal(FILE *file) noexcept { return ::_isatty(_fileno(file)) != 0; }

#if defined(SPDLOG_WCHAR_FILENAMES)
void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) / 4 - 1) {
throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8");
}

int wstr_size = static_cast<int>(wstr.size());
if (wstr_size == 0) {
target.resize(0);
return;
}

int result_size = static_cast<int>(target.capacity());
if ((wstr_size + 1) * 4 > result_size) {
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
}

if (result_size > 0) {
target.resize(result_size);
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL);

if (result_size > 0) {
target.resize(result_size);
return;
}
}

throw_spdlog_ex(fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
}

void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
if (str.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) - 1) {
throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16");
}

int str_size = static_cast<int>(str.size());
if (str_size == 0) {
target.resize(0);
return;
}

// find the size to allocate for the result buffer
int result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);

if (result_size > 0) {
target.resize(result_size);
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
if (result_size > 0) {
assert(result_size == target.size());
return;
}
}

throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
}
#endif // defined(SPDLOG_WCHAR_FILENAMES)

// return true on success
static bool mkdir_(const filename_t &path) {
#
#ifdef SPDLOG_WCHAR_FILENAMES
return ::_wmkdir(path.c_str()) == 0;
#else
return ::_mkdir(path.c_str()) == 0;
#endif
}

// create the given directory - and all directories leading to it
Expand Down
15 changes: 2 additions & 13 deletions tests/test_daily_and_rotation_loggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@

using filename_memory_buf_t = spdlog::memory_buf_t;

// #ifdef SPDLOG_WCHAR_FILENAMES
// std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
// spdlog::memory_buf_t buf;
// spdlog::details::os::wstr_to_utf8buf(spdlog::wstring_view_t(w.data(), w.size()), buf);
// return SPDLOG_BUF_TO_STRING(buf);
// }
// #else
// std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) { return SPDLOG_BUF_TO_STRING(w); }
// #endif

std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) { return SPDLOG_BUF_TO_STRING(w); }

TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
Expand All @@ -38,7 +27,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
}
logger->flush();

require_message_count(filename_buf_to_utf8string(w), 10);
require_message_count(SPDLOG_BUF_TO_STRING(w), 10);
}

struct custom_daily_file_name_calculator {
Expand Down Expand Up @@ -69,7 +58,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
}

logger->flush();
require_message_count(filename_buf_to_utf8string(w), 10);
require_message_count(SPDLOG_BUF_TO_STRING(w), 10);
}

/*
Expand Down

0 comments on commit 2057f67

Please sign in to comment.