Livox SDK API  V2.2.0
basic_file_sink.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015-2018 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/sinks/base_sink.h"
15 
16 #include <mutex>
17 #include <string>
18 
19 namespace spdlog {
20 namespace sinks {
21 /*
22  * Trivial file sink with single file as target
23  */
24 template<typename Mutex>
25 class basic_file_sink final : public base_sink<Mutex>
26 {
27 public:
28  explicit basic_file_sink(const filename_t &filename, bool truncate = false)
29  {
30  file_helper_.open(filename, truncate);
31  }
32 
33  const filename_t &filename() const
34  {
35  return file_helper_.filename();
36  }
37 
38 protected:
39  void sink_it_(const details::log_msg &msg) override
40  {
41  fmt::memory_buffer formatted;
42  sink::formatter_->format(msg, formatted);
43  file_helper_.write(formatted);
44  }
45 
46  void flush_() override
47  {
48  file_helper_.flush();
49  }
50 
51 private:
52  details::file_helper file_helper_;
53 };
54 
57 
58 } // namespace sinks
59 
60 //
61 // factory functions
62 //
63 template<typename Factory = default_factory>
64 inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false)
65 {
66  return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate);
67 }
68 
69 template<typename Factory = default_factory>
70 inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false)
71 {
72  return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate);
73 }
74 
75 } // namespace spdlog
std::shared_ptr< logger > basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate=false)
void open(const filename_t &fname, bool truncate=false)
Definition: file_helper.h:42
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:50
void sink_it_(const details::log_msg &msg) override
Definition: async.h:27
const filename_t & filename() const
void write(const fmt::memory_buffer &buf)
Definition: file_helper.h:83
std::string filename_t
Definition: common.h:205
const filename_t & filename() const
Definition: file_helper.h:102
std::shared_ptr< logger > basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate=false)
basic_file_sink(const filename_t &filename, bool truncate=false)