Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
spdlog::sinks::rotating_file_sink< Mutex > Class Template Referencefinal

#include <rotating_file_sink.h>

Inheritance diagram for spdlog::sinks::rotating_file_sink< Mutex >:
spdlog::sinks::base_sink< Mutex > spdlog::sinks::sink

Public Member Functions

 rotating_file_sink (filename_t base_filename, std::size_t max_size, std::size_t max_files)
 
- Public Member Functions inherited from spdlog::sinks::base_sink< Mutex >
 base_sink ()=default
 
 base_sink (const base_sink &)=delete
 
base_sinkoperator= (const base_sink &)=delete
 
void log (const details::log_msg &msg) final
 
void flush () final
 
void set_pattern (const std::string &pattern) final
 
void set_formatter (std::unique_ptr< spdlog::formatter > sink_formatter) final
 
- Public Member Functions inherited from spdlog::sinks::sink
 sink ()
 
 sink (std::unique_ptr< spdlog::pattern_formatter > formatter)
 
virtual ~sink ()=default
 
bool should_log (level::level_enum msg_level) const
 
void set_level (level::level_enum log_level)
 
level::level_enum level () const
 

Static Public Member Functions

static filename_t calc_filename (const filename_t &filename, std::size_t index)
 

Protected Member Functions

void sink_it_ (const details::log_msg &msg) override
 
void flush_ () override
 
- Protected Member Functions inherited from spdlog::sinks::base_sink< Mutex >
virtual void set_pattern_ (const std::string &pattern)
 
virtual void set_formatter_ (std::unique_ptr< spdlog::formatter > sink_formatter)
 

Private Member Functions

void rotate_ ()
 
bool rename_file (const filename_t &src_filename, const filename_t &target_filename)
 

Private Attributes

filename_t base_filename_
 
std::size_t max_size_
 
std::size_t max_files_
 
std::size_t current_size_
 
details::file_helper file_helper_
 

Additional Inherited Members

- Protected Attributes inherited from spdlog::sinks::base_sink< Mutex >
Mutex mutex_
 
- Protected Attributes inherited from spdlog::sinks::sink
level_t level_
 
std::unique_ptr< spdlog::formatterformatter_
 

Detailed Description

template<typename Mutex>
class spdlog::sinks::rotating_file_sink< Mutex >

Definition at line 31 of file rotating_file_sink.h.

Constructor & Destructor Documentation

template<typename Mutex >
spdlog::sinks::rotating_file_sink< Mutex >::rotating_file_sink ( filename_t  base_filename,
std::size_t  max_size,
std::size_t  max_files 
)
inline

Definition at line 34 of file rotating_file_sink.h.

35  : base_filename_(std::move(base_filename))
36  , max_size_(max_size)
37  , max_files_(max_files)
38  {
40  current_size_ = file_helper_.size(); // expensive. called only once
41  }
void open(const filename_t &fname, bool truncate=false)
Definition: file_helper.h:42
static filename_t calc_filename(const filename_t &filename, std::size_t index)
def move(depos, offset)
Definition: depos.py:107

Member Function Documentation

template<typename Mutex >
static filename_t spdlog::sinks::rotating_file_sink< Mutex >::calc_filename ( const filename_t filename,
std::size_t  index 
)
inlinestatic

Definition at line 45 of file rotating_file_sink.h.

