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/map_diagnostic.h"
36 : #include "snaplogger/variable.h"
37 :
38 :
39 : // cppthread lib
40 : //
41 : #include <cppthread/thread.h>
42 :
43 :
44 : // C lib
45 : //
46 : #include <netdb.h>
47 : #include <sys/param.h>
48 :
49 :
50 : // last include
51 : //
52 : #include <snapdev/poison.h>
53 :
54 :
55 :
56 : namespace snaplogger
57 : {
58 :
59 :
60 : namespace
61 : {
62 :
63 :
64 111 : DEFINE_LOGGER_VARIABLE(hostname)
65 : {
66 56 : auto params(get_params());
67 56 : if(params.size() > 0
68 28 : && params[0]->get_name() == "running")
69 : {
70 : char host[HOST_NAME_MAX + 2];
71 0 : if(gethostname(host, HOST_NAME_MAX + 1) == 0)
72 : {
73 0 : host[HOST_NAME_MAX + 1] = '\0'; // make sure it's null terminated
74 0 : value += host;
75 : }
76 : }
77 : else
78 : {
79 28 : value += msg.get_environment()->get_hostname();
80 : }
81 :
82 28 : variable::process_value(msg, value);
83 23 : }
84 :
85 :
86 8 : DEFINE_LOGGER_VARIABLE(hostbyname)
87 : {
88 0 : auto params(get_params());
89 0 : if(params.empty())
90 : {
91 0 : throw invalid_variable("the ${hostbyname:...} variable must have a name parameter.");
92 : }
93 0 : if(params[0]->get_name() != "name")
94 : {
95 0 : throw invalid_variable("the ${hostbyname:...} variable first parameter must be its name parameter.");
96 : }
97 0 : auto hostname(params[0]->get_value());
98 0 : if(hostname.empty())
99 : {
100 0 : throw invalid_variable("the ${hostbyname:...} variable first parameter must be its non-empty name.");
101 : }
102 0 : hostent * h(gethostbyname(hostname.c_str()));
103 0 : if(h != nullptr)
104 : {
105 0 : value += h->h_name;
106 : }
107 : else
108 : {
109 0 : value += "<host " + std::string(h->h_name) + " not found>";
110 : }
111 :
112 0 : variable::process_value(msg, value);
113 0 : }
114 :
115 :
116 8 : DEFINE_LOGGER_VARIABLE(domainname)
117 : {
118 0 : auto params(get_params());
119 0 : if(params.size() > 0
120 0 : && params[0]->get_name() == "running")
121 : {
122 : char domain[HOST_NAME_MAX + 2];
123 0 : if(getdomainname(domain, HOST_NAME_MAX + 1) == 0)
124 : {
125 0 : domain[HOST_NAME_MAX + 1] = '\0'; // make sure it's null terminated
126 0 : value += domain;
127 : }
128 : }
129 : else
130 : {
131 0 : value += msg.get_environment()->get_domainname();
132 : }
133 :
134 0 : variable::process_value(msg, value);
135 0 : }
136 :
137 :
138 19 : DEFINE_LOGGER_VARIABLE(pid)
139 : {
140 4 : auto params(get_params());
141 4 : if(params.size() > 0
142 2 : && params[0]->get_name() == "running")
143 : {
144 1 : value += std::to_string(getpid());
145 : }
146 : else
147 : {
148 1 : value += std::to_string(msg.get_environment()->get_pid());
149 : }
150 :
151 2 : variable::process_value(msg, value);
152 2 : }
153 :
154 :
155 8 : DEFINE_LOGGER_VARIABLE(tid)
156 : {
157 0 : auto params(get_params());
158 0 : if(params.size() > 0
159 0 : && params[0]->get_name() == "running")
160 : {
161 0 : value += std::to_string(cppthread::gettid());
162 : }
163 : else
164 : {
165 0 : value += std::to_string(msg.get_environment()->get_tid());
166 : }
167 :
168 0 : variable::process_value(msg, value);
169 0 : }
170 :
171 :
172 8 : DEFINE_LOGGER_VARIABLE(threadname)
173 : {
174 0 : auto params(get_params());
175 0 : if(params.size() > 0
176 0 : && params[0]->get_name() == "running")
177 : {
178 : // we assume that the user has a map_diagnostic with the name
179 : // "threadname"; this is going to be automatic in our own snap_thread
180 : // implementation, any others would have to be done manually
181 : //
182 0 : map_diagnostics_t diag(get_map_diagnostics());
183 0 : std::string const tid(std::to_string(cppthread::gettid()));
184 0 : auto it(diag.find("threadname#" + tid));
185 0 : if(it != diag.end())
186 : {
187 0 : value += it->second;
188 : }
189 : }
190 : else
191 : {
192 0 : value += msg.get_environment()->get_threadname();
193 : }
194 :
195 0 : variable::process_value(msg, value);
196 0 : }
197 :
198 :
199 : }
200 : // no name namespace
201 :
202 :
203 6 : } // snaplogger namespace
204 : // vim: ts=4 sw=4 et
|