snaplogger: /home/snapwebsites/snapcpp/contrib/snaplogger/snaplogger/variable.cpp Source File

variable.cpp
Go to the documentation of this file.
1 /*
2  * License:
3  * Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
4  *
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Authors:
23  * Alexis Wilke alexis@m2osw.com
24  */
25 
58 // self
59 //
60 #include "snaplogger/variable.h"
61 
62 #include "snaplogger/exception.h"
63 #include "snaplogger/guard.h"
65 
66 
67 // libutf8 lib
68 //
69 #include <libutf8/libutf8.h>
70 
71 
72 // C++ lib
73 //
74 #include <iostream>
75 #include <queue>
76 
77 
78 // last include
79 //
80 #include <snapdev/poison.h>
81 
82 
83 
84 namespace snaplogger
85 {
86 
87 
88 
89 namespace
90 {
91 
92 
93 
94 
95 
96 DEFINE_LOGGER_VARIABLE(direct)
97 {
98  snap::NOTUSED(msg);
99 
100  // apply all our parameters as is
101  //
102  auto const & params(get_params());
103  for(auto p : params)
104  {
105  value += p->get_value();
106  }
107 
108  // do NOT apply parameters further, the user has no access to those
109  // anyway; this is the direct text we find in between variables
110  //
111  //variable::process_value(msg, value);
112 }
113 
114 
115 
116 }
117 // no name namespace
118 
119 
120 
121 param::param(std::string const & name)
122  : f_name(name)
123 {
124 }
125 
126 
127 std::string const & param::get_name() const
128 {
129  return f_name;
130 }
131 
132 
133 param::type_t param::get_type() const
134 {
135  return f_type;
136 }
137 
138 
139 std::string param::get_value() const
140 {
141  if(f_type != type_t::TYPE_STRING)
142  {
143  // TBD: we may instead want to return the integer as a string
144  //
145  throw invalid_parameter(
146  "the ${...:"
147  + f_name
148  + "=<value>} parameter must be a valid string (not an integer).");
149  }
150  return f_value;
151 }
152 
153 
154 void param::set_value(std::string const & value)
155 {
156  f_value = value;
157  f_type = type_t::TYPE_STRING;
158 }
159 
160 
161 int64_t param::get_integer() const
162 {
163  if(f_type != type_t::TYPE_INTEGER)
164  {
165  // TBD: we may want to check whether the string represents a valid
166  // integer first and return that if so
167  //
168  throw invalid_parameter(
169  "the ${...:"
170  + f_name
171  + "=<value>} parameter must be a valid integer.");
172  }
173  return f_integer;
174 }
175 
176 
177 void param::set_integer(std::int64_t integer)
178 {
179  f_integer = integer;
180  f_type = type_t::TYPE_INTEGER;
181 }
182 
183 
184 
185 
186 
187 variable::~variable()
188 {
189 }
190 
191 
192 void variable::add_param(param::pointer_t p)
193 {
194  guard g;
195 
196  f_params.push_back(p);
197 }
198 
199 
200 param::vector_t variable::get_params() const
201 {
202  guard g;
203 
204  return f_params;
205 }
206 
207 
208 std::string variable::get_value(message const & msg) const
209 {
210  guard g;
211 
212  std::string value;
213  process_value(msg, value);
214  return value;
215 }
216 
217 
218 void variable::process_value(message const & msg, std::string & value) const
219 {
220  auto l(get_private_logger(msg));
221 
222  {
223  guard g;
224 
225  if(!l->has_functions())
226  {
227  // no functions available, we're done
228  return;
229  }
230  }
231 
232  function_data d;
233  d.set_value(value);
234 
235  for(auto p : f_params)
236  {
237  std::string const & name(p->get_name());
238  if(name.empty())
239  {
240  continue;
241  }
242 
243  auto func(l->get_function(name));
244  if(func != nullptr)
245  {
246  func->apply(msg, d, p);
247  }
248  // else -- ignore missing functions
249  }
250 
251  value = libutf8::to_u8string(d.get_value());
252 }
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 variable_factory::variable_factory(std::string const & type)
265  : f_type(type)
266 {
267 }
268 
269 
270 variable_factory::~variable_factory()
271 {
272 }
273 
274 
275 std::string const & variable_factory::get_type() const
276 {
277  return f_type;
278 }
279 
280 
281 
282 
283 void register_variable_factory(variable_factory::pointer_t factory)
284 {
285  get_private_logger()->register_variable_factory(factory);
286 }
287 
288 
289 
290 variable::pointer_t get_variable(std::string const & type)
291 {
292  return get_private_logger()->get_variable(type);
293 }
294 
295 
296 
297 
298 
299 
300 
301 
302 function::function(std::string const & function_name)
303  : f_name(function_name)
304 {
305 }
306 
307 
308 function::~function()
309 {
310 }
311 
312 
313 std::string const & function::get_name() const
314 {
315  return f_name;
316 }
317 
318 
319 
320 void register_function(function::pointer_t func)
321 {
322  guard g;
323 
324  get_private_logger()->register_function(func);
325 }
326 
327 
328 
329 
330 } // snaplogger namespace
331 // vim: ts=4 sw=4 et
Logger exceptions.
The declaration of the private logger.
Multi-thread compatibility.
Variables are used to dynamically add parameters to log messages.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly