mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
replaced flush_interval with auto_flush boolean
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
|
||||
// Helper class for file sink
|
||||
// When failing to open a file, retry several times(5) with small delay between the tries(10 ms)
|
||||
// Flush to file every X writes (or never if X==0)
|
||||
// Can be set to auto flush on every line
|
||||
// Throw spdlog_ex exception on errors
|
||||
|
||||
#include <cstdio>
|
||||
@@ -49,10 +49,10 @@ public:
|
||||
const int open_tries = 5;
|
||||
const int open_interval = 10;
|
||||
|
||||
explicit file_helper(const std::size_t flush_inverval):
|
||||
explicit file_helper(bool auto_flush):
|
||||
_fd(nullptr),
|
||||
_flush_inverval(flush_inverval),
|
||||
_flush_countdown(flush_inverval) {};
|
||||
_auto_flush(auto_flush)
|
||||
{};
|
||||
|
||||
file_helper(const file_helper&) = delete;
|
||||
file_helper& operator=(const file_helper&) = delete;
|
||||
@@ -104,11 +104,9 @@ public:
|
||||
if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size)
|
||||
throw spdlog_ex("Failed writing to file " + _filename);
|
||||
|
||||
if(--_flush_countdown == 0)
|
||||
{
|
||||
if(_auto_flush)
|
||||
std::fflush(_fd);
|
||||
_flush_countdown = _flush_inverval;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const std::string& filename() const
|
||||
@@ -133,8 +131,8 @@ public:
|
||||
private:
|
||||
FILE* _fd;
|
||||
std::string _filename;
|
||||
const std::size_t _flush_inverval;
|
||||
std::size_t _flush_countdown;
|
||||
bool _auto_flush;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@@ -39,24 +39,24 @@ inline std::shared_ptr<spdlog::logger> spdlog::get(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 std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval)
|
||||
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 auto_flush)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, flush_inverval);
|
||||
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, auto_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, size_t flush_inverval)
|
||||
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 auto_flush)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "txt", max_file_size, max_files, flush_inverval);
|
||||
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "txt", max_file_size, max_files, auto_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 std::string& filename, size_t flush_inverval)
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, bool auto_flush)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", flush_inverval);
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", auto_flush);
|
||||
}
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval)
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, bool auto_flush)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", flush_inverval);
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", auto_flush);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user