Line data Source code
1 : // Copyright (c) 2013-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snaplogger
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : #pragma once
20 :
21 : /** \file
22 : * \brief Handle the message generator.
23 : *
24 : * This file declares the base message class which is derived from an
25 : * std::stringstram. This allows you to use our logger with `<<`
26 : * to send anything that the `<<` operator understands to the logs.
27 : */
28 :
29 :
30 : // self
31 : //
32 : #include "snaplogger/component.h"
33 : #include "snaplogger/environment.h"
34 : #include "snaplogger/severity.h"
35 :
36 :
37 : // C++ lib
38 : //
39 : #include <sstream>
40 : #include <streambuf>
41 :
42 :
43 : // C lib
44 : //
45 : #include <sys/time.h>
46 :
47 :
48 :
49 : namespace snaplogger
50 : {
51 :
52 :
53 : class logger;
54 :
55 :
56 :
57 141009 : class null_buffer
58 : : public std::streambuf
59 : {
60 : public:
61 : typedef std::unique_ptr<null_buffer> pointer_t;
62 :
63 : virtual int overflow(int c) override;
64 : };
65 :
66 :
67 : enum class system_field_t
68 : {
69 : SYSTEM_FIELD_UNDEFINED = -1, // used for still undefined system fields
70 :
71 : SYSTEM_FIELD_MESSAGE,
72 : SYSTEM_FIELD_TIMESTAMP,
73 : SYSTEM_FIELD_SEVERITY,
74 : SYSTEM_FIELD_FILENAME,
75 : SYSTEM_FIELD_FUNCTION_NAME,
76 : SYSTEM_FIELD_LINE,
77 :
78 : SYSTEM_FIELD_max
79 : };
80 :
81 :
82 : typedef std::map<std::string, std::string> field_map_t;
83 :
84 :
85 : // the message class is final because the destructor does tricks which
86 : // would not work right if derived further
87 : //
88 : // also we do not offer a shared pointer because we expect the message
89 : // object to be created and immediately destroyed from the stack
90 : //
91 : class message final
92 : : public std::basic_stringstream<char>
93 : {
94 : public:
95 : typedef std::shared_ptr<message> pointer_t;
96 :
97 : message(
98 : severity_t sev = default_severity()
99 : , char const * file = nullptr
100 : , char const * func = nullptr
101 : , int line = -1);
102 : message(std::basic_stringstream<char> const & m, message const & msg);
103 : message(message const & rhs) = delete;
104 : virtual ~message();
105 :
106 : message & operator = (message const & rhs) = delete;
107 :
108 : static severity_t default_severity();
109 :
110 : void set_severity(severity_t severity);
111 : void set_filename(std::string const & filename);
112 : void set_function(std::string const & funcname);
113 : void set_line(int line);
114 : void set_recursive_message(bool state) const;
115 : bool can_add_component(component::pointer_t c) const;
116 : void add_component(component::pointer_t c);
117 : void add_field(std::string const & name, std::string const & value);
118 :
119 : std::shared_ptr<logger> get_logger() const;
120 : severity_t get_severity() const;
121 : void set_precise_time();
122 : timespec const & get_timestamp() const;
123 : std::string const & get_filename() const;
124 : std::string const & get_function() const;
125 : int get_line() const;
126 : bool get_recursive_message() const;
127 : bool has_component(component::pointer_t c) const;
128 : component::set_t const & get_components() const;
129 : environment::pointer_t get_environment() const;
130 : std::string get_message() const;
131 : static char const * get_system_field_name(system_field_t field);
132 : static system_field_t get_system_field_from_name(std::string const & name);
133 : std::string get_field(std::string const & name) const;
134 : field_map_t get_fields() const;
135 :
136 : private:
137 : std::shared_ptr<logger> f_logger = std::shared_ptr<logger>(); // make sure it does not go away under our feet
138 : timespec f_timestamp = timespec();
139 : severity_t f_severity = severity_t::SEVERITY_INFORMATION;
140 : std::string f_filename = std::string();
141 : std::string f_funcname = std::string();
142 : int f_line = 0;
143 : mutable bool f_recursive_message = false;
144 : environment::pointer_t f_environment = environment::pointer_t();
145 : component::set_t f_components = component::set_t();
146 : field_map_t f_fields = field_map_t();
147 : null_buffer::pointer_t f_null = null_buffer::pointer_t();
148 : std::streambuf * f_saved_buffer = nullptr;
149 : bool f_copy = false;
150 : };
151 :
152 :
153 : template<typename CharT, typename Traits>
154 : inline std::basic_ostream<CharT, Traits> &
155 0 : operator << (std::basic_ostream<CharT, Traits> & os, section_ptr sec)
156 : {
157 0 : message * m(dynamic_cast<message *>(&os));
158 0 : if(m == nullptr)
159 : {
160 0 : os << "(section:"
161 0 : << sec.f_component->get_name()
162 0 : << ")";
163 : }
164 : else
165 : {
166 0 : m->add_component(sec.f_component);
167 : }
168 0 : return os;
169 : }
170 :
171 :
172 : template<typename CharT, typename Traits>
173 : inline std::basic_ostream<CharT, Traits> &
174 5 : secure(std::basic_ostream<CharT, Traits> & os)
175 : {
176 5 : message * m(dynamic_cast<message *>(&os));
177 5 : if(m == nullptr)
178 : {
179 1 : os << "(section:secure)";
180 : }
181 : else
182 : {
183 4 : m->add_component(g_secure_component);
184 : }
185 5 : return os;
186 : }
187 :
188 :
189 : template<typename CharT, typename Traits>
190 : inline std::basic_ostream<CharT, Traits> &
191 1 : precise_time(std::basic_ostream<CharT, Traits> & os)
192 : {
193 1 : message * m(dynamic_cast<message *>(&os));
194 1 : if(m == nullptr)
195 : {
196 0 : m->set_precise_time();
197 : }
198 1 : return os;
199 : }
200 :
201 :
202 :
203 3 : struct field_t
204 : {
205 : std::string f_name;
206 : std::string f_value;
207 : };
208 :
209 3 : inline field_t field(std::string const & name, std::string const & value)
210 : {
211 3 : return { name, value };
212 : }
213 :
214 : template<typename CharT, typename Traits>
215 : inline std::basic_ostream<CharT, Traits> &
216 3 : operator << (std::basic_ostream<CharT, Traits> & os, field_t const & f)
217 : {
218 3 : message * m(dynamic_cast<message *>(&os));
219 3 : if(m != nullptr)
220 : {
221 3 : m->add_field(f.f_name, f.f_value);
222 : }
223 3 : return os;
224 : }
225 :
226 :
227 :
228 : void send_message(std::basic_ostream<char> & msg);
229 :
230 :
231 : // TODO: with C++20, change the __FILE__, __LINE__, __func__ with
232 : // std::location::current()
233 : //
234 : #define SNAP_LOG_FATAL ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_FATAL, __FILE__, __func__, __LINE__)
235 : #define SNAP_LOG_EMERG ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_EMERGENCY, __FILE__, __func__, __LINE__)
236 : #define SNAP_LOG_EMERGENCY ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_EMERGENCY, __FILE__, __func__, __LINE__)
237 : #define SNAP_LOG_ALERT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_ALERT, __FILE__, __func__, __LINE__)
238 : #define SNAP_LOG_CRIT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_CRITICAL, __FILE__, __func__, __LINE__)
239 : #define SNAP_LOG_CRITICAL ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_CRITICAL, __FILE__, __func__, __LINE__)
240 : #define SNAP_LOG_ERR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__)
241 : #define SNAP_LOG_ERROR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__)
242 : #define SNAP_LOG_RECOVERABLE_ERROR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_RECOVERABLE_ERROR, __FILE__, __func__, __LINE__)
243 : #define SNAP_LOG_MAJOR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_MAJOR, __FILE__, __func__, __LINE__)
244 : #define SNAP_LOG_WARN ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_WARNING, __FILE__, __func__, __LINE__)
245 : #define SNAP_LOG_WARNING ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_WARNING, __FILE__, __func__, __LINE__)
246 : #define SNAP_LOG_DEPRECATED ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_DEPRECATED, __FILE__, __func__, __LINE__)
247 : #define SNAP_LOG_MINOR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_MINOR, __FILE__, __func__, __LINE__)
248 : #define SNAP_LOG_IMPORTANT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_IMPORTANT, __FILE__, __func__, __LINE__)
249 : #define SNAP_LOG_INFO ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_INFORMATION, __FILE__, __func__, __LINE__)
250 : #define SNAP_LOG_INFORMATION ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_INFORMATION, __FILE__, __func__, __LINE__)
251 : #define SNAP_LOG_VERBOSE ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_VERBOSE, __FILE__, __func__, __LINE__)
252 : #define SNAP_LOG_UNIMPORTANT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_UNIMPORTANT, __FILE__, __func__, __LINE__)
253 : #define SNAP_LOG_NOTICE ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_NOTICE, __FILE__, __func__, __LINE__)
254 : #define SNAP_LOG_DEBUG ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_DEBUG, __FILE__, __func__, __LINE__)
255 : #define SNAP_LOG_TRACE ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_TRACE, __FILE__, __func__, __LINE__)
256 :
257 : #define SNAP_LOG_DEFAULT ::snaplogger::send_message((::snaplogger::message(::snaplogger::message::default_severity(), __FILE__, __func__, __LINE__)
258 :
259 : #define SNAP_LOG_FIELD(name, value) ::snaplogger::field((name), (value))
260 :
261 : // The (( are in the opening macros
262 : //
263 : #define SNAP_LOG_SEND ""))
264 : #define SNAP_LOG_SEND_SECURELY ::snaplogger::secure))
265 :
266 :
267 :
268 : } // snaplogger namespace
269 : // vim: ts=4 sw=4 et
|