LCOV - code coverage report
Current view: top level - snaplogger - environment.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 55 60.0 %
Date: 2021-06-04 12:34:06 Functions: 4 12 33.3 %
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             : // C lib
      51             : //
      52             : #include    <grp.h>
      53             : #include    <netdb.h>
      54             : #include    <pwd.h>
      55             : #include    <sys/param.h>
      56             : #include    <sys/sysinfo.h>
      57             : #include    <sys/types.h>
      58             : 
      59             : 
      60             : // last include
      61             : //
      62             : #include    <snapdev/poison.h>
      63             : 
      64             : 
      65             : 
      66             : namespace snaplogger
      67             : {
      68             : 
      69             : 
      70             : 
      71           2 : environment::environment(pid_t tid)
      72           2 :     : f_tid(tid)
      73             : {
      74           2 :     f_uid = getuid();
      75           2 :     f_pid = getpid();
      76           2 :     f_gid = getgid();
      77             : 
      78             :     char buf[1024];
      79             :     passwd pw;
      80           2 :     passwd * pw_ptr(nullptr);
      81           4 :     if(getpwuid_r(f_uid, &pw, buf, sizeof(buf), &pw_ptr) == 0
      82           2 :     && pw_ptr == &pw)
      83             :     {
      84           2 :         f_username = pw.pw_name;
      85             :     }
      86             : 
      87             :     group gr;
      88           2 :     group * gr_ptr(nullptr);
      89           4 :     if(getgrgid_r(f_gid, &gr, buf, sizeof(buf), &gr_ptr) == 0
      90           2 :     && gr_ptr == &gr)
      91             :     {
      92           2 :         f_groupname += gr.gr_name;
      93             :     }
      94             : 
      95             :     char host_buffer[HOST_NAME_MAX + 2];
      96           2 :     host_buffer[HOST_NAME_MAX + 1] = '\0'; // make sure it's null terminated
      97             : 
      98           2 :     if(gethostname(host_buffer, HOST_NAME_MAX + 1) == 0)
      99             :     {
     100           2 :         f_hostname = host_buffer;
     101             :     }
     102             : 
     103           2 :     if(getdomainname(host_buffer, HOST_NAME_MAX + 1) == 0)
     104             :     {
     105           2 :         f_domainname = host_buffer;
     106             :     }
     107             : 
     108           4 :     map_diagnostics_t const diag(get_map_diagnostics());
     109             : 
     110           2 :     auto const prog_it(diag.find("progname"));
     111           2 :     if(prog_it != diag.end())
     112             :     {
     113           2 :         f_progname = prog_it->second;
     114             :     }
     115             : 
     116           4 :     std::string const tid_str(std::to_string(tid));
     117           2 :     auto const thread_it(diag.find("threadname#" + tid_str));
     118           2 :     if(thread_it != diag.end())
     119             :     {
     120           0 :         f_threadname = thread_it->second;
     121             :     }
     122           2 : }
     123             : 
     124             : 
     125             : 
     126           0 : uid_t environment::get_uid() const
     127             : {
     128           0 :     return f_uid;
     129             : }
     130             : 
     131             : 
     132           1 : pid_t environment::get_pid() const
     133             : {
     134           1 :     return f_pid;
     135             : }
     136             : 
     137             : 
     138           0 : gid_t environment::get_gid() const
     139             : {
     140           0 :     return f_gid;
     141             : }
     142             : 
     143             : 
     144           0 : pid_t environment::get_tid() const
     145             : {
     146           0 :     return f_tid;
     147             : }
     148             : 
     149             : 
     150           0 : std::string environment::get_username() const
     151             : {
     152           0 :     guard g;
     153             : 
     154           0 :     return f_username;
     155             : }
     156             : 
     157             : 
     158           0 : std::string environment::get_groupname() const
     159             : {
     160           0 :     guard g;
     161             : 
     162           0 :     return f_groupname;
     163             : }
     164             : 
     165             : 
     166          28 : std::string environment::get_hostname() const
     167             : {
     168          56 :     guard g;
     169             : 
     170          56 :     return f_hostname;
     171             : }
     172             : 
     173             : 
     174           0 : std::string environment::get_domainname() const
     175             : {
     176           0 :     guard g;
     177             : 
     178           0 :     return f_domainname;
     179             : }
     180             : 
     181             : 
     182           0 : std::string environment::get_progname() const
     183             : {
     184           0 :     guard g;
     185             : 
     186           0 :     return f_progname;
     187             : }
     188             : 
     189             : 
     190           0 : std::string environment::get_threadname() const
     191             : {
     192           0 :     guard g;
     193             : 
     194           0 :     return f_threadname;
     195             : }
     196             : 
     197             : 
     198             : 
     199             : 
     200             : 
     201             : 
     202       89028 : environment::pointer_t create_environment()
     203             : {
     204       89028 :     return get_private_logger()->create_environment();
     205             : }
     206             : 
     207             : 
     208             : 
     209             : 
     210             : 
     211             : } // snaplogger namespace
     212             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13