source: trunk/GDE/SINA/builddir/include/spdlog/details/async_logger_impl.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: 2.9 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// async logger implementation
9// uses a thread pool to perform the actual logging
10
11#include "spdlog/details/thread_pool.h"
12
13#include <chrono>
14#include <memory>
15#include <string>
16
17template<typename It>
18inline spdlog::async_logger::async_logger(
19    std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
20    : logger(std::move(logger_name), begin, end)
21    , thread_pool_(std::move(tp))
22    , overflow_policy_(overflow_policy)
23{
24}
25
26inline spdlog::async_logger::async_logger(
27    std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
28    : async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy)
29{
30}
31
32inline spdlog::async_logger::async_logger(
33    std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
34    : async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy)
35{
36}
37
38// send the log message to the thread pool
39inline void spdlog::async_logger::sink_it_(details::log_msg &msg)
40{
41#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
42    incr_msg_counter_(msg);
43#endif
44    if (auto pool_ptr = thread_pool_.lock())
45    {
46        pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
47    }
48    else
49    {
50        throw spdlog_ex("async log: thread pool doesn't exist anymore");
51    }
52}
53
54// send flush request to the thread pool
55inline void spdlog::async_logger::flush_()
56{
57    if (auto pool_ptr = thread_pool_.lock())
58    {
59        pool_ptr->post_flush(shared_from_this(), overflow_policy_);
60    }
61    else
62    {
63        throw spdlog_ex("async flush: thread pool doesn't exist anymore");
64    }
65}
66
67//
68// backend functions - called from the thread pool to do the actual job
69//
70inline void spdlog::async_logger::backend_log_(const details::log_msg &incoming_log_msg)
71{
72    try
73    {
74        for (auto &s : sinks_)
75        {
76            if (s->should_log(incoming_log_msg.level))
77            {
78                s->log(incoming_log_msg);
79            }
80        }
81    }
82    SPDLOG_CATCH_AND_HANDLE
83
84    if (should_flush_(incoming_log_msg))
85    {
86        backend_flush_();
87    }
88}
89
90inline void spdlog::async_logger::backend_flush_()
91{
92    try
93    {
94        for (auto &sink : sinks_)
95        {
96            sink->flush();
97        }
98    }
99    SPDLOG_CATCH_AND_HANDLE
100}
101
102inline std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
103{
104    auto cloned = std::make_shared<spdlog::async_logger>(std::move(new_name), sinks_.begin(), sinks_.end(), thread_pool_, overflow_policy_);
105
106    cloned->set_level(this->level());
107    cloned->flush_on(this->flush_level());
108    cloned->set_error_handler(this->error_handler());
109    return std::move(cloned);
110}
Note: See TracBrowser for help on using the repository browser.