summaryrefslogtreecommitdiff
path: root/lib/spdlog/details/registry.h
blob: 614220d543549bc7439eb1acf3e99e44863cc75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

#pragma once

// Loggers registy of unique name->logger pointer
// An attempt to create a logger with an already existing name will be ignored
// If user requests a non existing logger, nullptr will be returned
// This class is thread safe

#include "../async_logger.h"
#include "../common.h"
#include "../details/null_mutex.h"
#include "../logger.h"

#include <chrono>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>

namespace spdlog {
namespace details {
template<class Mutex>
class registry_t
{
public:
    registry_t<Mutex>(const registry_t<Mutex> &) = delete;
    registry_t<Mutex> &operator=(const registry_t<Mutex> &) = delete;

    void register_logger(std::shared_ptr<logger> logger)
    {
        std::lock_guard<Mutex> lock(_mutex);
        auto logger_name = logger->name();
        throw_if_exists(logger_name);
        _loggers[logger_name] = logger;
    }

    std::shared_ptr<logger> get(const std::string &logger_name)
    {
        std::lock_guard<Mutex> lock(_mutex);
        auto found = _loggers.find(logger_name);
        return found == _loggers.end() ? nullptr : found->second;
    }

    template<class It>
    std::shared_ptr<logger> create(const std::string &logger_name, const It &sinks_begin, const It &sinks_end)
    {
        std::lock_guard<Mutex> lock(_mutex);
        throw_if_exists(logger_name);
        std::shared_ptr<logger> new_logger;
        if (_async_mode)
        {
            new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy,
                _worker_warmup_cb, _flush_interval_ms, _worker_teardown_cb);
        }
        else
        {
            new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
        }

        if (_formatter)
        {
            new_logger->set_formatter(_formatter);
        }

        if (_err_handler)
        {
            new_logger->set_error_handler(_err_handler);
        }

        new_logger->set_level(_level);
        new_logger->flush_on(_flush_level);

        // Add to registry
        _loggers[logger_name] = new_logger;
        return new_logger;
    }

    template<class It>
    std::shared_ptr<async_logger> create_async(const std::string &logger_name, size_t queue_size,
        const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
        const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb, const It &sinks_begin,
        const It &sinks_end)
    {
        std::lock_guard<Mutex> lock(_mutex);
        throw_if_exists(logger_name);
        auto new_logger = std::make_shared<async_logger>(
            logger_name, sinks_begin, sinks_end, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb);

        if (_formatter)
        {
            new_logger->set_formatter(_formatter);
        }

        if (_err_handler)
        {
            new_logger->set_error_handler(_err_handler);
        }

        new_logger->set_level(_level);
        new_logger->flush_on(_flush_level);

        // Add to registry
        _loggers[logger_name] = new_logger;
        return new_logger;
    }

    void apply_all(std::function<void(std::shared_ptr<logger>)> fun)
    {
        std::lock_guard<Mutex> lock(_mutex);
        for (auto &l : _loggers)
        {
            fun(l.second);
        }
    }

    void drop(const std::string &logger_name)
    {
        std::lock_guard<Mutex> lock(_mutex);
        _loggers.erase(logger_name);
    }

    void drop_all()
    {
        std::lock_guard<Mutex> lock(_mutex);
        _loggers.clear();
    }

    std::shared_ptr<logger> create(const std::string &logger_name, sinks_init_list sinks)
    {
        return create(logger_name, sinks.begin(), sinks.end());
    }

    std::shared_ptr<logger> create(const std::string &logger_name, sink_ptr sink)
    {
        return create(logger_name, {sink});
    }

    std::shared_ptr<async_logger> create_async(const std::string &logger_name, size_t queue_size,
        const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
        const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb, sinks_init_list sinks)
    {
        return create_async(
            logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, sinks.begin(), sinks.end());
    }

    std::shared_ptr<async_logger> create_async(const std::string &logger_name, size_t queue_size,
        const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
        const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb, sink_ptr sink)
    {
        return create_async(logger_name, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb, {sink});
    }

    void formatter(formatter_ptr f)
    {
        std::lock_guard<Mutex> lock(_mutex);
        _formatter = f;
        for (auto &l : _loggers)
        {
            l.second->set_formatter(_formatter);
        }
    }

    void set_pattern(const std::string &pattern)
    {
        std::lock_guard<Mutex> lock(_mutex);
        _formatter = std::make_shared<pattern_formatter>(pattern);
        for (auto &l : _loggers)
        {
            l.second->set_formatter(_formatter);
        }
    }

    void set_level(level::level_enum log_level)
    {
        std::lock_guard<Mutex> lock(_mutex);
        for (auto &l : _loggers)
        {
            l.second->set_level(log_level);
        }
        _level = log_level;
    }

    void flush_on(level::level_enum log_level)
    {
        std::lock_guard<Mutex> lock(_mutex);
        for (auto &l : _loggers)
        {
            l.second->flush_on(log_level);
        }
        _flush_level = log_level;
    }

    void set_error_handler(log_err_handler handler)
    {
        for (auto &l : _loggers)
        {
            l.second->set_error_handler(handler);
        }
        _err_handler = handler;
    }

    void set_async_mode(size_t q_size, const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
        const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb)
    {
        std::lock_guard<Mutex> lock(_mutex);
        _async_mode = true;
        _async_q_size = q_size;
        _overflow_policy = overflow_policy;
        _worker_warmup_cb = worker_warmup_cb;
        _flush_interval_ms = flush_interval_ms;
        _worker_teardown_cb = worker_teardown_cb;
    }

    void set_sync_mode()
    {
        std::lock_guard<Mutex> lock(_mutex);
        _async_mode = false;
    }

    static registry_t<Mutex> &instance()
    {
        static registry_t<Mutex> s_instance;
        return s_instance;
    }

private:
    registry_t<Mutex>() = default;

    void throw_if_exists(const std::string &logger_name)
    {
        if (_loggers.find(logger_name) != _loggers.end())
        {
            throw spdlog_ex("logger with name '" + logger_name + "' already exists");
        }
    }

    Mutex _mutex;
    std::unordered_map<std::string, std::shared_ptr<logger>> _loggers;
    formatter_ptr _formatter;
    level::level_enum _level = level::info;
    level::level_enum _flush_level = level::off;
    log_err_handler _err_handler;
    bool _async_mode = false;
    size_t _async_q_size = 0;
    async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
    std::function<void()> _worker_warmup_cb;
    std::chrono::milliseconds _flush_interval_ms{std::chrono::milliseconds::zero()};
    std::function<void()> _worker_teardown_cb;
};

#ifdef SPDLOG_NO_REGISTRY_MUTEX
using registry = registry_t<spdlog::details::null_mutex>;
#else
using registry = registry_t<std::mutex>;
#endif

} // namespace details
} // namespace spdlog