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 | |
6 | #include <spdlog/details/file_helper.h> |
7 | #include <spdlog/details/null_mutex.h> |
8 | #include <spdlog/sinks/base_sink.h> |
9 | #include <spdlog/details/synchronous_factory.h> |
10 | |
11 | #include <mutex> |
12 | #include <string> |
13 | |
14 | namespace spdlog { |
15 | namespace sinks { |
16 | /* |
17 | * Trivial file sink with single file as target |
18 | */ |
19 | template<typename Mutex> |
20 | class basic_file_sink final : public base_sink<Mutex> |
21 | { |
22 | public: |
23 | explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {}); |
24 | const filename_t &filename() const; |
25 | |
26 | protected: |
27 | void sink_it_(const details::log_msg &msg) override; |
28 | void flush_() override; |
29 | |
30 | private: |
31 | details::file_helper file_helper_; |
32 | }; |
33 | |
34 | using basic_file_sink_mt = basic_file_sink<std::mutex>; |
35 | using basic_file_sink_st = basic_file_sink<details::null_mutex>; |
36 | |
37 | } // namespace sinks |
38 | |
39 | // |
40 | // factory functions |
41 | // |
42 | template<typename Factory = spdlog::synchronous_factory> |
43 | inline std::shared_ptr<logger> basic_logger_mt( |
44 | const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {}) |
45 | { |
46 | return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate, event_handlers); |
47 | } |
48 | |
49 | template<typename Factory = spdlog::synchronous_factory> |
50 | inline std::shared_ptr<logger> basic_logger_st( |
51 | const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {}) |
52 | { |
53 | return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate, event_handlers); |
54 | } |
55 | |
56 | } // namespace spdlog |
57 | |
58 | #ifdef SPDLOG_HEADER_ONLY |
59 | # include "basic_file_sink-inl.h" |
60 | #endif |
61 | |