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