mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
wchar filenames support - minor improvements
This commit is contained in:
@@ -43,11 +43,11 @@ public:
|
||||
}
|
||||
|
||||
|
||||
void open(const filename_str_t& fname, bool truncate = false)
|
||||
void open(const filename_t& fname, bool truncate = false)
|
||||
{
|
||||
|
||||
close();
|
||||
const filename_char_t* mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
|
||||
auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
|
||||
_filename = fname;
|
||||
for (int tries = 0; tries < open_tries; ++tries)
|
||||
{
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
|
||||
}
|
||||
|
||||
throw spdlog_ex("Failed opening file " + filename_to_bytes(_filename) + " for writing");
|
||||
throw spdlog_ex("Failed opening file " + filename_to_str(_filename) + " for writing");
|
||||
}
|
||||
|
||||
void reopen(bool truncate)
|
||||
@@ -88,7 +88,7 @@ public:
|
||||
size_t msg_size = msg.formatted.size();
|
||||
auto data = msg.formatted.data();
|
||||
if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
|
||||
throw spdlog_ex("Failed writing to file " + filename_to_bytes(_filename));
|
||||
throw spdlog_ex("Failed writing to file " + filename_to_str(_filename));
|
||||
|
||||
if (_force_flush)
|
||||
std::fflush(_fd);
|
||||
@@ -98,19 +98,19 @@ public:
|
||||
long size()
|
||||
{
|
||||
if (!_fd)
|
||||
throw spdlog_ex("Cannot use size() on closed file " + filename_to_bytes(_filename));
|
||||
throw spdlog_ex("Cannot use size() on closed file " + filename_to_str(_filename));
|
||||
|
||||
auto pos = ftell(_fd);
|
||||
if (fseek(_fd, 0, SEEK_END) != 0)
|
||||
throw spdlog_ex("fseek failed on file " + filename_to_bytes(_filename));
|
||||
throw spdlog_ex("fseek failed on file " + filename_to_str(_filename));
|
||||
|
||||
auto file_size = ftell(_fd);
|
||||
|
||||
if(fseek(_fd, pos, SEEK_SET) !=0)
|
||||
throw spdlog_ex("fseek failed on file " + filename_to_bytes(_filename));
|
||||
throw spdlog_ex("fseek failed on file " + filename_to_str(_filename));
|
||||
|
||||
if (file_size == -1)
|
||||
throw spdlog_ex("ftell failed on file " + filename_to_bytes(_filename));
|
||||
throw spdlog_ex("ftell failed on file " + filename_to_str(_filename));
|
||||
|
||||
|
||||
return file_size;
|
||||
@@ -118,12 +118,12 @@ public:
|
||||
|
||||
}
|
||||
|
||||
const filename_str_t& filename() const
|
||||
const filename_t& filename() const
|
||||
{
|
||||
return _filename;
|
||||
}
|
||||
|
||||
static bool file_exists(const filename_str_t& name)
|
||||
static bool file_exists(const filename_t& name)
|
||||
{
|
||||
|
||||
return os::file_exists(name);
|
||||
@@ -133,7 +133,7 @@ public:
|
||||
|
||||
private:
|
||||
FILE* _fd;
|
||||
filename_str_t _filename;
|
||||
filename_t _filename;
|
||||
bool _force_flush;
|
||||
|
||||
|
||||
|
||||
@@ -137,45 +137,45 @@ constexpr inline unsigned short eol_size()
|
||||
#endif
|
||||
|
||||
//fopen_s on non windows for writing
|
||||
inline int fopen_s(FILE** fp, const filename_str_t& filename, const filename_char_t* mode)
|
||||
inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
#ifdef SPDLOG_USE_WCHAR
|
||||
*fp = _wfsopen((filename.c_str()), mode, _SH_DENYWR);
|
||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
||||
*fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
|
||||
#else
|
||||
*fp = _fsopen((filename.c_str()), mode, _SH_DENYWR);
|
||||
*fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
|
||||
#endif
|
||||
return *fp == nullptr;
|
||||
#else
|
||||
*fp = fopen((filename.c_str()), mode);
|
||||
*fp = fopen((filename.c_str()), mode.c_str());
|
||||
return *fp == nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int remove(const filename_char_t* filename)
|
||||
inline int remove(const filename_t &filename)
|
||||
{
|
||||
#if defined(_WIN32) && defined(SPDLOG_USE_WCHAR)
|
||||
return _wremove(filename);
|
||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||
return _wremove(filename.c_str());
|
||||
#else
|
||||
return std::remove(filename);
|
||||
return std::remove(filename.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int rename(const filename_char_t* filename1, const filename_char_t* filename2)
|
||||
inline int rename(const filename_t& filename1, const filename_t& filename2)
|
||||
{
|
||||
#if defined(_WIN32) && defined(SPDLOG_USE_WCHAR)
|
||||
return _wrename(filename1, filename2);
|
||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||
return _wrename(filename1.c_str(), filename2.c_str());
|
||||
#else
|
||||
return std::rename(filename1, filename2);
|
||||
return std::rename(filename1.c_str(), filename2.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
//Return if file exists
|
||||
inline bool file_exists(const filename_str_t& filename)
|
||||
inline bool file_exists(const filename_t& filename)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
#ifdef SPDLOG_USE_WCHAR
|
||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
||||
auto attribs = GetFileAttributesW(filename.c_str());
|
||||
#else
|
||||
auto attribs = GetFileAttributesA(filename.c_str());
|
||||
|
||||
@@ -36,23 +36,23 @@ inline void spdlog::drop(const std::string &name)
|
||||
}
|
||||
|
||||
// Create multi/single threaded rotating file logger
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const filename_str_t& 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 filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const filename_str_t& 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 filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush);
|
||||
}
|
||||
|
||||
// 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 filename_str_t& filename, int hour, int minute, bool force_flush)
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_str_t& filename, int hour, int minute, bool force_flush)
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user