file_sinks.h 5.39 KB
Newer Older
gabime's avatar
gabime committed
1 2 3
#pragma once

#include <fstream>
gabime's avatar
gabime committed
4
#include <sstream>
gabime's avatar
gabime committed
5
#include  <iomanip>
6
#include "./base_sink.h"
7 8
#include <mutex>
#include "../details/null_mutex.h"
gabime's avatar
gabime committed
9
#include "../details/flush_helper.h"
gabime's avatar
gabime committed
10
#include "../details/blocking_queue.h"
gabime's avatar
gabime committed
11

12

13

gabime's avatar
gabime committed
14 15 16 17 18 19
namespace c11log
{
namespace sinks
{

/*
20
* Trivial file sink with single file as target
gabime's avatar
gabime committed
21
*/
22 23
template<class Mutex>
class simple_file_sink : public base_sink<Mutex>
gabime's avatar
gabime committed
24 25 26
{
public:
    explicit simple_file_sink(const std::string &filename,
27 28 29
                              const std::size_t flush_every=0):
        _ofstream(filename, std::ofstream::binary|std::ofstream::app),
        _flush_helper(flush_every)
gabime's avatar
gabime committed
30 31 32
    {
    }
protected:
gabime's avatar
gabime committed
33
    void _sink_it(const details::log_msg& msg) override
gabime's avatar
gabime committed
34
    {
35
        _flush_helper.write(msg, _ofstream);
gabime's avatar
gabime committed
36 37 38 39 40 41
    }
private:
    std::ofstream _ofstream;
    details::file_flush_helper _flush_helper;
};

42 43
typedef simple_file_sink<std::mutex> simple_file_sink_mt;
typedef simple_file_sink<details::null_mutex> simple_file_sink_st;
gabime's avatar
gabime committed
44 45

/*
46
 * Rotating file sink based on size
gabime's avatar
gabime committed
47
*/
48 49
template<class Mutex>
class rotating_file_sink : public base_sink<Mutex>
gabime's avatar
gabime committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
public:
    rotating_file_sink(const std::string &base_filename, const std::string &extension,
                       const std::size_t max_size, const std::size_t max_files,
                       const std::size_t flush_every=0):
        _base_filename(base_filename),
        _extension(extension),
        _max_size(max_size),
        _max_files(max_files),
        _current_size(0),
        _ofstream(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary),
        _flush_helper(flush_every)
    {
    }

protected:
gabime's avatar
gabime committed
66
    void _sink_it(const details::log_msg& msg) override
gabime's avatar
gabime committed
67
    {
68
        _current_size += msg.formatted.size();
gabime's avatar
gabime committed
69 70 71
        if (_current_size  > _max_size)
        {
            _rotate();
72
            _current_size = msg.formatted.size();
gabime's avatar
gabime committed
73
        }
74
        _flush_helper.write(msg, _ofstream);
gabime's avatar
gabime committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }


private:
    static std::string _calc_filename(const std::string& filename, std::size_t index, const std::string& extension)
    {
        std::ostringstream oss;
        if (index)
            oss << filename << "." << index << "." << extension;
        else
            oss << filename << "." << extension;
        return oss.str();
    }


gabi's avatar
gabi committed
90
    // Rotate files:
gabime's avatar
gabime committed
91
    // log.txt -> log.1.txt
gabi's avatar
gabi committed
92 93
    // ..
    // log.n.txt -> log.n+1.txt
94 95


gabime's avatar
gabime committed
96 97 98 99 100 101 102 103 104 105 106 107
    void _rotate()
    {
        _ofstream.close();
        //Remove oldest file
        for (auto i = _max_files; i > 0; --i)
        {
            auto src = _calc_filename(_base_filename, i - 1, _extension);
            auto target = _calc_filename(_base_filename, i, _extension);
            if (i == _max_files)
                std::remove(target.c_str());
            std::rename(src.c_str(), target.c_str());
        }
108
        _ofstream.open(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary);
gabime's avatar
gabime committed
109 110 111 112 113 114 115 116 117 118
    }
    std::string _base_filename;
    std::string _extension;
    std::size_t _max_size;
    std::size_t _max_files;
    std::size_t _current_size;
    std::ofstream _ofstream;
    details::file_flush_helper _flush_helper;
};

119 120 121
typedef rotating_file_sink<std::mutex> rotating_file_sink_mt;
typedef rotating_file_sink<details::null_mutex>rotating_file_sink_st;

gabime's avatar
gabime committed
122
/*
123
 * Rotating file sink based on date. rotates at midnight
gabime's avatar
gabime committed
124
 */
125 126
template<class Mutex>
class daily_file_sink:public base_sink<Mutex>
gabime's avatar
gabime committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
public:
    explicit daily_file_sink(const std::string& base_filename,
                             const std::string& extension,
                             const std::size_t flush_every=0):
        _base_filename(base_filename),
        _extension(extension),
        _midnight_tp (_calc_midnight_tp() ),
        _ofstream(_calc_filename(_base_filename, _extension), std::ofstream::binary|std::ofstream::app),
        _flush_helper(flush_every)
    {
    }

protected:
gabime's avatar
gabime committed
141
    void _sink_it(const details::log_msg& msg) override
gabime's avatar
gabime committed
142 143 144 145 146 147 148
    {
        if (std::chrono::system_clock::now() >= _midnight_tp)
        {
            _ofstream.close();
            _ofstream.open(_calc_filename(_base_filename, _extension));
            _midnight_tp = _calc_midnight_tp();
        }
149
        _flush_helper.write(msg, _ofstream);
gabime's avatar
gabime committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    }

private:
    // Return next midnight's time_point
    static std::chrono::system_clock::time_point _calc_midnight_tp()
    {
        using namespace std::chrono;
        auto now = system_clock::now();
        time_t tnow = std::chrono::system_clock::to_time_t(now);
        tm date = c11log::details::os::localtime(tnow);
        date.tm_hour = date.tm_min = date.tm_sec = 0;
        auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date));
        return system_clock::time_point(midnight + hours(24));
    }

    //Create filename for the form basename.YYYY-MM-DD.extension
    static std::string _calc_filename(const std::string& basename, const std::string& extension)
    {
        std::tm tm = c11log::details::os::localtime();
        std::ostringstream oss;
        oss << basename << '.';
        oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday;
        oss << '.' << extension;
        return oss.str();
    }

    std::string _base_filename;
    std::string _extension;
    std::chrono::system_clock::time_point _midnight_tp;
    std::ofstream _ofstream;
    details::file_flush_helper _flush_helper;

};
183 184 185

typedef daily_file_sink<std::mutex> daily_file_sink_mt;
typedef daily_file_sink<details::null_mutex> daily_file_sink_st;
gabime's avatar
gabime committed
186 187
}
}