89f50f3d5f02344325894a77e1f0dab5c045ec1d
[platform/core/security/device-certificate-manager.git] / src / dcm-daemon / boost_log_dlog_sink.h
1 /******************************************************************
2  *
3  * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
4  *
5  * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef SHARED_BOOST_LOG_DLOG_SINK_H_
22 #define SHARED_BOOST_LOG_DLOG_SINK_H_
23
24 #include <boost/log/sinks.hpp>
25 #include <functional>
26
27 #include <dlog.h>
28
29 template<typename AttributeValueT = int> class dlog_direct_severity_mapping :
30         public boost::log::sinks::basic_direct_mapping<log_priority, AttributeValueT>
31 {
32         typedef  boost::log::sinks::basic_direct_mapping<log_priority, AttributeValueT> base_type;
33 public:
34         explicit dlog_direct_severity_mapping(boost::log::attribute_name const& name) :
35                 base_type(name)
36         {
37         }
38 };
39
40 template<typename AttributeValueT = int> class dlog_custom_severity_mapping :
41         public boost::log::sinks::basic_custom_mapping<log_priority, AttributeValueT>
42 {
43         typedef  boost::log::sinks::basic_custom_mapping<log_priority, AttributeValueT> base_type;
44 public:
45         explicit dlog_custom_severity_mapping(boost::log::attribute_name const& name) :
46                 base_type(name, DLOG_DEBUG)
47         {
48         }
49 };
50
51 class dlog_output_backend :
52         public boost::log::sinks::basic_formatted_sink_backend<char>
53 {
54         typedef boost::log::sinks::basic_formatted_sink_backend<char>   base_type;
55         typedef std::function< log_priority (boost::log::record_view const&) > severity_mapper_type;
56
57 private:
58         std::string                             log_domain_;
59         severity_mapper_type    level_mapper_;
60
61         inline void send(log_priority level, string_type const& formatted_message) {
62                 dlog_print(level, log_domain_.c_str(), "%s", formatted_message.c_str());
63         }
64
65 public:
66         dlog_output_backend()
67         {
68         }
69
70     void consume(boost::log::record_view const& rec, string_type const& formatted_message) {
71         if(!level_mapper_) {
72                 send(DLOG_DEBUG, formatted_message);
73         } else {
74                 send(level_mapper_(rec), formatted_message);
75         }
76     }
77
78         void set_log_domain(const std::string& name)
79         {
80                 log_domain_ = name;
81         }
82
83     void set_severity_mapper(severity_mapper_type const& mapper)
84     {
85         level_mapper_ = mapper;
86     }
87 };
88
89 #endif /* SHARED_BOOST_LOG_DLOG_SINK_H_ */