daily_file_sink.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #ifndef SPDLOG_H
9 #include "spdlog/spdlog.h"
10 #endif
11 
14 #include "spdlog/fmt/fmt.h"
15 #include "spdlog/sinks/base_sink.h"
16 
17 #include <chrono>
18 #include <cstdio>
19 #include <ctime>
20 #include <mutex>
21 #include <string>
22 
23 namespace spdlog {
24 namespace sinks {
25 
26 /*
27  * Generator of daily log file names in format basename.YYYY-MM-DD.ext
28  */
30 {
31  // Create filename for the form basename.YYYY-MM-DD
32  static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
33  {
34  filename_t basename, ext;
35  std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
38  w, SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext);
39  return fmt::to_string(w);
40  }
41 };
42 
43 /*
44  * Rotating file sink based on date. rotates at midnight
45  */
46 template<typename Mutex, typename FileNameCalc = daily_filename_calculator>
47 class daily_file_sink final : public base_sink<Mutex>
48 {
49 public:
50  // create daily file sink which rotates on given time
51  daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false)
52  : base_filename_(std::move(base_filename))
53  , rotation_h_(rotation_hour)
54  , rotation_m_(rotation_minute)
55  , truncate_(truncate)
56  {
57  if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
58  {
59  throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
60  }
61  auto now = log_clock::now();
62  file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_);
63  rotation_tp_ = next_rotation_tp_();
64  }
65 
66 protected:
67  void sink_it_(const details::log_msg &msg) override
68  {
69 
70  if (msg.time >= rotation_tp_)
71  {
72  file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time)), truncate_);
73  rotation_tp_ = next_rotation_tp_();
74  }
75  fmt::memory_buffer formatted;
76  sink::formatter_->format(msg, formatted);
77  file_helper_.write(formatted);
78  }
79 
80  void flush_() override
81  {
82  file_helper_.flush();
83  }
84 
85 private:
86  tm now_tm(log_clock::time_point tp)
87  {
88  time_t tnow = log_clock::to_time_t(tp);
89  return spdlog::details::os::localtime(tnow);
90  }
91 
92  log_clock::time_point next_rotation_tp_()
93  {
94  auto now = log_clock::now();
95  tm date = now_tm(now);
96  date.tm_hour = rotation_h_;
97  date.tm_min = rotation_m_;
98  date.tm_sec = 0;
99  auto rotation_time = log_clock::from_time_t(std::mktime(&date));
100  if (rotation_time > now)
101  {
102  return rotation_time;
103  }
104  return {rotation_time + std::chrono::hours(24)};
105  }
106 
110  log_clock::time_point rotation_tp_;
112  bool truncate_;
113 };
114 
117 
118 } // namespace sinks
119 
120 //
121 // factory functions
122 //
123 template<typename Factory = default_factory>
124 inline std::shared_ptr<logger> daily_logger_mt(
125  const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
126 {
127  return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate);
128 }
129 
130 template<typename Factory = default_factory>
131 inline std::shared_ptr<logger> daily_logger_st(
132  const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
133 {
134  return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate);
135 }
136 } // namespace spdlog
log_clock::time_point rotation_tp_
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
log_clock::time_point next_rotation_tp_()
STL namespace.
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
string filename
Definition: train.py:213
tm now_tm(log_clock::time_point tp)
Definition: async.h:27
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
details::file_helper file_helper_
def move(depos, offset)
Definition: depos.py:107
std::string filename_t
Definition: common.h:202
std::shared_ptr< logger > daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour=0, int minute=0, bool truncate=false)
std::shared_ptr< logger > daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour=0, int minute=0, bool truncate=false)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
#define SPDLOG_FILENAME_T(s)
Definition: os.h:369
void sink_it_(const details::log_msg &msg) override
std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT
Definition: os.h:73
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
basic_memory_buffer< wchar_t > wmemory_buffer
Definition: format.h:554
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate=false)
log_clock::time_point time
Definition: log_msg.h:43