source: trunk/GDE/SINA/builddir/include/spdlog/sinks/ostream_sink.h

Last change on this file was 19170, checked in by westram, 2 years ago
  • sina source
    • unpack + remove tarball
    • no longer ignore sina builddir.
File size: 1.2 KB
Line 
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
12#include "spdlog/details/null_mutex.h"
13#include "spdlog/sinks/base_sink.h"
14
15#include <mutex>
16#include <ostream>
17
18namespace spdlog {
19namespace sinks {
20template<typename Mutex>
21class ostream_sink final : public base_sink<Mutex>
22{
23public:
24    explicit ostream_sink(std::ostream &os, bool force_flush = false)
25        : ostream_(os)
26        , force_flush_(force_flush)
27    {
28    }
29    ostream_sink(const ostream_sink &) = delete;
30    ostream_sink &operator=(const ostream_sink &) = delete;
31
32protected:
33    void sink_it_(const details::log_msg &msg) override
34    {
35        fmt::memory_buffer formatted;
36        sink::formatter_->format(msg, formatted);
37        ostream_.write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
38        if (force_flush_)
39        {
40            ostream_.flush();
41        }
42    }
43
44    void flush_() override
45    {
46        ostream_.flush();
47    }
48
49    std::ostream &ostream_;
50    bool force_flush_;
51};
52
53using ostream_sink_mt = ostream_sink<std::mutex>;
54using ostream_sink_st = ostream_sink<details::null_mutex>;
55
56} // namespace sinks
57} // namespace spdlog
Note: See TracBrowser for help on using the repository browser.