Line data Source code
1 : // Copyright (c) 2013-2022 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 :
20 : /** \file
21 : * \brief Implementation of the environment variable support.
22 : *
23 : * This file implements a variable which retrieves its value from the
24 : * process environment. For example, you could retrieve the path to
25 : * the HOME directory.
26 : *
27 : * This is often used to distinguish between runs.
28 : */
29 :
30 : // self
31 : //
32 : #include "snaplogger/exception.h"
33 : #include "snaplogger/variable.h"
34 :
35 :
36 : // last include
37 : //
38 : #include <snapdev/poison.h>
39 :
40 :
41 :
42 : namespace snaplogger
43 : {
44 :
45 :
46 : namespace
47 : {
48 :
49 :
50 7 : DEFINE_LOGGER_VARIABLE(env)
51 : {
52 0 : auto params(get_params());
53 0 : if(params.empty())
54 : {
55 0 : throw invalid_variable("the ${env:...} variable must have a \"name\" parameter.");
56 : }
57 0 : if(params[0]->get_name() != "name")
58 : {
59 0 : throw invalid_variable("the ${env:...} variable first parameter must be its \"name\" parameter.");
60 : }
61 0 : std::string const env_name(params[0]->get_value());
62 0 : if(env_name.empty())
63 : {
64 0 : throw invalid_variable("the ${env:name=...} variable first parameter cannot be empty.");
65 : }
66 0 : char const * env(getenv(env_name.c_str()));
67 0 : if(env != nullptr)
68 : {
69 0 : value += env;
70 : }
71 :
72 0 : variable::process_value(msg, value);
73 0 : }
74 :
75 :
76 : }
77 : // no name namespace
78 :
79 :
80 6 : } // snaplogger namespace
81 : // vim: ts=4 sw=4 et
|