LCOV - code coverage report
Current view: top level - snaplogger - system_variable.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 64 10.9 %
Date: 2019-08-13 00:35:33 Functions: 26 56 46.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.12