logger.h 4.49 KB
Newer Older
gab's avatar
gab committed
1 2
#pragma once

gabi's avatar
gabi committed
3 4
// Thread safe logger
// Has log level and vector sinks which do the actual logging
gab's avatar
gab committed
5 6 7 8 9 10
#include<vector>
#include<memory>
#include<mutex>
#include<atomic>
#include <algorithm>

gabime's avatar
gabime committed
11
#include "common_types.h"
gab's avatar
gab committed
12 13 14
#include "sinks/base_sink.h"
#include "details/factory.h"

gabime's avatar
gabime committed
15
namespace c11log {
gabime's avatar
gabime committed
16 17


gabime's avatar
gabime committed
18
namespace details {
19 20
class line_logger;
}
gab's avatar
gab committed
21

gabime's avatar
gabime committed
22

gabime's avatar
gabime committed
23
class logger {
gab's avatar
gab committed
24 25
public:

gabime's avatar
gabime committed
26 27 28 29
    typedef std::shared_ptr<sinks::base_sink>  sink_ptr_t;
    typedef std::vector<sink_ptr_t> sinks_vector_t;

    explicit logger(const std::string& name) :
gabime's avatar
gabime committed
30 31 32 33 34
        _logger_name(name),
        _formatter(new formatters::default_formatter()),
        _sinks(),
        _mutex(),
        _atomic_level(level::INFO) {
gabime's avatar
gabime committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    }

    ~logger() = default;

    logger(const logger&) = delete;
    logger& operator=(const logger&) = delete;

    void set_name(const std::string& name);
    const std::string& get_name();
    void add_sink(sink_ptr_t sink_ptr);
    void remove_sink(sink_ptr_t sink_ptr);
    void set_formatter(std::unique_ptr<formatters::formatter> formatter);
    void set_level(c11log::level::level_enum level);
    c11log::level::level_enum get_level() const;
    bool should_log(c11log::level::level_enum level) const;

    details::line_logger log(level::level_enum level);
    details::line_logger debug();
    details::line_logger info();
    details::line_logger warn();
    details::line_logger error();
    details::line_logger fatal();
gab's avatar
gab committed
57 58

private:
gabime's avatar
gabime committed
59
    friend details::line_logger;
gab's avatar
gab committed
60

gabime's avatar
gabime committed
61 62 63 64 65
    std::string _logger_name = "";
    std::unique_ptr<c11log::formatters::formatter> _formatter;
    sinks_vector_t _sinks;
    std::mutex _mutex;
    std::atomic_int _atomic_level;
gab's avatar
gab committed
66

gabime's avatar
gabime committed
67
    void _log_it(const std::string& msg);
gab's avatar
gab committed
68 69 70 71

};

logger& get_logger(const std::string& name);
gabime's avatar
gabime committed
72 73 74 75




gab's avatar
gab committed
76 77
}

gabime's avatar
gabime committed
78 79


gabi's avatar
gabi committed
80 81 82
//
// Logger inline impl
//
gabime's avatar
gabime committed
83
#include "details/line_logger.h"
gab's avatar
gab committed
84 85 86
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{

gabime's avatar
gabime committed
87
    if (msg_level >= _atomic_level)
gabime's avatar
gabime committed
88 89 90
        return details::line_logger(this, msg_level);
    else
        return details::line_logger(nullptr);
gab's avatar
gab committed
91 92 93 94
}

inline c11log::details::line_logger c11log::logger::debug()
{
gabime's avatar
gabime committed
95
    return log(c11log::level::DEBUG);
gab's avatar
gab committed
96 97 98
}
inline c11log::details::line_logger c11log::logger::info()
{
gabime's avatar
gabime committed
99
    return log(c11log::level::INFO);
gab's avatar
gab committed
100 101 102
}
inline c11log::details::line_logger c11log::logger::warn()
{
gabime's avatar
gabime committed
103
    return log(c11log::level::WARNING);
gab's avatar
gab committed
104 105 106
}
inline c11log::details::line_logger c11log::logger::error()
{
gabime's avatar
gabime committed
107
    return log(level::ERROR);
gab's avatar
gab committed
108 109 110
}
inline c11log::details::line_logger c11log::logger::fatal()
{
gabime's avatar
gabime committed
111
    return log(c11log::level::FATAL);
gab's avatar
gab committed
112 113 114 115
}

inline void c11log::logger::set_name(const std::string& name)
{
gabime's avatar
gabime committed
116 117
    std::lock_guard<std::mutex> lock(_mutex);
    _logger_name = name;
gab's avatar
gab committed
118 119 120 121
}

inline const std::string& c11log::logger::get_name()
{
gabime's avatar
gabime committed
122 123
    std::lock_guard<std::mutex> lock(_mutex);
    return _logger_name;
gab's avatar
gab committed
124 125 126 127
}

inline void c11log::logger::add_sink(sink_ptr_t sink_ptr)
{
gabime's avatar
gabime committed
128 129
    std::lock_guard<std::mutex> lock(_mutex);
    _sinks.push_back(sink_ptr);
gab's avatar
gab committed
130 131 132 133
}

inline void c11log::logger::remove_sink(sink_ptr_t sink_ptr)
{
gabime's avatar
gabime committed
134 135
    std::lock_guard<std::mutex> lock(_mutex);
    _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
gab's avatar
gab committed
136 137 138 139
}

inline void c11log::logger::set_formatter(std::unique_ptr<formatters::formatter> formatter)
{
gabime's avatar
gabime committed
140 141
    std::lock_guard<std::mutex> lock(_mutex);
    _formatter = std::move(formatter);
gab's avatar
gab committed
142 143 144 145
}

inline void c11log::logger::set_level(c11log::level::level_enum level)
{
gabime's avatar
gabime committed
146
    _atomic_level.store(level);
gab's avatar
gab committed
147 148 149 150
}

inline c11log::level::level_enum c11log::logger::get_level() const
{
gabime's avatar
gabime committed
151
    return static_cast<c11log::level::level_enum>(_atomic_level.load());
gab's avatar
gab committed
152 153 154 155
}

inline bool c11log::logger::should_log(c11log::level::level_enum level) const
{
gabime's avatar
gabime committed
156
    return level >= _atomic_level.load();
gab's avatar
gab committed
157
}
gabime's avatar
gabime committed
158
inline void c11log::logger::_log_it(const std::string& msg)
gab's avatar
gab committed
159
{
gabime's avatar
gabime committed
160 161 162
    level::level_enum level = static_cast<level::level_enum>(_atomic_level.load());
    std::lock_guard<std::mutex> lock(_mutex);
    for (auto &sink : _sinks)
gabime's avatar
gabime committed
163
        sink->log(msg, level);
gab's avatar
gab committed
164 165
}

gabi's avatar
gabi committed
166
// Static factory function
gab's avatar
gab committed
167 168
inline c11log::logger& c11log::get_logger(const std::string& name)
{
gabime's avatar
gabime committed
169
    return *(c11log::details::factory::instance().get_logger(name));
gab's avatar
gab committed
170
}
gabime's avatar
gabime committed
171

gabime's avatar
gabime committed
172 173 174 175

namespace c11log {
class info_logger {
public:
gabime's avatar
gabime committed
176 177 178 179 180
    info_logger (c11log::logger* logger):_logger(logger) {}
    template<class T>
    details::line_logger& operator<<(const T& msg) {
        return _logger->info() << msg;
    }
gabime's avatar
gabime committed
181 182

private:
gabime's avatar
gabime committed
183
    c11log::logger* _logger;
gabime's avatar
gabime committed
184 185
};
}