LCOV - code coverage report
Current view: top level - snaplogger - environment.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 55 50.9 %
Date: 2019-08-13 00:35:33 Functions: 2 12 16.7 %
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 support.
      25             :  *
      26             :  * Whenever a log is emitted, an environment is attached to it. It is
      27             :  * very important since the environment of the appender (the one used
      28             :  * to format the message) may be different than the environment where
      29             :  * the message was emitted.
      30             :  *
      31             :  * Examples:
      32             :  *
      33             :  * 1. if you use asynchronous logging, then the tid changes, we use a
      34             :  * separate thread to work on the log so the tid is going to be different
      35             :  * from the tid of the emitter
      36             :  * 2. if you use the network feature, the appenders are going to be run
      37             :  * on a completely different computer through a log service which is
      38             :  * likely to have a different PID, UID, TID, domain name, hostname, etc.
      39             :  *
      40             :  * Environments are used through smart pointers, if one changes the
      41             :  * ones in existing messages do not change.
      42             :  */
      43             : 
      44             : // self
      45             : //
      46             : #include    "snaplogger/environment.h"
      47             : 
      48             : #include    "snaplogger/guard.h"
      49             : #include    "snaplogger/map_diagnostic.h"
      50             : #include    "snaplogger/private_logger.h"
      51             : 
      52             : 
      53             : // C lib
      54             : //
      55             : #include    <grp.h>
      56             : #include    <netdb.h>
      57             : #include    <pwd.h>
      58             : #include    <sys/param.h>
      59             : #include    <sys/sysinfo.h>
      60             : #include    <sys/types.h>
      61             : 
      62             : 
      63             : // last include
      64             : //
      65             : #include    <snapdev/poison.h>
      66             : 
      67             : 
      68             : 
      69             : namespace snaplogger
      70             : {
      71             : 
      72             : 
      73             : 
      74           1 : environment::environment(pid_t tid)
      75           1 :     : f_tid(tid)
      76             : {
      77           1 :     f_uid = getuid();
      78           1 :     f_pid = getpid();
      79           1 :     f_gid = getgid();
      80             : 
      81             :     char buf[1024];
      82             :     passwd pw;
      83           1 :     passwd * pw_ptr(nullptr);
      84           2 :     if(getpwuid_r(f_uid, &pw, buf, sizeof(buf), &pw_ptr) == 0
      85           1 :     && pw_ptr == &pw)
      86             :     {
      87           1 :         f_username = pw.pw_name;
      88             :     }
      89             : 
      90             :     group gr;
      91           1 :     group * gr_ptr(nullptr);
      92           2 :     if(getgrgid_r(f_gid, &gr, buf, sizeof(buf), &gr_ptr) == 0
      93           1 :     && gr_ptr == &gr)
      94             :     {
      95           1 :         f_groupname += gr.gr_name;
      96             :     }
      97             : 
      98             :     char host_buffer[HOST_NAME_MAX + 2];
      99           1 :     host_buffer[HOST_NAME_MAX + 1] = '\0'; // make sure it's null terminated
     100             : 
     101           1 :     if(gethostname(host_buffer, HOST_NAME_MAX + 1) == 0)
     102             :     {
     103           1 :         f_hostname = host_buffer;
     104             :     }
     105             : 
     106           1 :     if(getdomainname(host_buffer, HOST_NAME_MAX + 1) == 0)
     107             :     {
     108           1 :         f_domainname = host_buffer;
     109             :     }
     110             : 
     111           2 :     map_diagnostics_t const diag(get_map_diagnostics());
     112             : 
     113           1 :     auto const prog_it(diag.find("progname"));
     114           1 :     if(prog_it != diag.end())
     115             :     {
     116           1 :         f_progname = prog_it->second;
     117             :     }
     118             : 
     119           2 :     std::string const tid_str(std::to_string(tid));
     120           1 :     auto const thread_it(diag.find("threadname#" + tid_str));
     121           1 :     if(thread_it != diag.end())
     122             :     {
     123           0 :         f_threadname = thread_it->second;
     124             :     }
     125           1 : }
     126             : 
     127             : 
     128             : 
     129           0 : uid_t environment::get_uid() const
     130             : {
     131           0 :     return f_uid;
     132             : }
     133             : 
     134             : 
     135           0 : pid_t environment::get_pid() const
     136             : {
     137           0 :     return f_pid;
     138             : }
     139             : 
     140             : 
     141           0 : gid_t environment::get_gid() const
     142             : {
     143           0 :     return f_gid;
     144             : }
     145             : 
     146             : 
     147           0 : pid_t environment::get_tid() const
     148             : {
     149           0 :     return f_tid;
     150             : }
     151             : 
     152             : 
     153           0 : std::string environment::get_username() const
     154             : {
     155           0 :     guard g;
     156             : 
     157           0 :     return f_username;
     158             : }
     159             : 
     160             : 
     161           0 : std::string environment::get_groupname() const
     162             : {
     163           0 :     guard g;
     164             : 
     165           0 :     return f_groupname;
     166             : }
     167             : 
     168             : 
     169           0 : std::string environment::get_hostname() const
     170             : {
     171           0 :     guard g;
     172             : 
     173           0 :     return f_hostname;
     174             : }
     175             : 
     176             : 
     177           0 : std::string environment::get_domainname() const
     178             : {
     179           0 :     guard g;
     180             : 
     181           0 :     return f_domainname;
     182             : }
     183             : 
     184             : 
     185           0 : std::string environment::get_progname() const
     186             : {
     187           0 :     guard g;
     188             : 
     189           0 :     return f_progname;
     190             : }
     191             : 
     192             : 
     193           0 : std::string environment::get_threadname() const
     194             : {
     195           0 :     guard g;
     196             : 
     197           0 :     return f_threadname;
     198             : }
     199             : 
     200             : 
     201             : 
     202             : 
     203             : 
     204             : 
     205       65541 : environment::pointer_t create_environment()
     206             : {
     207       65541 :     return get_private_logger()->create_environment();
     208             : }
     209             : 
     210             : 
     211             : 
     212             : 
     213             : 
     214             : } // snaplogger namespace
     215             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12