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