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