| 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/details/synchronous_factory.h> |
| 9 | #include <spdlog/sinks/base_sink.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 | public: |
| 22 | explicit basic_file_sink(const filename_t &filename, |
| 23 | bool truncate = false, |
| 24 | const file_event_handlers &event_handlers = {}); |
| 25 | const filename_t &filename() const; |
| 26 | |
| 27 | protected: |
| 28 | void sink_it_(const details::log_msg &msg) override; |
| 29 | void flush_() override; |
| 30 | |
| 31 | private: |
| 32 | details::file_helper file_helper_; |
| 33 | }; |
| 34 | |
| 35 | using basic_file_sink_mt = basic_file_sink<std::mutex>; |
| 36 | using basic_file_sink_st = basic_file_sink<details::null_mutex>; |
| 37 | |
| 38 | } // namespace sinks |
| 39 | |
| 40 | // |
| 41 | // factory functions |
| 42 | // |
| 43 | template <typename Factory = spdlog::synchronous_factory> |
| 44 | inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, |
| 45 | const filename_t &filename, |
| 46 | bool truncate = false, |
| 47 | const file_event_handlers &event_handlers = {}) { |
| 48 | return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate, |
| 49 | event_handlers); |
| 50 | } |
| 51 | |
| 52 | template <typename Factory = spdlog::synchronous_factory> |
| 53 | inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, |
| 54 | const filename_t &filename, |
| 55 | bool truncate = false, |
| 56 | const file_event_handlers &event_handlers = {}) { |
| 57 | return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate, |
| 58 | event_handlers); |
| 59 | } |
| 60 | |
| 61 | } // namespace spdlog |
| 62 | |
| 63 | #ifdef SPDLOG_HEADER_ONLY |
| 64 | #include "basic_file_sink-inl.h" |
| 65 | #endif |
| 66 | |