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