mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
Reverted pull #111 - wchar support under windows - it pollutes global namespace with new defines
This commit is contained in:
@@ -48,7 +48,7 @@ public:
|
||||
const int open_tries = 5;
|
||||
const int open_interval = 10;
|
||||
|
||||
explicit file_helper(bool force_flush):
|
||||
explicit file_helper(bool force_flush) :
|
||||
_fd(nullptr),
|
||||
_force_flush(force_flush)
|
||||
{}
|
||||
@@ -62,26 +62,26 @@ public:
|
||||
}
|
||||
|
||||
|
||||
void open(const tstring& fname, bool truncate=false)
|
||||
void open(const std::string& fname, bool truncate = false)
|
||||
{
|
||||
|
||||
close();
|
||||
const tchar* mode = truncate ? S("wb") : S("ab");
|
||||
const char* mode = truncate ? "wb" : "ab";
|
||||
_filename = fname;
|
||||
for (int tries = 0; tries < open_tries; ++tries)
|
||||
{
|
||||
if(!os::fopen_s(&_fd, fname, mode))
|
||||
if (!os::fopen_s(&_fd, fname, mode))
|
||||
return;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
|
||||
}
|
||||
|
||||
throw spdlog_ex("Failed opening file for writing");
|
||||
throw spdlog_ex("Failed opening file " + fname + " for writing");
|
||||
}
|
||||
|
||||
void reopen(bool truncate)
|
||||
{
|
||||
if(_filename.empty())
|
||||
if (_filename.empty())
|
||||
throw spdlog_ex("Failed re opening file - was not opened before");
|
||||
open(_filename, truncate);
|
||||
|
||||
@@ -105,23 +105,23 @@ public:
|
||||
|
||||
size_t size = msg.formatted.size();
|
||||
auto data = msg.formatted.data();
|
||||
if(std::fwrite(data, 1, size, _fd) != size)
|
||||
throw spdlog_ex("Failed writing to file");
|
||||
if (std::fwrite(data, 1, size, _fd) != size)
|
||||
throw spdlog_ex("Failed writing to file " + _filename);
|
||||
|
||||
if(_force_flush)
|
||||
if (_force_flush)
|
||||
std::fflush(_fd);
|
||||
|
||||
}
|
||||
|
||||
const tstring& filename() const
|
||||
const std::string& filename() const
|
||||
{
|
||||
return _filename;
|
||||
}
|
||||
|
||||
static bool file_exists(const tstring& name)
|
||||
static bool file_exists(const std::string& name)
|
||||
{
|
||||
FILE* file;
|
||||
if (!os::fopen_s(&file, name.c_str(), S("r")))
|
||||
if (!os::fopen_s(&file, name.c_str(), "r"))
|
||||
{
|
||||
fclose(file);
|
||||
return true;
|
||||
@@ -134,11 +134,10 @@ public:
|
||||
|
||||
private:
|
||||
FILE* _fd;
|
||||
tstring _filename;
|
||||
std::string _filename;
|
||||
bool _force_flush;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -147,37 +147,17 @@ constexpr inline unsigned short eol_size()
|
||||
#endif
|
||||
|
||||
//fopen_s on non windows for writing
|
||||
inline int fopen_s(FILE** fp, const tstring& filename, const tchar* mode)
|
||||
inline int fopen_s(FILE** fp, const std::string& filename, const char* mode)
|
||||
{
|
||||
#if defined(WIN32)
|
||||
#if defined(SPDLOG_USE_WCHAR)
|
||||
*fp = _wfsopen((filename.c_str()), mode, _SH_DENYWR);
|
||||
#else
|
||||
#ifdef _WIN32
|
||||
*fp = _fsopen((filename.c_str()), mode, _SH_DENYWR);
|
||||
#endif
|
||||
return *fp == nullptr;
|
||||
#else
|
||||
*fp = fopen((filename.c_str()), mode);
|
||||
return *fp == nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int remove(const tchar* filename)
|
||||
{
|
||||
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
|
||||
return _wremove(filename);
|
||||
#else
|
||||
return std::remove(filename);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int rename(const tchar* filename1, const tchar* filename2)
|
||||
{
|
||||
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
|
||||
return _wrename(filename1, filename2);
|
||||
#else
|
||||
return std::rename(filename1, filename2);
|
||||
#endif
|
||||
}
|
||||
|
||||
//Return utc offset in minutes or -1 on failure
|
||||
@@ -215,4 +195,3 @@ inline size_t thread_id()
|
||||
} //spdlog
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -48,24 +48,24 @@ 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 tstring& 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 std::string& filename, size_t max_file_size, size_t max_files, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, S("txt"), max_file_size, max_files, force_flush);
|
||||
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, 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)
|
||||
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)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, S("txt"), max_file_size, max_files, force_flush);
|
||||
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "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 tstring& filename, int hour, int minute, bool force_flush)
|
||||
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)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, S("txt"), hour, minute, force_flush);
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", hour, minute, 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)
|
||||
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)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, S("txt"), hour, minute, force_flush);
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", hour, minute, force_flush);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user