Commit 2fa7410c authored by gabime's avatar gabime

Improve rotating sink error handling

parent 3771d129
// //
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //
#pragma once #pragma once
#include "spdlog/details/file_helper.h" #include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h" #include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <cerrno> #include <cerrno>
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <tuple> #include <tuple>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
// //
// Rotating file sink based on size // Rotating file sink based on size
// //
template<typename Mutex> template<typename Mutex>
class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex> class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{ {
public: public:
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files) rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files)
: base_filename_(std::move(base_filename)) : base_filename_(std::move(base_filename))
, max_size_(max_size) , max_size_(max_size)
, max_files_(max_files) , max_files_(max_files)
{ {
file_helper_.open(calc_filename(base_filename_, 0)); file_helper_.open(calc_filename(base_filename_, 0));
current_size_ = file_helper_.size(); // expensive. called only once current_size_ = file_helper_.size(); // expensive. called only once
} }
// calc filename according to index and file extension if exists. // calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". // e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
static filename_t calc_filename(const filename_t &filename, std::size_t index) static filename_t calc_filename(const filename_t &filename, std::size_t index)
{ {
typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w; typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w;
if (index != 0u) if (index != 0u)
{ {
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename); std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
fmt::format_to(w, SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext); fmt::format_to(w, SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
} }
else else
{ {
fmt::format_to(w, SPDLOG_FILENAME_T("{}"), filename); fmt::format_to(w, SPDLOG_FILENAME_T("{}"), filename);
} }
return fmt::to_string(w); return fmt::to_string(w);
} }
protected: protected:
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
sink::formatter_->format(msg, formatted); sink::formatter_->format(msg, formatted);
current_size_ += formatted.size(); current_size_ += formatted.size();
if (current_size_ > max_size_) if (current_size_ > max_size_)
{ {
rotate_(); rotate_();
current_size_ = formatted.size(); current_size_ = formatted.size();
} }
file_helper_.write(formatted); file_helper_.write(formatted);
} }
void flush_() override void flush_() override
{ {
file_helper_.flush(); file_helper_.flush();
} }
private: private:
// Rotate files: // Rotate files:
// log.txt -> log.1.txt // log.txt -> log.1.txt
// log.1.txt -> log.2.txt // log.1.txt -> log.2.txt
// log.2.txt -> log.3.txt // log.2.txt -> log.3.txt
// log.3.txt -> delete // log.3.txt -> delete
void rotate_() void rotate_()
{ {
using details::os::filename_to_str; using details::os::filename_to_str;
file_helper_.close(); file_helper_.close();
for (auto i = max_files_; i > 0; --i) for (auto i = max_files_; i > 0; --i)
{ {
filename_t src = calc_filename(base_filename_, i - 1); filename_t src = calc_filename(base_filename_, i - 1);
filename_t target = calc_filename(base_filename_, i); if (!details::file_helper::file_exists(src))
{
if (details::file_helper::file_exists(target)) continue;
{ }
if (details::os::remove(target) != 0) filename_t target = calc_filename(base_filename_, i);
{
throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target), errno); if (!rename_file(src, target))
} {
} // if failed try again after a small delay.
if (details::file_helper::file_exists(src) && details::os::rename(src, target) != 0) // this is a workaround to a windows issue, where very high rotation
{ // rates can cause the rename to fail with permission denied (because of antivirus?).
// if failed try again after small delay. details::os::sleep_for_millis(100);
// this is a workaround to a windows issue, where very high rotation if (!rename_file(src, target))
// rates sometimes fail (because of antivirus?). {
details::os::sleep_for_millis(20); file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
details::os::remove(target); throw spdlog_ex(
if (details::os::rename(src, target) != 0) "rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
{ }
throw spdlog_ex( }
"rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno); }
} file_helper_.reopen(true);
} }
}
file_helper_.reopen(true); // delete the target if exists, and rename the src file to target
} // return true on success, false otherwise.
bool rename_file(const filename_t &src_filename, const filename_t &target_filename)
filename_t base_filename_; {
std::size_t max_size_; // try to delete the target file in case it already exists.
std::size_t max_files_; (void)details::os::remove(target_filename);
std::size_t current_size_; return details::os::rename(src_filename, target_filename) == 0;
details::file_helper file_helper_; }
};
filename_t base_filename_;
using rotating_file_sink_mt = rotating_file_sink<std::mutex>; std::size_t max_size_;
using rotating_file_sink_st = rotating_file_sink<details::null_mutex>; std::size_t max_files_;
std::size_t current_size_;
} // namespace sinks details::file_helper file_helper_;
};
//
// factory functions using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
// using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
template<typename Factory = default_factory> } // namespace sinks
inline std::shared_ptr<logger> rotating_logger_mt(
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files) //
{ // factory functions
return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files); //
}
template<typename Factory = default_factory>
template<typename Factory = default_factory> inline std::shared_ptr<logger> rotating_logger_mt(
inline std::shared_ptr<logger> rotating_logger_st( const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files)
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files) {
{ return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files);
return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files); }
}
} // namespace spdlog template<typename Factory = default_factory>
inline std::shared_ptr<logger> rotating_logger_st(
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files)
{
return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files);
}
} // namespace spdlog
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