LCOV - code coverage report
Current view: top level - snaplogger - environment.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 38 63 60.3 %
Date: 2022-01-29 21:11:29 Functions: 4 13 30.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-2021  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snaplogger
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : 
      20             : /** \file
      21             :  * \brief Implementation of the environment support.
      22             :  *
      23             :  * Whenever a log is emitted, an environment is attached to it. It is
      24             :  * very important since the environment of the appender (the one used
      25             :  * to format the message) may be different than the environment where
      26             :  * the message was emitted.
      27             :  *
      28             :  * Examples:
      29             :  *
      30             :  * 1. if you use asynchronous logging, then the tid changes, we use a
      31             :  * separate thread to work on the log so the tid is going to be different
      32             :  * from the tid of the emitter
      33             :  * 2. if you use the network feature, the appenders are going to be run
      34             :  * on a completely different computer through a log service which is
      35             :  * likely to have a different PID, UID, TID, domain name, hostname, etc.
      36             :  *
      37             :  * Environments are used through smart pointers, if one changes the
      38             :  * ones in existing messages do not change.
      39             :  */
      40             : 
      41             : // self
      42             : //
      43             : #include    "snaplogger/environment.h"
      44             : 
      45             : #include    "snaplogger/guard.h"
      46             : #include    "snaplogger/map_diagnostic.h"
      47             : #include    "snaplogger/private_logger.h"
      48             : 
      49             : 
      50             : // cppthread lib
      51             : //
      52             : #include    <cppthread/thread.h>
      53             : 
      54             : 
      55             : // C lib
      56             : //
      57             : #include    <grp.h>
      58             : #include    <netdb.h>
      59             : #include    <pwd.h>
      60             : #include    <sys/param.h>
      61             : #include    <sys/sysinfo.h>
      62             : #include    <sys/types.h>
      63             : 
      64             : 
      65             : // last include
      66             : //
      67             : #include    <snapdev/poison.h>
      68             : 
      69             : 
      70             : 
      71             : namespace snaplogger
      72             : {
      73             : 
      74             : 
      75             : 
      76           2 : environment::environment(pid_t tid)
      77           2 :     : f_tid(tid)
      78             : {
      79           2 :     f_uid = getuid();
      80           2 :     f_pid = getpid();
      81           2 :     f_gid = getgid();
      82             : 
      83           2 :     char buf[1024];
      84           2 :     passwd pw;
      85           2 :     passwd * pw_ptr(nullptr);
      86           4 :     if(getpwuid_r(f_uid, &pw, buf, sizeof(buf), &pw_ptr) == 0
      87           2 :     && pw_ptr == &pw)
      88             :     {
      89           2 :         f_username = pw.pw_name;
      90             :     }
      91             : 
      92           2 :     group gr;
      93           2 :     group * gr_ptr(nullptr);
      94           4 :     if(getgrgid_r(f_gid, &gr, buf, sizeof(buf), &gr_ptr) == 0
      95           2 :     && gr_ptr == &gr)
      96             :     {
      97           2 :         f_groupname += gr.gr_name;
      98             :     }
      99             : 
     100           2 :     char host_buffer[HOST_NAME_MAX + 2];
     101           2 :     host_buffer[HOST_NAME_MAX + 1] = '\0'; // make sure it's null terminated
     102             : 
     103           2 :     if(gethostname(host_buffer, HOST_NAME_MAX + 1) == 0)
     104             :     {
     105           2 :         f_hostname = host_buffer;
     106             :     }
     107             : 
     108           2 :     if(getdomainname(host_buffer, HOST_NAME_MAX + 1) == 0)
     109             :     {
     110           2 :         f_domainname = host_buffer;
     111             :     }
     112             : 
     113           4 :     map_diagnostics_t const diag(get_map_diagnostics());
     114             : 
     115           2 :     auto const prog_it(diag.find("progname"));
     116           2 :     if(prog_it != diag.end())
     117             :     {
     118           2 :         f_progname = prog_it->second;
     119             :     }
     120             : 
     121           4 :     std::string const tid_str(std::to_string(tid));
     122           2 :     auto const thread_it(diag.find("threadname#" + tid_str));
     123           2 :     if(thread_it != diag.end())
     124             :     {
     125           0 :         f_threadname = thread_it->second;
     126             :     }
     127             : 
     128           2 :     f_boot_id = cppthread::get_boot_id();
     129           2 : }
     130             : 
     131             : 
     132             : 
     133           0 : uid_t environment::get_uid() const
     134             : {
     135           0 :     return f_uid;
     136             : }
     137             : 
     138             : 
     139           1 : pid_t environment::get_pid() const
     140             : {
     141           1 :     return f_pid;
     142             : }
     143             : 
     144             : 
     145           0 : gid_t environment::get_gid() const
     146             : {
     147           0 :     return f_gid;
     148             : }
     149             : 
     150             : 
     151           0 : pid_t environment::get_tid() const
     152             : {
     153           0 :     return f_tid;
     154             : }
     155             : 
     156             : 
     157           0 : std::string environment::get_username() const
     158             : {
     159           0 :     guard g;
     160             : 
     161           0 :     return f_username;
     162             : }
     163             : 
     164             : 
     165           0 : std::string environment::get_groupname() const
     166             : {
     167           0 :     guard g;
     168             : 
     169           0 :     return f_groupname;
     170             : }
     171             : 
     172             : 
     173          46 : std::string environment::get_hostname() const
     174             : {
     175          92 :     guard g;
     176             : 
     177          92 :     return f_hostname;
     178             : }
     179             : 
     180             : 
     181           0 : std::string environment::get_domainname() const
     182             : {
     183           0 :     guard g;
     184             : 
     185           0 :     return f_domainname;
     186             : }
     187             : 
     188             : 
     189           0 : std::string environment::get_progname() const
     190             : {
     191           0 :     guard g;
     192             : 
     193           0 :     return f_progname;
     194             : }
     195             : 
     196             : 
     197           0 : std::string environment::get_threadname() const
     198             : {
     199           0 :     guard g;
     200             : 
     201           0 :     return f_threadname;
     202             : }
     203             : 
     204             : 
     205           0 : std::string environment::get_boot_id() const
     206             : {
     207           0 :     guard g;
     208             : 
     209           0 :     return f_boot_id;
     210             : }
     211             : 
     212             : 
     213             : 
     214             : 
     215             : 
     216             : 
     217             : 
     218             : 
     219       97893 : environment::pointer_t create_environment()
     220             : {
     221       97893 :     return get_private_logger()->create_environment();
     222             : }
     223             : 
     224             : 
     225             : 
     226             : 
     227             : 
     228             : } // snaplogger namespace
     229             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13