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 :
22 : /** \file
23 : * \brief Appenders are used to append data to somewhere.
24 : *
25 : * This file declares the base appender class.
26 : */
27 :
28 : // self
29 : //
30 : #include "snaplogger/message.h"
31 :
32 : #include "snaplogger/exception.h"
33 : #include "snaplogger/logger.h"
34 :
35 :
36 : // C++ lib
37 : //
38 : #include <iostream>
39 :
40 :
41 : // C lib
42 : //
43 : #include <sys/time.h>
44 :
45 :
46 : // last include
47 : //
48 : #include <snapdev/poison.h>
49 :
50 :
51 :
52 : namespace snaplogger
53 : {
54 :
55 :
56 1077227 : int null_buffer::overflow(int c)
57 : {
58 1077227 : return c;
59 : }
60 :
61 :
62 :
63 :
64 :
65 94615 : message::message(
66 : severity_t sev
67 : , char const * file
68 : , char const * func
69 94615 : , int line)
70 : : f_logger(logger::get_instance())
71 : , f_severity(sev)
72 : , f_filename(file == nullptr ? std::string() : std::string(file))
73 : , f_funcname(func == nullptr ? std::string() : std::string(func))
74 : , f_line(line)
75 94615 : , f_environment(create_environment())
76 : {
77 94615 : clock_gettime(CLOCK_REALTIME_COARSE, &f_timestamp);
78 :
79 189230 : if(f_severity < f_logger->get_lowest_severity()
80 94615 : || f_severity == severity_t::SEVERITY_OFF)
81 : {
82 47163 : f_null.reset(new null_buffer);
83 47163 : std::ostream & ref = *this;
84 47163 : f_saved_buffer = ref.rdbuf(f_null.get());
85 : }
86 94615 : }
87 :
88 :
89 2 : message::message(std::basic_stringstream<char> const & m, message const & msg)
90 : : f_logger(msg.f_logger)
91 : , f_timestamp(msg.f_timestamp)
92 2 : , f_severity(msg.f_severity)
93 : , f_filename(msg.f_filename)
94 : , f_funcname(msg.f_funcname)
95 2 : , f_line(msg.f_line)
96 2 : , f_recursive_message(msg.f_recursive_message)
97 : , f_environment(msg.f_environment)
98 : , f_components(msg.f_components)
99 : , f_null(null_buffer::pointer_t())
100 : , f_saved_buffer(nullptr)
101 8 : , f_copy(true)
102 : {
103 2 : *this << m.rdbuf();
104 2 : }
105 :
106 :
107 189234 : message::~message()
108 : {
109 94617 : if(f_saved_buffer != nullptr)
110 : {
111 47163 : std::ostream & ref = *this;
112 47163 : ref.rdbuf(f_saved_buffer);
113 : }
114 94617 : }
115 :
116 :
117 29028 : void message::set_severity(severity_t severity)
118 : {
119 29028 : f_severity = severity;
120 29028 : }
121 :
122 :
123 1 : void message::set_filename(std::string const & filename)
124 : {
125 1 : f_filename = filename;
126 1 : }
127 :
128 :
129 1 : void message::set_function(std::string const & funcname)
130 : {
131 1 : f_funcname = funcname;
132 1 : }
133 :
134 :
135 1 : void message::set_line(int line)
136 : {
137 1 : f_line = line;
138 1 : }
139 :
140 :
141 30 : void message::set_recursive_message(bool state) const
142 : {
143 30 : f_recursive_message = state;
144 30 : }
145 :
146 :
147 4 : void message::add_component(component::pointer_t c)
148 : {
149 4 : if(c != nullptr)
150 : {
151 4 : f_components.insert(c);
152 : }
153 4 : }
154 :
155 :
156 48313 : std::shared_ptr<logger> message::get_logger() const
157 : {
158 48313 : return f_logger;
159 : }
160 :
161 :
162 94639 : severity_t message::get_severity() const
163 : {
164 94639 : return f_severity;
165 : }
166 :
167 :
168 4 : timespec const & message::get_timestamp() const
169 : {
170 4 : return f_timestamp;
171 : }
172 :
173 :
174 2 : std::string const & message::get_filename() const
175 : {
176 2 : return f_filename;
177 : }
178 :
179 :
180 2 : std::string const & message::get_function() const
181 : {
182 2 : return f_funcname;
183 : }
184 :
185 :
186 2 : int message::get_line() const
187 : {
188 2 : return f_line;
189 : }
190 :
191 :
192 48152 : bool message::get_recursive_message() const
193 : {
194 48152 : return f_recursive_message;
195 : }
196 :
197 :
198 142771 : component::set_t const & message::get_components() const
199 : {
200 142771 : return f_components;
201 : }
202 :
203 :
204 29 : environment::pointer_t message::get_environment() const
205 : {
206 29 : return f_environment;
207 : }
208 :
209 :
210 48150 : std::string message::get_message() const
211 : {
212 48150 : std::string s(str());
213 :
214 96300 : if(!s.empty()
215 48150 : && s.back() == '\n')
216 : {
217 4 : s.pop_back();
218 : }
219 :
220 96300 : if(!s.empty()
221 48150 : && s.back() == '\r')
222 : {
223 2 : s.pop_back();
224 : }
225 :
226 48150 : return s;
227 : }
228 :
229 :
230 94610 : void send_message(std::basic_ostream<char> & out)
231 : {
232 94610 : message * msg(dynamic_cast<message *>(&out));
233 94610 : if(msg == nullptr)
234 : {
235 1 : throw not_a_message("the 'out' parameter to the send_message() function is expected to be a snaplogger::message object.");
236 : }
237 :
238 94609 : logger::get_instance()->log_message(*msg);
239 94609 : }
240 :
241 :
242 :
243 :
244 6 : } // snaplogger namespace
245 : // vim: ts=4 sw=4 et
|