Photon 1.0.0
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
16{
17 namespace sinks
18 {
19
20 //
21 // Rotating file sink based on size
22 //
23 template <typename Mutex>
24 class rotating_file_sink final : public base_sink<Mutex>
25 {
26 public:
27 rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers& event_handlers = {});
28 static filename_t calc_filename(const filename_t& filename, std::size_t index);
30
31 protected:
32 void sink_it_(const details::log_msg& msg) override;
33 void flush_() override;
34
35 private:
36 // Rotate files:
37 // log.txt -> log.1.txt
38 // log.1.txt -> log.2.txt
39 // log.2.txt -> log.3.txt
40 // log.3.txt -> delete
41 void rotate_();
42
43 // delete the target if exists, and rename the src file to target
44 // return true on success, false otherwise.
45 bool rename_file_(const filename_t& src_filename, const filename_t& target_filename);
46
48 std::size_t max_size_;
49 std::size_t max_files_;
50 std::size_t current_size_;
52 };
53
56
57 } // namespace sinks
58
59 //
60 // factory functions
61 //
62
63 template <typename Factory = spdlog::synchronous_factory>
64 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, bool rotate_on_open = false, const file_event_handlers& event_handlers = {})
65 {
66 return Factory::template create<sinks::rotating_file_sink_mt>(
67 logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
68 }
69
70 template <typename Factory = spdlog::synchronous_factory>
71 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, bool rotate_on_open = false, const file_event_handlers& event_handlers = {})
72 {
73 return Factory::template create<sinks::rotating_file_sink_st>(
74 logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
75 }
76} // namespace spdlog
77
78#ifdef SPDLOG_HEADER_ONLY
80#endif
Definition file_helper.h:19
Definition base_sink.h:22
Definition rotating_file_sink.h:25
bool rename_file_(const filename_t &src_filename, const filename_t &target_filename)
Definition rotating_file_sink-inl.h:143
std::size_t max_files_
Definition rotating_file_sink.h:49
filename_t base_filename_
Definition rotating_file_sink.h:47
void flush_() override
Definition rotating_file_sink-inl.h:97
std::size_t current_size_
Definition rotating_file_sink.h:50
filename_t filename()
Definition rotating_file_sink-inl.h:67
std::size_t max_size_
Definition rotating_file_sink.h:48
static filename_t calc_filename(const filename_t &filename, std::size_t index)
Definition rotating_file_sink-inl.h:54
void sink_it_(const details::log_msg &msg) override
Definition rotating_file_sink-inl.h:74
details::file_helper file_helper_
Definition rotating_file_sink.h:51
void rotate_()
Definition rotating_file_sink-inl.h:108
rotating_file_sink< details::null_mutex > rotating_file_sink_st
Definition rotating_file_sink.h:55
rotating_file_sink< std::mutex > rotating_file_sink_mt
Definition rotating_file_sink.h:54
Definition async.h:26
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, const file_event_handlers &event_handlers={})
Definition rotating_file_sink.h:71
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, const file_event_handlers &event_handlers={})
Definition rotating_file_sink.h:64
std::string filename_t
Definition common.h:131
Definition log_msg.h:14
Definition common.h:329