Livox SDK API  V2.2.0
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);
36  std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w;
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  const filename_t &filename() const
67  {
68  return file_helper_.filename();
69  }
70 
71 protected:
72  void sink_it_(const details::log_msg &msg) override
73  {
74 
75  if (msg.time >= rotation_tp_)
76  {
77  file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time)), truncate_);
78  rotation_tp_ = next_rotation_tp_();
79  }
80  fmt::memory_buffer formatted;
81  sink::formatter_->format(msg, formatted);
82  file_helper_.write(formatted);
83  }
84 
85  void flush_() override
86  {
87  file_helper_.flush();
88  }
89 
90 private:
91  tm now_tm(log_clock::time_point tp)
92  {
93  time_t tnow = log_clock::to_time_t(tp);
94  return spdlog::details::os::localtime(tnow);
95  }
96 
97  log_clock::time_point next_rotation_tp_()
98  {
99  auto now = log_clock::now();
100  tm date = now_tm(now);
101  date.tm_hour = rotation_h_;
102  date.tm_min = rotation_m_;
103  date.tm_sec = 0;
104  auto rotation_time = log_clock::from_time_t(std::mktime(&date));
105  if (rotation_time > now)
106  {
107  return rotation_time;
108  }
109  return {rotation_time + std::chrono::hours(24)};
110  }
111 
112  filename_t base_filename_;
113  int rotation_h_;
114  int rotation_m_;
115  log_clock::time_point rotation_tp_;
116  details::file_helper file_helper_;
117  bool truncate_;
118 };
119 
122 
123 } // namespace sinks
124 
125 //
126 // factory functions
127 //
128 template<typename Factory = default_factory>
129 inline std::shared_ptr<logger> daily_logger_mt(
130  const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
131 {
132  return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate);
133 }
134 
135 template<typename Factory = default_factory>
136 inline std::shared_ptr<logger> daily_logger_st(
137  const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
138 {
139  return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate);
140 }
141 } // namespace spdlog
spdlog::log_clock::time_point now() noexcept
Definition: os.h:60
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:50
std::tm localtime(const std::time_t &time_tt) noexcept
Definition: os.h:73
Definition: format.h:297
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
Definition: async.h:27
static std::tuple< filename_t, filename_t > split_by_extension(const spdlog::filename_t &fname)
Definition: file_helper.h:125
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
std::string filename_t
Definition: common.h:205
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)
const filename_t & filename() const
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)
#define SPDLOG_FILENAME_T(s)
Definition: os.h:369
void sink_it_(const details::log_msg &msg) override
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
std::string to_string(const T &value)
Definition: format.h:3209
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