spdlog
Loading...
Searching...
No Matches
rotating_file_sink.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
10
11#include <chrono>
12#include <mutex>
13#include <string>
14
15namespace spdlog {
16namespace sinks {
17
18//
19// Rotating file sink based on size
20//
21template<typename Mutex>
22class rotating_file_sink final : public base_sink<Mutex>
23{
24public:
25 rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false);
28
29protected:
30 void sink_it_(const details::log_msg &msg) override;
31 void flush_() override;
32
33private:
34 // Rotate files:
35 // log.txt -> log.1.txt
36 // log.1.txt -> log.2.txt
37 // log.2.txt -> log.3.txt
38 // log.3.txt -> delete
39 void rotate_();
40
41 // delete the target if exists, and rename the src file to target
42 // return true on success, false otherwise.
43 bool rename_file_(const filename_t &src_filename, const filename_t &target_filename);
44
50};
51
54
55} // namespace sinks
56
57//
58// factory functions
59//
60
61template<typename Factory = spdlog::synchronous_factory>
63 const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false)
64{
65 return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open);
66}
67
68template<typename Factory = spdlog::synchronous_factory>
70 const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false)
71{
72 return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files, rotate_on_open);
73}
74} // namespace spdlog
75
76#ifdef SPDLOG_HEADER_ONLY
78#endif
bool rename_file_(const filename_t &src_filename, const filename_t &target_filename)
static filename_t calc_filename(const filename_t &filename, std::size_t index)
void sink_it_(const details::log_msg &msg) override
Definition async.h:25
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, bool rotate_on_open=false)
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, bool rotate_on_open=false)