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 138660 : 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 : // the message class is final because the destructor does tricks which
83 : // would not work right if derived further
84 : //
85 : // also we do not offer a shared pointer because we expect the message
86 : // object to be created and immediately destroyed from the stack
87 : //
88 : class message final
89 : : public std::basic_stringstream<char>
90 : {
91 : public:
92 : typedef std::shared_ptr<message> pointer_t;
93 : typedef std::map<std::string, std::string> field_map_t;
94 :
95 : message(
96 : severity_t sev = default_severity()
97 : , char const * file = nullptr
98 : , char const * func = nullptr
99 : , int line = -1);
100 : message(std::basic_stringstream<char> const & m, message const & msg);
101 : message(message const & rhs) = delete;
102 : virtual ~message();
103 :
104 : message & operator = (message const & rhs) = delete;
105 :
106 : static severity_t default_severity();
107 :
108 : void set_severity(severity_t severity);
109 : void set_filename(std::string const & filename);
110 : void set_function(std::string const & funcname);
111 : void set_line(int line);
112 : void set_recursive_message(bool state) const;
113 : bool can_add_component(component::pointer_t c) const;
114 : void add_component(component::pointer_t c);
115 : void add_field(std::string const & name, std::string const & value);
116 :
117 : std::shared_ptr<logger> get_logger() const;
118 : severity_t get_severity() const;
119 : void set_precise_time();
120 : timespec const & get_timestamp() const;
121 : std::string const & get_filename() const;
122 : std::string const & get_function() const;
123 : int get_line() const;
124 : bool get_recursive_message() const;
125 : bool has_component(component::pointer_t c) const;
126 : component::set_t const & get_components() const;
127 : environment::pointer_t get_environment() const;
128 : std::string get_message() const;
129 : static char const * get_system_field_name(system_field_t field);
130 : static system_field_t get_system_field_from_name(std::string const & name);
131 : std::string get_field(std::string const & name) const;
132 : field_map_t get_fields() const;
133 :
134 : private:
135 : std::shared_ptr<logger> f_logger = std::shared_ptr<logger>(); // make sure it does not go away under our feet
136 : timespec f_timestamp = timespec();
137 : severity_t f_severity = severity_t::SEVERITY_INFORMATION;
138 : std::string f_filename = std::string();
139 : std::string f_funcname = std::string();
140 : int f_line = 0;
141 : mutable bool f_recursive_message = false;
142 : environment::pointer_t f_environment = environment::pointer_t();
143 : component::set_t f_components = component::set_t();
144 : field_map_t f_fields = field_map_t();
145 : null_buffer::pointer_t f_null = null_buffer::pointer_t();
146 : std::streambuf * f_saved_buffer = nullptr;
147 : bool f_copy = false;
148 : };
149 :
150 :
151 : template<typename CharT, typename Traits>
152 : inline std::basic_ostream<CharT, Traits> &
153 0 : operator << (std::basic_ostream<CharT, Traits> & os, section_ptr sec)
154 : {
155 0 : message * m(dynamic_cast<message *>(&os));
156 0 : if(m == nullptr)
157 : {
158 0 : os << "(section:"
159 0 : << sec.f_component->get_name()
160 0 : << ")";
161 : }
162 : else
163 : {
164 0 : m->add_component(sec.f_component);
165 : }
166 0 : return os;
167 : }
168 :
169 :
170 : template<typename CharT, typename Traits>
171 : inline std::basic_ostream<CharT, Traits> &
172 5 : secure(std::basic_ostream<CharT, Traits> & os)
173 : {
174 5 : message * m(dynamic_cast<message *>(&os));
175 5 : if(m == nullptr)
176 : {
177 1 : os << "(section:secure)";
178 : }
179 : else
180 : {
181 4 : m->add_component(g_secure_component);
182 : }
183 5 : return os;
184 : }
185 :
186 :
187 : template<typename CharT, typename Traits>
188 : inline std::basic_ostream<CharT, Traits> &
189 1 : precise_time(std::basic_ostream<CharT, Traits> & os)
190 : {
191 1 : message * m(dynamic_cast<message *>(&os));
192 1 : if(m == nullptr)
193 : {
194 0 : m->set_precise_time();
195 : }
196 1 : return os;
197 : }
198 :
199 :
200 :
201 3 : struct field_t
202 : {
203 : std::string f_name;
204 : std::string f_value;
205 : };
206 :
207 3 : inline field_t field(std::string const & name, std::string const & value)
208 : {
209 3 : return { name, value };
210 : }
211 :
212 : template<typename CharT, typename Traits>
213 : inline std::basic_ostream<CharT, Traits> &
214 3 : operator << (std::basic_ostream<CharT, Traits> & os, field_t const & f)
215 : {
216 3 : message * m(dynamic_cast<message *>(&os));
217 3 : if(m != nullptr)
218 : {
219 3 : m->add_field(f.f_name, f.f_value);
220 : }
221 3 : return os;
222 : }
223 :
224 :
225 :
226 : void send_message(std::basic_ostream<char> & msg);
227 :
228 :
229 : // TODO: with C++20, change the __FILE__, __LINE__, __func__ with
230 : // std::location::current()
231 : //
232 : #define SNAP_LOG_FATAL ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_FATAL, __FILE__, __func__, __LINE__)
233 : #define SNAP_LOG_EMERG ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_EMERGENCY, __FILE__, __func__, __LINE__)
234 : #define SNAP_LOG_EMERGENCY ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_EMERGENCY, __FILE__, __func__, __LINE__)
235 : #define SNAP_LOG_ALERT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_ALERT, __FILE__, __func__, __LINE__)
236 : #define SNAP_LOG_CRIT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_CRITICAL, __FILE__, __func__, __LINE__)
237 : #define SNAP_LOG_CRITICAL ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_CRITICAL, __FILE__, __func__, __LINE__)
238 : #define SNAP_LOG_ERR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__)
239 : #define SNAP_LOG_ERROR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__)
240 : #define SNAP_LOG_RECOVERABLE_ERROR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_RECOVERABLE_ERROR, __FILE__, __func__, __LINE__)
241 : #define SNAP_LOG_MAJOR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_MAJOR, __FILE__, __func__, __LINE__)
242 : #define SNAP_LOG_WARN ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_WARNING, __FILE__, __func__, __LINE__)
243 : #define SNAP_LOG_WARNING ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_WARNING, __FILE__, __func__, __LINE__)
244 : #define SNAP_LOG_DEPRECATED ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_DEPRECATED, __FILE__, __func__, __LINE__)
245 : #define SNAP_LOG_MINOR ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_MINOR, __FILE__, __func__, __LINE__)
246 : #define SNAP_LOG_IMPORTANT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_IMPORTANT, __FILE__, __func__, __LINE__)
247 : #define SNAP_LOG_INFO ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_INFORMATION, __FILE__, __func__, __LINE__)
248 : #define SNAP_LOG_INFORMATION ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_INFORMATION, __FILE__, __func__, __LINE__)
249 : #define SNAP_LOG_VERBOSE ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_VERBOSE, __FILE__, __func__, __LINE__)
250 : #define SNAP_LOG_UNIMPORTANT ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_UNIMPORTANT, __FILE__, __func__, __LINE__)
251 : #define SNAP_LOG_NOTICE ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_NOTICE, __FILE__, __func__, __LINE__)
252 : #define SNAP_LOG_DEBUG ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_DEBUG, __FILE__, __func__, __LINE__)
253 : #define SNAP_LOG_TRACE ::snaplogger::send_message((::snaplogger::message(::snaplogger::severity_t::SEVERITY_TRACE, __FILE__, __func__, __LINE__)
254 :
255 : #define SNAP_LOG_DEFAULT ::snaplogger::send_message((::snaplogger::message(::snaplogger::message::default_severity(), __FILE__, __func__, __LINE__)
256 :
257 : #define SNAP_LOG_FIELD(name, value) ::snaplogger::field((name), (value))
258 :
259 : // The (( are in the opening macros
260 : //
261 : #define SNAP_LOG_SEND ""))
262 : #define SNAP_LOG_SEND_SECURELY ::snaplogger::secure))
263 :
264 :
265 :
266 : } // snaplogger namespace
267 : // vim: ts=4 sw=4 et
|