46  {
48  if (index != 0u)
49  {
50  filename_t basename, ext;
51  std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
52  fmt::format_to(w, SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
53  }
54  else
55  {
57  }
58  return fmt::to_string(w);
59  }
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
string filename
Definition: train.py:213
std::enable_if< is_contiguous< Container >::value &&internal::is_string< S >::value, std::back_insert_iterator< Container > >::type format_to(std::back_insert_iterator< Container > out, const S &format_str, const Args &...args)
Definition: core.h:1430
static std::tuple< filename_t, filename_t > split_by_extension(const spdlog::filename_t &fname)
Definition: file_helper.h:125
std::string filename_t
Definition: common.h:202
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
#define SPDLOG_FILENAME_T(s)
Definition: os.h:369
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
basic_memory_buffer< wchar_t > wmemory_buffer
Definition: format.h:554
template<typename Mutex >
void spdlog::sinks::rotating_file_sink< Mutex >::flush_ ( )
inlineoverrideprotectedvirtual

Implements spdlog::sinks::base_sink< Mutex >.

Definition at line 75 of file rotating_file_sink.h.

76  {
78  }
template<typename Mutex >
bool spdlog::sinks::rotating_file_sink< Mutex >::rename_file ( const filename_t src_filename,
const filename_t target_filename 
)
inlineprivate

Definition at line 119 of file rotating_file_sink.h.

120  {
121  // try to delete the target file in case it already exists.
122  (void)details::os::remove(target_filename);
123  return details::os::rename(src_filename, target_filename) == 0;
124  }
int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
Definition: os.h:178
int remove(const filename_t &filename) SPDLOG_NOEXCEPT
Definition: os.h:169
template<typename Mutex >
void spdlog::sinks::rotating_file_sink< Mutex >::rotate_ ( )
inlineprivate

Definition at line 86 of file rotating_file_sink.h.

87  {
90  for (auto i = max_files_; i > 0; --i)
91  {
94  {
95  continue;
96  }
98 
99  if (!rename_file(src, target))
100  {
101  // if failed try again after a small delay.
102  // this is a workaround to a windows issue, where very high rotation
103  // rates can cause the rename to fail with permission denied (because of antivirus?).
105  if (!rename_file(src, target))
106  {
107  file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
108  current_size_ = 0;
109  throw spdlog_ex(
110  "rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
111  }
112  }
113  }
114  file_helper_.reopen(true);
115  }
static filename_t calc_filename(const filename_t &filename, std::size_t index)
void reopen(bool truncate)
Definition: file_helper.h:60
int errno
Contains the last error code.
Definition: structcmd.h:53
std::string filename_t
Definition: common.h:202
bool rename_file(const filename_t &src_filename, const filename_t &target_filename)
static bool file_exists(const filename_t &fname)
Definition: file_helper.h:107
void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT
Definition: os.h:351
std::string filename_to_str(const filename_t &filename)
Definition: os.h:370
template<typename Mutex >
void spdlog::sinks::rotating_file_sink< Mutex >::sink_it_ ( const details::log_msg msg)
inlineoverrideprotectedvirtual

Implements spdlog::sinks::base_sink< Mutex >.

Definition at line 62 of file rotating_file_sink.h.

63  {
64  fmt::memory_buffer formatted;
65  sink::formatter_->format(msg, formatted);
66  current_size_ += formatted.size();
68  {
69  rotate_();
70  current_size_ = formatted.size();
71  }
72  file_helper_.write(formatted);
73  }
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void msg(const char *fmt,...)
Definition: message.cpp:107
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
void write(const fmt::memory_buffer &buf)
Definition: file_helper.h:83

Member Data Documentation

template<typename Mutex >
filename_t spdlog::sinks::rotating_file_sink< Mutex >::base_filename_
private

Definition at line 126 of file rotating_file_sink.h.

template<typename Mutex >
std::size_t spdlog::sinks::rotating_file_sink< Mutex >::current_size_
private

Definition at line 129 of file rotating_file_sink.h.

template<typename Mutex >
details::file_helper spdlog::sinks::rotating_file_sink< Mutex >::file_helper_
private

Definition at line 130 of file rotating_file_sink.h.

template<typename Mutex >
std::size_t spdlog::sinks::rotating_file_sink< Mutex >::max_files_
private

Definition at line 128 of file rotating_file_sink.h.

template<typename Mutex >
std::size_t spdlog::sinks::rotating_file_sink< Mutex >::max_size_
private

Definition at line 127 of file rotating_file_sink.h.


The documentation for this class was generated from the following file: