Unverified Commit 46d41816 authored by Gabi Melman's avatar Gabi Melman Committed by GitHub

Merge pull request #1787 from sylveon/windows-separator-filenames

Better support for / separators on Windows, improve wchar filename coverage
parents 710a0e3a ac35dd5a
...@@ -5,31 +5,51 @@ environment: ...@@ -5,31 +5,51 @@ environment:
- GENERATOR: '"Visual Studio 14 2015"' - GENERATOR: '"Visual Studio 14 2015"'
BUILD_TYPE: Debug BUILD_TYPE: Debug
WCHAR: 'OFF' WCHAR: 'OFF'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'OFF' BUILD_SHARED: 'OFF'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 14 2015"' - GENERATOR: '"Visual Studio 14 2015"'
BUILD_TYPE: Release BUILD_TYPE: Release
WCHAR: 'ON' WCHAR: 'ON'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'OFF' BUILD_SHARED: 'OFF'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 14 2015 Win64"' - GENERATOR: '"Visual Studio 14 2015 Win64"'
BUILD_TYPE: Debug BUILD_TYPE: Debug
WCHAR: 'ON' WCHAR: 'ON'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'OFF' BUILD_SHARED: 'OFF'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 14 2015 Win64"' - GENERATOR: '"Visual Studio 14 2015 Win64"'
BUILD_TYPE: Release BUILD_TYPE: Release
WCHAR: 'ON' WCHAR: 'ON'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'OFF' BUILD_SHARED: 'OFF'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 15 2017 Win64"' - GENERATOR: '"Visual Studio 15 2017 Win64"'
BUILD_TYPE: Debug BUILD_TYPE: Debug
WCHAR: 'ON' WCHAR: 'ON'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'OFF' BUILD_SHARED: 'OFF'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 15 2017 Win64"' - GENERATOR: '"Visual Studio 15 2017 Win64"'
BUILD_TYPE: Release BUILD_TYPE: Release
WCHAR: 'OFF' WCHAR: 'OFF'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'OFF' BUILD_SHARED: 'OFF'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 15 2017 Win64"' - GENERATOR: '"Visual Studio 15 2017 Win64"'
BUILD_TYPE: Release BUILD_TYPE: Release
WCHAR: 'OFF' WCHAR: 'OFF'
WCHAR_FILES: 'OFF'
BUILD_SHARED: 'ON' BUILD_SHARED: 'ON'
BUILD_EXAMPLE: 'ON'
- GENERATOR: '"Visual Studio 15 2017 Win64"'
BUILD_TYPE: Release
WCHAR: 'ON'
WCHAR_FILES: 'ON'
BUILD_SHARED: 'ON'
BUILD_EXAMPLE: 'OFF'
build_script: build_script:
- cmd: >- - cmd: >-
set set
...@@ -40,7 +60,7 @@ build_script: ...@@ -40,7 +60,7 @@ build_script:
set PATH=%PATH%;C:\Program Files\Git\usr\bin set PATH=%PATH%;C:\Program Files\Git\usr\bin
cmake .. -G %GENERATOR% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DSPDLOG_WCHAR_SUPPORT=%WCHAR% -DSPDLOG_BUILD_SHARED=%BUILD_SHARED% -DSPDLOG_BUILD_EXAMPLE=ON -DSPDLOG_BUILD_EXAMPLE_HO=ON -DSPDLOG_BUILD_TESTS=ON -DSPDLOG_BUILD_TESTS_HO=OFF -DSPDLOG_BUILD_WARNINGS=ON cmake .. -G %GENERATOR% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DSPDLOG_WCHAR_SUPPORT=%WCHAR% -DSPDLOG_WCHAR_FILENAMES=%WCHAR_FILES% -DSPDLOG_BUILD_SHARED=%BUILD_SHARED% -DSPDLOG_BUILD_EXAMPLE=%BUILD_EXAMPLE% -DSPDLOG_BUILD_EXAMPLE_HO=%BUILD_EXAMPLE% -DSPDLOG_BUILD_TESTS=ON -DSPDLOG_BUILD_TESTS_HO=OFF -DSPDLOG_BUILD_WARNINGS=ON
cmake --build . --config %BUILD_TYPE% cmake --build . --config %BUILD_TYPE%
......
...@@ -88,7 +88,9 @@ class sink; ...@@ -88,7 +88,9 @@ class sink;
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
using filename_t = std::wstring; using filename_t = std::wstring;
#define SPDLOG_FILENAME_T(s) L##s // 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 #else
using filename_t = std::string; using filename_t = std::string;
#define SPDLOG_FILENAME_T(s) s #define SPDLOG_FILENAME_T(s) s
...@@ -101,6 +103,7 @@ using err_handler = std::function<void(const std::string &err_msg)>; ...@@ -101,6 +103,7 @@ using err_handler = std::function<void(const std::string &err_msg)>;
using string_view_t = fmt::basic_string_view<char>; using string_view_t = fmt::basic_string_view<char>;
using wstring_view_t = fmt::basic_string_view<wchar_t>; using wstring_view_t = fmt::basic_string_view<wchar_t>;
using memory_buf_t = fmt::basic_memory_buffer<char, 250>; using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
#ifndef _WIN32 #ifndef _WIN32
......
...@@ -133,7 +133,7 @@ SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension ...@@ -133,7 +133,7 @@ SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension
} }
// treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
auto folder_index = fname.rfind(details::os::folder_sep); auto folder_index = fname.find_last_of(details::os::folder_seps_filename);
if (folder_index != filename_t::npos && folder_index >= ext_index - 1) if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
{ {
return std::make_tuple(fname, filename_t()); return std::make_tuple(fname, filename_t());
......
...@@ -436,7 +436,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT ...@@ -436,7 +436,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target)
{ {
if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max)())) if (wstr.size() > static_cast<size_t>(std::numeric_limits<int>::max()) / 2 - 1)
{ {
throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8"); throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8");
} }
...@@ -469,9 +469,9 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) ...@@ -469,9 +469,9 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target)
throw_spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); throw_spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
} }
SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target) SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target)
{ {
if (str.size() > static_cast<size_t>((std::numeric_limits<int>::max)())) 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"); throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16");
} }
...@@ -484,7 +484,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target) ...@@ -484,7 +484,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target)
} }
int result_size = static_cast<int>(target.capacity()); int result_size = static_cast<int>(target.capacity());
if ((str_size + 1) * 2 < result_size) if (str_size + 1 > result_size)
{ {
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0); result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
} }
...@@ -492,7 +492,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target) ...@@ -492,7 +492,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target)
if (result_size > 0) if (result_size > 0)
{ {
target.resize(result_size); target.resize(result_size);
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, (LPWSTR)target.data(), result_size); result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
if (result_size > 0) if (result_size > 0)
{ {
...@@ -533,15 +533,10 @@ SPDLOG_INLINE bool create_dir(filename_t path) ...@@ -533,15 +533,10 @@ SPDLOG_INLINE bool create_dir(filename_t path)
return false; return false;
} }
#ifdef _WIN32
// support forward slash in windows
std::replace(path.begin(), path.end(), '/', folder_sep);
#endif
size_t search_offset = 0; size_t search_offset = 0;
do do
{ {
auto token_pos = path.find(folder_sep, search_offset); auto token_pos = path.find_first_of(folder_seps_filename, search_offset);
// treat the entire path as a folder if no folder separator not found // treat the entire path as a folder if no folder separator not found
if (token_pos == filename_t::npos) if (token_pos == filename_t::npos)
{ {
...@@ -567,11 +562,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) ...@@ -567,11 +562,7 @@ SPDLOG_INLINE bool create_dir(filename_t path)
// "abc///" => "abc//" // "abc///" => "abc//"
SPDLOG_INLINE filename_t dir_name(filename_t path) SPDLOG_INLINE filename_t dir_name(filename_t path)
{ {
#ifdef _WIN32 auto pos = path.find_last_of(folder_seps_filename);
// support forward slash in windows
std::replace(path.begin(), path.end(), '/', folder_sep);
#endif
auto pos = path.find_last_of(folder_sep);
return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; return pos != filename_t::npos ? path.substr(0, pos) : filename_t{};
} }
......
...@@ -32,11 +32,16 @@ SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT; ...@@ -32,11 +32,16 @@ SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT;
SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL; SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL;
// folder separator // folder separator
#if !defined(SPDLOG_FOLDER_SEPS)
#ifdef _WIN32 #ifdef _WIN32
static const char folder_sep = '\\'; #define SPDLOG_FOLDER_SEPS "\\/"
#else #else
SPDLOG_CONSTEXPR static const char folder_sep = '/'; #define SPDLOG_FOLDER_SEPS "/"
#endif #endif
#endif
SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS;
SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
// fopen_s on non windows for writing // fopen_s on non windows for writing
SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode); SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
...@@ -86,7 +91,7 @@ SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; ...@@ -86,7 +91,7 @@ SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target);
SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target); SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
#endif #endif
// Return directory name from given path or empty string // Return directory name from given path or empty string
......
...@@ -13,11 +13,13 @@ ...@@ -13,11 +13,13 @@
#include <spdlog/fmt/fmt.h> #include <spdlog/fmt/fmt.h>
#include <spdlog/formatter.h> #include <spdlog/formatter.h>
#include <algorithm>
#include <array> #include <array>
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>
#include <cctype> #include <cctype>
#include <cstring> #include <cstring>
#include <iterator>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
...@@ -814,11 +816,31 @@ public: ...@@ -814,11 +816,31 @@ public:
: flag_formatter(padinfo) : flag_formatter(padinfo)
{} {}
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4127) // consider using 'if constexpr' instead
#endif // _MSC_VER
static const char *basename(const char *filename) static const char *basename(const char *filename)
{ {
const char *rv = std::strrchr(filename, os::folder_sep); // if the size is 2 (1 character + null terminator) we can use the more efficient strrchr
return rv != nullptr ? rv + 1 : filename; // the branch will be elided by optimizations
if (sizeof(os::folder_seps) == 2)
{
const char *rv = std::strrchr(filename, os::folder_seps[0]);
return rv != nullptr ? rv + 1 : filename;
}
else
{
const std::reverse_iterator<const char*> begin(filename + std::strlen(filename));
const std::reverse_iterator<const char*> end(filename);
const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps), std::end(os::folder_seps) - 1);
return it != end ? it.base() : filename;
}
} }
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{ {
......
...@@ -224,35 +224,22 @@ protected: ...@@ -224,35 +224,22 @@ protected:
formatted.push_back('\0'); formatted.push_back('\0');
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
try wmemory_buf_t buf;
{ details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
memory_buf_t buf;
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
LPCWSTR lp_wstr = reinterpret_cast<LPCWSTR>(buf.data()); LPCWSTR lp_wstr = buf.data();
succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_, succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr); current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr);
if (!succeeded)
{
SPDLOG_THROW(win32_error("ReportEvent"));
}
}
catch (...)
{
// WCHAR string conversion can fail and if it does, we shouldn't call to report event function.
}
#else #else
LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data()); LPCSTR lp_str = formatted.data();
succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_, succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr); current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);
#endif
if (!succeeded) if (!succeeded)
{ {
SPDLOG_THROW(win32_error("ReportEvent")); SPDLOG_THROW(win32_error("ReportEvent"));
} }
#endif
} }
void flush_() override {} void flush_() override {}
......
...@@ -58,6 +58,14 @@ ...@@ -58,6 +58,14 @@
// #define SPDLOG_EOL ";-)\n" // #define SPDLOG_EOL ";-)\n"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to override default folder separators ("/" or "\\/" under
// Linux/Windows). Each character in the string is treated as a different
// separator.
//
// #define SPDLOG_FOLDER_SEPS "\\"
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Uncomment to use your own copy of the fmt library instead of spdlog's copy. // Uncomment to use your own copy of the fmt library instead of spdlog's copy.
// In this case spdlog will try to include <fmt/format.h> so set your -I flag // In this case spdlog will try to include <fmt/format.h> so set your -I flag
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "test_sink.h" #include "test_sink.h"
#define TEST_FILENAME "test_logs/async_test.log"
TEST_CASE("basic async test ", "[async]") TEST_CASE("basic async test ", "[async]")
{ {
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>(); auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
...@@ -149,7 +151,7 @@ TEST_CASE("to_file", "[async]") ...@@ -149,7 +151,7 @@ TEST_CASE("to_file", "[async]")
prepare_logdir(); prepare_logdir();
size_t messages = 1024; size_t messages = 1024;
size_t tp_threads = 1; size_t tp_threads = 1;
std::string filename = "test_logs/async_test.log"; spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
{ {
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true); auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads); auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
...@@ -161,8 +163,8 @@ TEST_CASE("to_file", "[async]") ...@@ -161,8 +163,8 @@ TEST_CASE("to_file", "[async]")
} }
} }
require_message_count(filename, messages); require_message_count(TEST_FILENAME, messages);
auto contents = file_contents(filename); auto contents = file_contents(TEST_FILENAME);
using spdlog::details::os::default_eol; using spdlog::details::os::default_eol;
REQUIRE(ends_with(contents, fmt::format("Hello message #1023{}", default_eol))); REQUIRE(ends_with(contents, fmt::format("Hello message #1023{}", default_eol)));
} }
...@@ -172,7 +174,7 @@ TEST_CASE("to_file multi-workers", "[async]") ...@@ -172,7 +174,7 @@ TEST_CASE("to_file multi-workers", "[async]")
prepare_logdir(); prepare_logdir();
size_t messages = 1024 * 10; size_t messages = 1024 * 10;
size_t tp_threads = 10; size_t tp_threads = 10;
std::string filename = "test_logs/async_test.log"; spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
{ {
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true); auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads); auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
...@@ -184,5 +186,5 @@ TEST_CASE("to_file multi-workers", "[async]") ...@@ -184,5 +186,5 @@ TEST_CASE("to_file multi-workers", "[async]")
} }
} }
require_message_count(filename, messages); require_message_count(TEST_FILENAME, messages);
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
using spdlog::details::os::create_dir; using spdlog::details::os::create_dir;
using spdlog::details::os::path_exists; using spdlog::details::os::path_exists;
bool try_create_dir(const char *path, const char *normalized_path) bool try_create_dir(const spdlog::filename_t &path, const spdlog::filename_t &normalized_path)
{ {
auto rv = create_dir(path); auto rv = create_dir(path);
REQUIRE(rv == true); REQUIRE(rv == true);
...@@ -17,24 +17,24 @@ TEST_CASE("create_dir", "[create_dir]") ...@@ -17,24 +17,24 @@ TEST_CASE("create_dir", "[create_dir]")
{ {
prepare_logdir(); prepare_logdir();
REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"), SPDLOG_FILENAME_T("test_logs/dir1/dir1")));
REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); // test existing REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"), SPDLOG_FILENAME_T("test_logs/dir1/dir1"))); // test existing
REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1///dir2//"), SPDLOG_FILENAME_T("test_logs/dir1/dir2")));
REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("./test_logs/dir1/dir3"), SPDLOG_FILENAME_T("test_logs/dir1/dir3")));
REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/../test_logs/dir1/dir4"), SPDLOG_FILENAME_T("test_logs/dir1/dir4")));
#ifdef WIN32 #ifdef WIN32
// test backslash folder separator // test backslash folder separator
REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir222"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir222")));
REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\")));
REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir2\\dir23")));
REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\..\\test_logs\\dir1\\dir5"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir5")));
#endif #endif
} }
TEST_CASE("create_invalid_dir", "[create_dir]") TEST_CASE("create_invalid_dir", "[create_dir]")
{ {
REQUIRE(create_dir("") == false); REQUIRE(create_dir(SPDLOG_FILENAME_T("")) == false);
REQUIRE(create_dir(spdlog::filename_t{}) == false); REQUIRE(create_dir(spdlog::filename_t{}) == false);
#ifdef __linux__ #ifdef __linux__
REQUIRE(create_dir("/proc/spdlog-utest") == false); REQUIRE(create_dir("/proc/spdlog-utest") == false);
...@@ -44,37 +44,31 @@ TEST_CASE("create_invalid_dir", "[create_dir]") ...@@ -44,37 +44,31 @@ TEST_CASE("create_invalid_dir", "[create_dir]")
TEST_CASE("dir_name", "[create_dir]") TEST_CASE("dir_name", "[create_dir]")
{ {
using spdlog::details::os::dir_name; using spdlog::details::os::dir_name;
REQUIRE(dir_name("").empty()); REQUIRE(dir_name(SPDLOG_FILENAME_T("")).empty());
REQUIRE(dir_name("dir").empty()); REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty());
#ifdef WIN32 #ifdef WIN32
REQUIRE(dir_name(R"(dir\)") == "dir"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\\\)")) == SPDLOG_FILENAME_T(R"(dir\\)"));
REQUIRE(dir_name(R"(dir\file)") == "dir"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt\)")) == SPDLOG_FILENAME_T(R"(dir\file.txt)"));
REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\dir)"));
REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\\dir)"));
REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(..\file.txt)")) == SPDLOG_FILENAME_T(".."));
REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(.\file.txt)")) == SPDLOG_FILENAME_T("."));
REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c:\\a\b\c\d\file.txt)")) == SPDLOG_FILENAME_T(R"(c:\\a\b\c\d)"));
REQUIRE(dir_name(R"(..\file.txt)") == ".."); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c://a/b/c/d/file.txt)")) == SPDLOG_FILENAME_T(R"(c://a/b/c/d)"));
REQUIRE(dir_name(R"(../file.txt)") == "..");
REQUIRE(dir_name(R"(.\file.txt)") == ".");
REQUIRE(dir_name(R"(./file.txt)") == ".");
REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)");
REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)");
#else
REQUIRE(dir_name("dir/") == "dir");
REQUIRE(dir_name("dir///") == "dir//");
REQUIRE(dir_name("dir/file") == "dir");
REQUIRE(dir_name("dir/file.txt") == "dir");
REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt");
REQUIRE(dir_name("/dir/file.txt") == "/dir");
REQUIRE(dir_name("//dir/file.txt") == "//dir");
REQUIRE(dir_name("../file.txt") == "..");
REQUIRE(dir_name("./file.txt") == ".");
#endif #endif
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir///")) == SPDLOG_FILENAME_T("dir//"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt")) == SPDLOG_FILENAME_T("dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt/")) == SPDLOG_FILENAME_T("dir/file.txt"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("/dir/file.txt")) == SPDLOG_FILENAME_T("/dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir"));
REQUIRE(dir_name(SPDLOG_FILENAME_T("../file.txt")) == SPDLOG_FILENAME_T(".."));
REQUIRE(dir_name(SPDLOG_FILENAME_T("./file.txt")) == SPDLOG_FILENAME_T("."));
} }
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
*/ */
#include "includes.h" #include "includes.h"
using filename_memory_buf_t = fmt::basic_memory_buffer<spdlog::filename_t::value_type, 250>;
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
{ {
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>; using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
...@@ -10,10 +12,10 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") ...@@ -10,10 +12,10 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
prepare_logdir(); prepare_logdir();
// calculate filename (time based) // calculate filename (time based)
std::string basename = "test_logs/daily_dateonly"; spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
std::tm tm = spdlog::details::os::localtime(); std::tm tm = spdlog::details::os::localtime();
spdlog::memory_buf_t w; filename_memory_buf_t w;
fmt::format_to(w, "{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); fmt::format_to(w, SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0); auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
...@@ -23,7 +25,13 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") ...@@ -23,7 +25,13 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
} }
logger->flush(); logger->flush();
#ifdef SPDLOG_WCHAR_FILENAMES
spdlog::memory_buf_t buf;
spdlog::details::os::wstr_to_utf8buf(fmt::to_string(w), buf);
auto filename = fmt::to_string(buf);
#else
auto filename = fmt::to_string(w); auto filename = fmt::to_string(w);
#endif
require_message_count(filename, 10); require_message_count(filename, 10);
} }
...@@ -31,8 +39,8 @@ struct custom_daily_file_name_calculator ...@@ -31,8 +39,8 @@ struct custom_daily_file_name_calculator
{ {
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm)
{ {
spdlog::memory_buf_t w; filename_memory_buf_t w;
fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday); fmt::format_to(w, SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday);
return fmt::to_string(w); return fmt::to_string(w);
} }
}; };
...@@ -44,10 +52,10 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") ...@@ -44,10 +52,10 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
prepare_logdir(); prepare_logdir();
// calculate filename (time based) // calculate filename (time based)
std::string basename = "test_logs/daily_dateonly"; spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
std::tm tm = spdlog::details::os::localtime(); std::tm tm = spdlog::details::os::localtime();
spdlog::memory_buf_t w; filename_memory_buf_t w;
fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); fmt::format_to(w, SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0); auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
...@@ -57,7 +65,13 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") ...@@ -57,7 +65,13 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
logger->flush(); logger->flush();
#ifdef SPDLOG_WCHAR_FILENAMES
spdlog::memory_buf_t buf;
spdlog::details::os::wstr_to_utf8buf(fmt::to_string(w), buf);
auto filename = fmt::to_string(buf);
#else
auto filename = fmt::to_string(w); auto filename = fmt::to_string(w);
#endif
require_message_count(filename, 10); require_message_count(filename, 10);
} }
...@@ -67,20 +81,20 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") ...@@ -67,20 +81,20 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]") TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]")
{ {
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3); auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 3);
REQUIRE(filename == "rotated.3.txt"); REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3.txt"));
} }
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]") TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]")
{ {
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3); auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated"), 3);
REQUIRE(filename == "rotated.3"); REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3"));
} }
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]") TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
{ {
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0); auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 0);
REQUIRE(filename == "rotated.txt"); REQUIRE(filename == SPDLOG_FILENAME_T("rotated.txt"));
} }
// regex supported only from gcc 4.9 and above // regex supported only from gcc 4.9 and above
...@@ -91,10 +105,10 @@ TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]") ...@@ -91,10 +105,10 @@ TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]]") TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]]")
{ {
// daily_YYYY-MM-DD_hh-mm.txt // daily_YYYY-MM-DD_hh-mm.txt
auto filename = spdlog::sinks::daily_filename_calculator::calc_filename("daily.txt", spdlog::details::os::localtime()); auto filename = spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
// date regex based on https://www.regular-expressions.info/dates.html // date regex based on https://www.regular-expressions.info/dates.html
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"); std::basic_regex<spdlog::filename_t::value_type> re(SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
std::smatch match; std::match_results<spdlog::filename_t::const_iterator> match;
REQUIRE(std::regex_match(filename, match, re)); REQUIRE(std::regex_match(filename, match, re));
} }
#endif #endif
...@@ -116,7 +130,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_ ...@@ -116,7 +130,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_
prepare_logdir(); prepare_logdir();
std::string basename = "test_logs/daily_rotate.txt"; spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_rotate.txt");
daily_file_sink_st sink{basename, 2, 30, true, max_days}; daily_file_sink_st sink{basename, 2, 30, true, max_days};
// simulate messages with 24 intervals // simulate messages with 24 intervals
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#include <iostream> #include <iostream>
#define SIMPLE_LOG "test_logs/simple_log.txt"
#define SIMPLE_ASYNC_LOG "test_logs/simple_async_log.txt"
class failing_sink : public spdlog::sinks::base_sink<std::mutex> class failing_sink : public spdlog::sinks::base_sink<std::mutex>
{ {
protected: protected:
...@@ -22,7 +25,7 @@ protected: ...@@ -22,7 +25,7 @@ protected:
TEST_CASE("default_error_handler", "[errors]]") TEST_CASE("default_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "test_logs/simple_log.txt"; spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v"); logger->set_pattern("%v");
...@@ -31,8 +34,8 @@ TEST_CASE("default_error_handler", "[errors]]") ...@@ -31,8 +34,8 @@ TEST_CASE("default_error_handler", "[errors]]")
logger->flush(); logger->flush();
using spdlog::details::os::default_eol; using spdlog::details::os::default_eol;
REQUIRE(file_contents(filename) == fmt::format("Test message 2{}", default_eol)); REQUIRE(file_contents(SIMPLE_LOG) == fmt::format("Test message 2{}", default_eol));
REQUIRE(count_lines(filename) == 1); REQUIRE(count_lines(SIMPLE_LOG) == 1);
} }
struct custom_ex struct custom_ex
...@@ -40,7 +43,7 @@ struct custom_ex ...@@ -40,7 +43,7 @@ struct custom_ex
TEST_CASE("custom_error_handler", "[errors]]") TEST_CASE("custom_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "test_logs/simple_log.txt"; spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
...@@ -48,7 +51,7 @@ TEST_CASE("custom_error_handler", "[errors]]") ...@@ -48,7 +51,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex); REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
logger->info("Good message #2"); logger->info("Good message #2");
require_message_count(filename, 2); require_message_count(SIMPLE_LOG, 2);
} }
TEST_CASE("default_error_handler2", "[errors]]") TEST_CASE("default_error_handler2", "[errors]]")
...@@ -72,7 +75,7 @@ TEST_CASE("async_error_handler", "[errors]]") ...@@ -72,7 +75,7 @@ TEST_CASE("async_error_handler", "[errors]]")
prepare_logdir(); prepare_logdir();
std::string err_msg("log failed with some msg"); std::string err_msg("log failed with some msg");
std::string filename = "test_logs/simple_async_log.txt"; spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_ASYNC_LOG);
{ {
spdlog::init_thread_pool(128, 1); spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true); auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
...@@ -90,7 +93,7 @@ TEST_CASE("async_error_handler", "[errors]]") ...@@ -90,7 +93,7 @@ TEST_CASE("async_error_handler", "[errors]]")
spdlog::drop("logger"); // force logger to drain the queue and shutdown spdlog::drop("logger"); // force logger to drain the queue and shutdown
} }
spdlog::init_thread_pool(128, 1); spdlog::init_thread_pool(128, 1);
require_message_count(filename, 2); require_message_count(SIMPLE_ASYNC_LOG, 2);
REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg); REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
} }
...@@ -100,7 +103,7 @@ TEST_CASE("async_error_handler2", "[errors]]") ...@@ -100,7 +103,7 @@ TEST_CASE("async_error_handler2", "[errors]]")
prepare_logdir(); prepare_logdir();
std::string err_msg("This is async handler error message"); std::string err_msg("This is async handler error message");
{ {
spdlog::details::os::create_dir("test_logs"); spdlog::details::os::create_dir(SPDLOG_FILENAME_T("test_logs"));
spdlog::init_thread_pool(128, 1); spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<failing_sink>("failed_logger"); auto logger = spdlog::create_async<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { logger->set_error_handler([=](const std::string &) {
......
...@@ -24,20 +24,20 @@ static void test_single_print(std::function<void(std::string const &)> do_log, s ...@@ -24,20 +24,20 @@ static void test_single_print(std::function<void(std::string const &)> do_log, s
REQUIRE(CloseEventLog(handle_)); REQUIRE(CloseEventLog(handle_));
} }
} }
} event_log{::OpenEventLog(nullptr, TEST_SOURCE)}; } event_log{::OpenEventLogA(nullptr, TEST_SOURCE)};
REQUIRE(event_log.handle_); REQUIRE(event_log.handle_);
DWORD read_bytes{}, size_needed{}; DWORD read_bytes{}, size_needed{};
auto ok = auto ok =
::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed); ::ReadEventLogA(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed);
REQUIRE(!ok); REQUIRE(!ok);
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER); REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
std::vector<char> record_buffer(size_needed); std::vector<char> record_buffer(size_needed);
PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data(); PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data();
ok = ::ReadEventLog( ok = ::ReadEventLogA(
event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed); event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed);
REQUIRE(ok); REQUIRE(ok);
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
*/ */
#include "includes.h" #include "includes.h"
#define TEST_FILENAME "test_logs/file_helper_test.txt"
using spdlog::details::file_helper; using spdlog::details::file_helper;
static void write_with_helper(file_helper &helper, size_t howmany) static void write_with_helper(file_helper &helper, size_t howmany)
...@@ -18,7 +20,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]") ...@@ -18,7 +20,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
prepare_logdir(); prepare_logdir();
file_helper helper; file_helper helper;
std::string target_filename = "test_logs/file_helper_test.txt"; spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
helper.open(target_filename); helper.open(target_filename);
REQUIRE(helper.filename() == target_filename); REQUIRE(helper.filename() == target_filename);
} }
...@@ -26,7 +28,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]") ...@@ -26,7 +28,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
TEST_CASE("file_helper_size", "[file_helper::size()]]") TEST_CASE("file_helper_size", "[file_helper::size()]]")
{ {
prepare_logdir(); prepare_logdir();
std::string target_filename = "test_logs/file_helper_test.txt"; spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
size_t expected_size = 123; size_t expected_size = 123;
{ {
file_helper helper; file_helper helper;
...@@ -34,13 +36,13 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]") ...@@ -34,13 +36,13 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]")
write_with_helper(helper, expected_size); write_with_helper(helper, expected_size);
REQUIRE(static_cast<size_t>(helper.size()) == expected_size); REQUIRE(static_cast<size_t>(helper.size()) == expected_size);
} }
REQUIRE(get_filesize(target_filename) == expected_size); REQUIRE(get_filesize(TEST_FILENAME) == expected_size);
} }
TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
{ {
prepare_logdir(); prepare_logdir();
std::string target_filename = "test_logs/file_helper_test.txt"; spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
file_helper helper; file_helper helper;
helper.open(target_filename); helper.open(target_filename);
write_with_helper(helper, 12); write_with_helper(helper, 12);
...@@ -52,7 +54,7 @@ TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") ...@@ -52,7 +54,7 @@ TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]") TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
{ {
prepare_logdir(); prepare_logdir();
std::string target_filename = "test_logs/file_helper_test.txt"; spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
size_t expected_size = 14; size_t expected_size = 14;
file_helper helper; file_helper helper;
helper.open(target_filename); helper.open(target_filename);
...@@ -62,16 +64,12 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]") ...@@ -62,16 +64,12 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
REQUIRE(helper.size() == expected_size); REQUIRE(helper.size() == expected_size);
} }
static void test_split_ext(const char *fname, const char *expect_base, const char *expect_ext) static void test_split_ext(const spdlog::filename_t::value_type *fname, const spdlog::filename_t::value_type *expect_base, const spdlog::filename_t::value_type *expect_ext)
{ {
spdlog::filename_t filename(fname); spdlog::filename_t filename(fname);
spdlog::filename_t expected_base(expect_base); spdlog::filename_t expected_base(expect_base);
spdlog::filename_t expected_ext(expect_ext); spdlog::filename_t expected_ext(expect_ext);
#ifdef _WIN32 // replace folder sep
std::replace(filename.begin(), filename.end(), '/', '\\');
std::replace(expected_base.begin(), expected_base.end(), '/', '\\');
#endif
spdlog::filename_t basename; spdlog::filename_t basename;
spdlog::filename_t ext; spdlog::filename_t ext;
std::tie(basename, ext) = file_helper::split_by_extension(filename); std::tie(basename, ext) = file_helper::split_by_extension(filename);
...@@ -81,22 +79,22 @@ static void test_split_ext(const char *fname, const char *expect_base, const cha ...@@ -81,22 +79,22 @@ static void test_split_ext(const char *fname, const char *expect_base, const cha
TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]]") TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]]")
{ {
test_split_ext("mylog.txt", "mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("mylog.txt"), SPDLOG_FILENAME_T("mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(".mylog.txt", ".mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T(".mylog.txt"), SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(".mylog", ".mylog", ""); test_split_ext(SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(""));
test_split_ext("/aaa/bb.d/mylog", "/aaa/bb.d/mylog", ""); test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T(""));
test_split_ext("/aaa/bb.d/mylog.txt", "/aaa/bb.d/mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext("aaa/bbb/ccc/mylog.txt", "aaa/bbb/ccc/mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog.txt"), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext("aaa/bbb/ccc/mylog.", "aaa/bbb/ccc/mylog.", ""); test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T(""));
test_split_ext("aaa/bbb/ccc/.mylog.txt", "aaa/bbb/ccc/.mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog.txt"), SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext("/aaa/bbb/ccc/mylog.txt", "/aaa/bbb/ccc/mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext("/aaa/bbb/ccc/.mylog", "/aaa/bbb/ccc/.mylog", ""); test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(""));
test_split_ext("../mylog.txt", "../mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("../mylog.txt"), SPDLOG_FILENAME_T("../mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(".././mylog.txt", ".././mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt"), SPDLOG_FILENAME_T(".././mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext(".././mylog.txt/xxx", ".././mylog.txt/xxx", ""); test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(""));
test_split_ext("/mylog.txt", "/mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("/mylog.txt"), SPDLOG_FILENAME_T("/mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext("//mylog.txt", "//mylog", ".txt"); test_split_ext(SPDLOG_FILENAME_T("//mylog.txt"), SPDLOG_FILENAME_T("//mylog"), SPDLOG_FILENAME_T(".txt"));
test_split_ext("", "", ""); test_split_ext(SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""));
test_split_ext(".", ".", ""); test_split_ext(SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(""));
test_split_ext("..txt", ".", ".txt"); test_split_ext(SPDLOG_FILENAME_T("..txt"), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(".txt"));
} }
...@@ -3,10 +3,13 @@ ...@@ -3,10 +3,13 @@
*/ */
#include "includes.h" #include "includes.h"
#define SIMPLE_LOG "test_logs/simple_log"
#define ROTATING_LOG "test_logs/rotating_log"
TEST_CASE("simple_file_logger", "[simple_logger]]") TEST_CASE("simple_file_logger", "[simple_logger]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "test_logs/simple_log"; spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
...@@ -15,29 +18,29 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") ...@@ -15,29 +18,29 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
logger->info("Test message {}", 2); logger->info("Test message {}", 2);
logger->flush(); logger->flush();
require_message_count(filename, 2); require_message_count(SIMPLE_LOG, 2);
using spdlog::details::os::default_eol; using spdlog::details::os::default_eol;
REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", default_eol, default_eol)); REQUIRE(file_contents(SIMPLE_LOG) == fmt::format("Test message 1{}Test message 2{}", default_eol, default_eol));
} }
TEST_CASE("flush_on", "[flush_on]]") TEST_CASE("flush_on", "[flush_on]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "test_logs/simple_log"; spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);
logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);
logger->trace("Should not be flushed"); logger->trace("Should not be flushed");
REQUIRE(count_lines(filename) == 0); REQUIRE(count_lines(SIMPLE_LOG) == 0);
logger->info("Test message {}", 1); logger->info("Test message {}", 1);
logger->info("Test message {}", 2); logger->info("Test message {}", 2);
require_message_count(filename, 3); require_message_count(SIMPLE_LOG, 3);
using spdlog::details::os::default_eol; using spdlog::details::os::default_eol;
REQUIRE(file_contents(filename) == REQUIRE(file_contents(SIMPLE_LOG) ==
fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", default_eol, default_eol, default_eol)); fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", default_eol, default_eol, default_eol));
} }
...@@ -45,7 +48,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") ...@@ -45,7 +48,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
{ {
prepare_logdir(); prepare_logdir();
size_t max_size = 1024 * 10; size_t max_size = 1024 * 10;
std::string basename = "test_logs/rotating_log"; spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0); auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
...@@ -54,14 +57,14 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") ...@@ -54,14 +57,14 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
} }
logger->flush(); logger->flush();
require_message_count(basename, 10); require_message_count(ROTATING_LOG, 10);
} }
TEST_CASE("rotating_file_logger2", "[rotating_logger]]") TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
{ {
prepare_logdir(); prepare_logdir();
size_t max_size = 1024 * 10; size_t max_size = 1024 * 10;
std::string basename = "test_logs/rotating_log"; spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
{ {
// make an initial logger to create the first output file // make an initial logger to create the first output file
...@@ -83,7 +86,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") ...@@ -83,7 +86,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
logger->flush(); logger->flush();
require_message_count(basename, 10); require_message_count(ROTATING_LOG, 10);
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
{ {
...@@ -92,7 +95,6 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") ...@@ -92,7 +95,6 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
} }
logger->flush(); logger->flush();
REQUIRE(get_filesize(basename) <= max_size); REQUIRE(get_filesize(ROTATING_LOG) <= max_size);
auto filename1 = basename + ".1"; REQUIRE(get_filesize(ROTATING_LOG ".1") <= max_size);
REQUIRE(get_filesize(filename1) <= max_size);
} }
...@@ -8,11 +8,13 @@ ...@@ -8,11 +8,13 @@
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG" #error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
#endif #endif
#define TEST_FILENAME "test_logs/simple_log"
TEST_CASE("debug and trace w/o format string", "[macros]]") TEST_CASE("debug and trace w/o format string", "[macros]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "test_logs/simple_log"; spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
...@@ -23,8 +25,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") ...@@ -23,8 +25,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
logger->flush(); logger->flush();
using spdlog::details::os::default_eol; using spdlog::details::os::default_eol;
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 2{}", default_eol))); REQUIRE(ends_with(file_contents(TEST_FILENAME), fmt::format("Test message 2{}", default_eol)));
REQUIRE(count_lines(filename) == 1); REQUIRE(count_lines(TEST_FILENAME) == 1);
spdlog::set_default_logger(logger); spdlog::set_default_logger(logger);
...@@ -32,8 +34,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") ...@@ -32,8 +34,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
SPDLOG_DEBUG("Test message {}", 4); SPDLOG_DEBUG("Test message {}", 4);
logger->flush(); logger->flush();
require_message_count(filename, 2); require_message_count(TEST_FILENAME, 2);
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 4{}", default_eol))); REQUIRE(ends_with(file_contents(TEST_FILENAME), fmt::format("Test message 4{}", default_eol)));
} }
TEST_CASE("disable param evaluation", "[macros]") TEST_CASE("disable param evaluation", "[macros]")
......
...@@ -372,7 +372,7 @@ TEST_CASE("clone-custom_formatter", "[pattern_formatter]") ...@@ -372,7 +372,7 @@ TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
// //
#ifdef _WIN32 #ifdef _WIN32
static const char *const test_path = "\\a\\b\\myfile.cpp"; static const char *const test_path = "\\a\\b\\c/myfile.cpp";
#else #else
static const char *const test_path = "/a/b//myfile.cpp"; static const char *const test_path = "/a/b//myfile.cpp";
#endif #endif
......
...@@ -103,7 +103,7 @@ TEST_CASE("disable automatic registration", "[registry]") ...@@ -103,7 +103,7 @@ TEST_CASE("disable automatic registration", "[registry]")
spdlog::set_level(log_level); spdlog::set_level(log_level);
// but disable automatic registration // but disable automatic registration
spdlog::set_automatic_registration(false); spdlog::set_automatic_registration(false);
auto logger1 = spdlog::create<spdlog::sinks::daily_file_sink_st>(tested_logger_name, "filename", 11, 59); auto logger1 = spdlog::create<spdlog::sinks::daily_file_sink_st>(tested_logger_name, SPDLOG_FILENAME_T("filename"), 11, 59);
auto logger2 = spdlog::create_async<spdlog::sinks::stdout_color_sink_mt>(tested_logger_name2); auto logger2 = spdlog::create_async<spdlog::sinks::stdout_color_sink_mt>(tested_logger_name2);
// loggers should not be part of the registry // loggers should not be part of the registry
REQUIRE_FALSE(spdlog::get(tested_logger_name)); REQUIRE_FALSE(spdlog::get(tested_logger_name));
......
...@@ -84,7 +84,7 @@ bool ends_with(std::string const &value, std::string const &ending) ...@@ -84,7 +84,7 @@ bool ends_with(std::string const &value, std::string const &ending)
std::size_t count_files(const std::string &folder) std::size_t count_files(const std::string &folder)
{ {
size_t counter = 0; size_t counter = 0;
WIN32_FIND_DATA ffd; WIN32_FIND_DATAA ffd;
// Start iterating over the files in the folder directory. // Start iterating over the files in the folder directory.
HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd); HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd);
...@@ -94,7 +94,7 @@ std::size_t count_files(const std::string &folder) ...@@ -94,7 +94,7 @@ std::size_t count_files(const std::string &folder)
{ {
if (ffd.cFileName[0] != '.') if (ffd.cFileName[0] != '.')
counter++; counter++;
} while (::FindNextFile(hFind, &ffd) != 0); } while (::FindNextFileA(hFind, &ffd) != 0);
::FindClose(hFind); ::FindClose(hFind);
} }
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment