Commit 2b59393b authored by Artem Martynovich's avatar Artem Martynovich

Add wchar_t support for Windows.

parent 08b6b0be
...@@ -36,6 +36,14 @@ ...@@ -36,6 +36,14 @@
#define SPDLOG_NOEXCEPT throw() #define SPDLOG_NOEXCEPT throw()
#endif #endif
#ifdef WIN32
typedef std::wstring tstring;
typedef wchar_t tchar;
#else
#define L
typedef std::string tstring;
typedef char tchar;
#endif
namespace spdlog namespace spdlog
{ {
......
...@@ -2648,6 +2648,11 @@ Any write method will throw ``std::runtime_error`` if the output doesn't fit ...@@ -2648,6 +2648,11 @@ Any write method will throw ``std::runtime_error`` if the output doesn't fit
into the array. into the array.
You can use one of the following typedefs for common character types: You can use one of the following typedefs for common character types:
#ifdef WIN32
#define TMemoryWriter WMemoryWriter
#else
#define TMemoryWriter MemoryWriter
#endif
+--------------+---------------------------+ +--------------+---------------------------+
| Type | Definition | | Type | Definition |
...@@ -3152,4 +3157,4 @@ FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef) ...@@ -3152,4 +3157,4 @@ FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
# include "format.cc" # include "format.cc"
#endif #endif
#endif // FMT_FORMAT_H_ #endif // FMT_FORMAT_H_
\ No newline at end of file
...@@ -147,17 +147,33 @@ constexpr inline unsigned short eol_size() ...@@ -147,17 +147,33 @@ constexpr inline unsigned short eol_size()
#endif #endif
//fopen_s on non windows for writing //fopen_s on non windows for writing
inline int fopen_s(FILE** fp, const std::string& filename, const char* mode) inline int fopen_s(FILE** fp, const tstring& filename, tchar* mode)
{ {
#ifdef _WIN32 #ifdef _WIN32
*fp = _fsopen((filename.c_str()), mode, _SH_DENYWR); *fp = _wfsopen((filename.c_str()), mode, _SH_DENYWR);
return *fp == nullptr; return *fp == nullptr;
#else #else
*fp = fopen((filename.c_str()), mode); *fp = fopen((filename.c_str()), mode);
return *fp == nullptr; return *fp == nullptr;
#endif #endif
}
inline int remove(const tchar* filename)
{
#ifdef _WIN32
return _wremove(filename);
#else
return std::remove(filename);
#endif
}
inline int rename(const tchar* filename1, const tchar* filename2)
{
#ifdef _WIN32
return _wrename(filename1, filename2);
#else
return std::remove(filename1, filename2);
#endif
} }
//Return utc offset in minutes or -1 on failure //Return utc offset in minutes or -1 on failure
......
...@@ -48,24 +48,24 @@ inline void spdlog::drop(const std::string &name) ...@@ -48,24 +48,24 @@ inline void spdlog::drop(const std::string &name)
} }
// Create multi/single threaded rotating file logger // Create multi/single threaded rotating file logger
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const tstring& filename, size_t max_file_size, size_t max_files, bool force_flush)
{ {
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, force_flush); return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, L"txt", max_file_size, max_files, force_flush);
} }
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const tstring& filename, size_t max_file_size, size_t max_files, bool force_flush)
{ {
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "txt", max_file_size, max_files, force_flush); return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, L"txt", max_file_size, max_files, force_flush);
} }
// Create file logger which creates new file at midnight): // Create file logger which creates new file at midnight):
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const tstring& filename, int hour, int minute, bool force_flush)
{ {
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", hour, minute, force_flush); return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, L"txt", hour, minute, force_flush);
} }
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const tstring& filename, int hour, int minute, bool force_flush)
{ {
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", hour, minute, force_flush); return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, L"txt", hour, minute, force_flush);
} }
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "../details/file_helper.h" #include "../details/file_helper.h"
#include "../details/format.h" #include "../details/format.h"
#include "../details/os.h"
namespace spdlog namespace spdlog
{ {
...@@ -71,7 +72,7 @@ template<class Mutex> ...@@ -71,7 +72,7 @@ template<class Mutex>
class rotating_file_sink : public base_sink < Mutex > class rotating_file_sink : public base_sink < Mutex >
{ {
public: public:
rotating_file_sink(const std::string &base_filename, const std::string &extension, rotating_file_sink(const tstring &base_filename, const tstring &extension,
std::size_t max_size, std::size_t max_files, std::size_t max_size, std::size_t max_files,
bool force_flush = false) : bool force_flush = false) :
_base_filename(base_filename), _base_filename(base_filename),
...@@ -102,13 +103,13 @@ protected: ...@@ -102,13 +103,13 @@ protected:
} }
private: private:
static std::string calc_filename(const std::string& filename, std::size_t index, const std::string& extension) static tstring calc_filename(const tstring& filename, std::size_t index, const tstring& extension)
{ {
fmt::MemoryWriter w; fmt::TMemoryWriter w;
if (index) if (index)
w.write("{}.{}.{}", filename, index, extension); w.write(L"{}.{}.{}", filename, index, extension);
else else
w.write("{}.{}", filename, extension); w.write(L"{}.{}", filename, extension);
return w.str(); return w.str();
} }
...@@ -123,28 +124,28 @@ private: ...@@ -123,28 +124,28 @@ private:
_file_helper.close(); _file_helper.close();
for (auto i = _max_files; i > 0; --i) for (auto i = _max_files; i > 0; --i)
{ {
std::string src = calc_filename(_base_filename, i - 1, _extension); tstring src = calc_filename(_base_filename, i - 1, _extension);
std::string target = calc_filename(_base_filename, i, _extension); tstring target = calc_filename(_base_filename, i, _extension);
if (details::file_helper::file_exists(target)) if (details::file_helper::file_exists(target))
{ {
if (std::remove(target.c_str()) != 0) if (details::os::remove(target.c_str()) != 0)
{ {
throw spdlog_ex("rotating_file_sink: failed removing " + target); throw spdlog_ex("rotating_file_sink: failed removing");
} }
} }
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str())) if (details::file_helper::file_exists(src) && details::os::rename(src.c_str(), target.c_str()))
{ {
throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target); throw spdlog_ex("rotating_file_sink: failed renaming");
} }
} }
_file_helper.reopen(true); _file_helper.reopen(true);
} }
std::string _base_filename; tstring _base_filename,
std::string _extension; _extension;
std::size_t _max_size; std::size_t _max_size,
std::size_t _max_files; _max_files,
std::size_t _current_size; _current_size;
details::file_helper _file_helper; details::file_helper _file_helper;
}; };
...@@ -160,8 +161,8 @@ class daily_file_sink :public base_sink < Mutex > ...@@ -160,8 +161,8 @@ class daily_file_sink :public base_sink < Mutex >
public: public:
//create daily file sink which rotates on given time //create daily file sink which rotates on given time
daily_file_sink( daily_file_sink(
const std::string& base_filename, const tstring& base_filename,
const std::string& extension, const tstring& extension,
int rotation_hour, int rotation_hour,
int rotation_minute, int rotation_minute,
bool force_flush = false) : _base_filename(base_filename), bool force_flush = false) : _base_filename(base_filename),
...@@ -210,16 +211,16 @@ private: ...@@ -210,16 +211,16 @@ private:
} }
//Create filename for the form basename.YYYY-MM-DD.extension //Create filename for the form basename.YYYY-MM-DD.extension
static std::string calc_filename(const std::string& basename, const std::string& extension) static tstring calc_filename(const tstring& basename, const tstring& extension)
{ {
std::tm tm = spdlog::details::os::localtime(); std::tm tm = spdlog::details::os::localtime();
fmt::MemoryWriter w; fmt::TMemoryWriter w;
w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, extension); w.write(L"{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, extension);
return w.str(); return w.str();
} }
std::string _base_filename; tstring _base_filename;
std::string _extension; tstring _extension;
int _rotation_h; int _rotation_h;
int _rotation_m; int _rotation_m;
std::chrono::system_clock::time_point _rotation_tp; std::chrono::system_clock::time_point _rotation_tp;
......
...@@ -76,14 +76,14 @@ void set_sync_mode(); ...@@ -76,14 +76,14 @@ void set_sync_mode();
// //
// Create and register multi/single threaded rotating file logger // Create and register multi/single threaded rotating file logger
// //
std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const tstring& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const tstring& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
// //
// Create file logger which creates new file on the given time (default in midnight): // Create file logger which creates new file on the given time (default in midnight):
// //
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const tstring& filename, int hour=0, int minute=0, bool force_flush = false);
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const tstring& filename, int hour=0, int minute=0, bool force_flush = false);
// //
......
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