LCOV - code coverage report
Current view: top level - snaplogger - options.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 171 0.6 %
Date: 2021-06-01 17:16:42 Functions: 2 4 50.0 %
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 Handle logger specific command line and other options.
      22             :  *
      23             :  * The logger supports a few options to override configuration files
      24             :  * and tweak settings from the command line. Since the user is in
      25             :  * control of the environment variable, we do not offer that option
      26             :  * here.
      27             :  */
      28             : 
      29             : 
      30             : // self
      31             : //
      32             : #include    "snaplogger/options.h"
      33             : 
      34             : #include    "snaplogger/logger.h"
      35             : #include    "snaplogger/map_diagnostic.h"
      36             : #include    "snaplogger/version.h"
      37             : 
      38             : 
      39             : // boost lib
      40             : //
      41             : #include    <boost/algorithm/string/replace.hpp>
      42             : 
      43             : 
      44             : // advgetopt lib
      45             : //
      46             : #include    <advgetopt/exception.h>
      47             : 
      48             : 
      49             : // cppthread lib
      50             : //
      51             : #include    <cppthread/log.h>
      52             : 
      53             : 
      54             : // last include
      55             : //
      56             : #include    <snapdev/poison.h>
      57             : 
      58             : 
      59             : 
      60             : namespace snaplogger
      61             : {
      62             : 
      63             : 
      64             : namespace
      65             : {
      66             : 
      67             : 
      68             : 
      69             : advgetopt::option const g_options[] =
      70             : {
      71             :     // DIRECT SELECT
      72             :     //
      73             :     advgetopt::define_option(
      74             :           advgetopt::Name("no-log")
      75             :         , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
      76             :         , advgetopt::Help("do not log anything.")
      77             :     ),
      78             :     advgetopt::define_option(
      79             :           advgetopt::Name("log-file")
      80             :         , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS
      81             :                                                   , advgetopt::GETOPT_FLAG_REQUIRED>())
      82             :         , advgetopt::Help("log data to this specific log files")
      83             :     ),
      84             :     advgetopt::define_option(
      85             :           advgetopt::Name("log-config")
      86             :         , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS
      87             :                                                   , advgetopt::GETOPT_FLAG_REQUIRED>())
      88             :         , advgetopt::Help("only load this specific configuration file.")
      89             :     ),
      90             :     advgetopt::define_option(
      91             :           advgetopt::Name("syslog")
      92             :         , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
      93             :         , advgetopt::Help("send the logs to syslog only, the argument, if specified, is the name to use as the identity.")
      94             :     ),
      95             :     advgetopt::define_option(
      96             :           advgetopt::Name("console")
      97             :         , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
      98             :         , advgetopt::Help("print the logs out to the console.")
      99             :     ),
     100             : 
     101             :     // ALTERNATIVE CONFIG FILES
     102             :     //
     103             :     advgetopt::define_option(
     104             :           advgetopt::Name("log-config-path")
     105             :         , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS
     106             :                                                   , advgetopt::GETOPT_FLAG_REQUIRED>())
     107             :         , advgetopt::Help("the path to the configuration folders.")
     108             :     ),
     109             : 
     110             :     // SEVERITY
     111             :     //
     112             :     advgetopt::define_option(
     113             :           advgetopt::Name("debug")
     114             :         , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
     115             :         , advgetopt::Help("change the severity level of each appender to DEBUG.")
     116             :     ),
     117             :     advgetopt::define_option(
     118             :           advgetopt::Name("trace")
     119             :         , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
     120             :         , advgetopt::Help("change the severity level of each appender to TRACE.")
     121             :     ),
     122             :     advgetopt::define_option(
     123             :           advgetopt::Name("log-severity")
     124             :         , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS
     125             :                             , advgetopt::GETOPT_FLAG_REQUIRED>())
     126             :         , advgetopt::Help("reduce the severity level of each appender to the specified level unless it is already lower.")
     127             :     ),
     128             :     advgetopt::define_option(
     129             :           advgetopt::Name("force-severity")
     130             :         , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_GROUP_OPTIONS
     131             :                             , advgetopt::GETOPT_FLAG_REQUIRED>())
     132             :         , advgetopt::Help("change the severity level of each appender to the specified level.")
     133             :     ),
     134             : 
     135             :     // FILTERS
     136             :     //
     137             :     advgetopt::define_option(
     138             :           advgetopt::Name("log-component")
     139             :         , advgetopt::Flags(advgetopt::command_flags<
     140             :                       advgetopt::GETOPT_FLAG_GROUP_OPTIONS
     141             :                     , advgetopt::GETOPT_FLAG_MULTIPLE
     142             :                     , advgetopt::GETOPT_FLAG_REQUIRED>())
     143             :         , advgetopt::Help("filter logs by component, use ! in front of a name to prevent those logs.")
     144             :     ),
     145             : 
     146             :     // COMMANDS
     147             :     //
     148             :     advgetopt::define_option(
     149             :           advgetopt::Name("logger-version")
     150             :         , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
     151             :         , advgetopt::Help("show the version of the logger library.")
     152             :     ),
     153             :     advgetopt::define_option(
     154             :           advgetopt::Name("logger-configuration-filenames")
     155             :         , advgetopt::Flags(advgetopt::standalone_command_flags<advgetopt::GETOPT_FLAG_GROUP_COMMANDS>())
     156             :         , advgetopt::Help("show the list of configuration filenames that would be loaded with the current options.")
     157             :     ),
     158             : 
     159             :     // END
     160             :     //
     161             :     advgetopt::end_options()
     162             : };
     163             : 
     164             : 
     165             : 
     166             : 
     167             : }
     168             : // no name namespace
     169             : 
     170             : 
     171           0 : void add_logger_options(advgetopt::getopt & opts)
     172             : {
     173           0 :     auto env(opts.get_options_environment());
     174           0 :     if(env.f_version != nullptr)
     175             :     {
     176           0 :         set_diagnostic(DIAG_KEY_VERSION, env.f_version);
     177             :     }
     178           0 :     if(env.f_build_date != nullptr)
     179             :     {
     180           0 :         set_diagnostic(DIAG_KEY_BUILD_DATE, env.f_build_date);
     181             :     }
     182           0 :     if(env.f_build_time != nullptr)
     183             :     {
     184           0 :         set_diagnostic(DIAG_KEY_BUILD_TIME, env.f_build_time);
     185             :     }
     186           0 :     set_diagnostic(DIAG_KEY_PROJECT_NAME, opts.get_project_name());
     187             : 
     188           0 :     opts.parse_options_info(g_options, true);
     189           0 : }
     190             : 
     191             : 
     192             : constexpr int const OPTION_NO_LOG           = 0x001;
     193             : constexpr int const OPTION_LOG_FILE         = 0x002;
     194             : constexpr int const OPTION_LOG_CONFIG       = 0x004;
     195             : constexpr int const OPTION_SYSLOG           = 0x008;
     196             : constexpr int const OPTION_CONSOLE          = 0x010;
     197             : 
     198             : constexpr int const OPTION_TRACE_SEVERITY   = 0x020;
     199             : constexpr int const OPTION_DEBUG_SEVERITY   = 0x040;
     200             : constexpr int const OPTION_LOG_SEVERITY     = 0x080;
     201             : constexpr int const OPTION_FORCE_SEVERITY   = 0x100;
     202             : 
     203             : 
     204           0 : bool process_logger_options(advgetopt::getopt & opts
     205             :                           , std::string const & config_path
     206             :                           , std::basic_ostream<char> & out)
     207             : {
     208           0 :     set_diagnostic(DIAG_KEY_PROGNAME, opts.get_program_name());
     209             : 
     210             :     // COMMANDS
     211             :     //
     212           0 :     if(opts.is_defined("logger-version"))
     213             :     {
     214           0 :         std::cout << snaplogger::get_version_string() << std::endl;
     215           0 :         throw advgetopt::getopt_exit("logger command processed.", 0);
     216             :     }
     217             : 
     218             :     // LOG CONFIG
     219             :     //
     220           0 :     int log_config(0);
     221           0 :     if(opts.is_defined("no-log"))
     222             :     {
     223           0 :         log_config |= OPTION_NO_LOG;
     224             :     }
     225           0 :     if(opts.is_defined("log-file"))
     226             :     {
     227           0 :         log_config |= OPTION_LOG_FILE;
     228             :     }
     229           0 :     if(opts.is_defined("log-config"))
     230             :     {
     231           0 :         log_config |= OPTION_LOG_CONFIG;
     232             :     }
     233           0 :     if(opts.is_defined("syslog"))
     234             :     {
     235           0 :         log_config |= OPTION_SYSLOG;
     236             :     }
     237           0 :     if(opts.is_defined("console"))
     238             :     {
     239           0 :         log_config |= OPTION_CONSOLE;
     240             :     }
     241             : 
     242           0 :     bool const show_logger_configuration_files(opts.is_defined("logger-configuration-filenames"));
     243           0 :     switch(log_config)
     244             :     {
     245           0 :     case 0:
     246             :         // defaults apply as normal
     247             : 
     248             :         {
     249           0 :             advgetopt::options_environment opt_env;
     250             : 
     251           0 :             std::string user_config("~/.config/");
     252           0 :             user_config += opts.get_project_name();
     253           0 :             user_config += "/logger";
     254           0 :             char const * config_dirs[] =
     255             :             {
     256             :                   "/usr/share/snaplogger/etc"
     257           0 :                 , config_path.c_str()
     258           0 :                 , user_config.c_str()
     259             :                 , nullptr
     260           0 :             };
     261             : 
     262           0 :             if(opts.is_defined("log-config-path"))
     263             :             {
     264           0 :                 config_dirs[0] = opts.get_string("log-config-path").c_str();
     265             :             }
     266             : 
     267           0 :             std::string const keep_project_name(opts.get_project_name());
     268           0 :             opt_env.f_project_name = keep_project_name.c_str();
     269           0 :             std::string const keep_group_name(opts.get_group_name());
     270           0 :             opt_env.f_group_name = keep_group_name.c_str();
     271           0 :             opt_env.f_environment_variable_name = "SNAPLOGGER";
     272             :             //opt_env.f_configuration_files = nullptr;
     273           0 :             opt_env.f_configuration_filename = "snaplogger.conf";
     274           0 :             opt_env.f_configuration_directories = config_dirs;
     275           0 :             opt_env.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_DYNAMIC_PARAMETERS;
     276             : 
     277           0 :             advgetopt::getopt system_opts(opt_env);
     278             : 
     279           0 :             if(show_logger_configuration_files)
     280             :             {
     281           0 :                 advgetopt::string_list_t list(system_opts.get_configuration_filenames(false, false));
     282           0 :                 out << "Logger common configuration filenames:" << std::endl;
     283           0 :                 for(auto n : list)
     284             :                 {
     285           0 :                     out << " . " << n << "\n";
     286             :                 }
     287             :             }
     288             : 
     289             :             // load the system configuration file first
     290             :             //
     291           0 :             system_opts.parse_configuration_files();
     292           0 :             if(opts.get_program_fullname().empty())
     293             :             {
     294             :                 // process environment variable now if no user filename
     295             :                 // is going to be loaded
     296             :                 //
     297           0 :                 system_opts.parse_environment_variable();
     298             :             }
     299           0 :             logger::get_instance()->set_config(system_opts);
     300             : 
     301           0 :             if(!opts.get_program_fullname().empty())
     302             :             {
     303             :                 // if we have a valid program name (non-empty) then try
     304             :                 // to load these configuration files
     305             :                 //
     306           0 :                 std::string filename(opts.get_program_name());
     307           0 :                 boost::replace_all(filename, "_", "-");
     308           0 :                 filename += ".conf";
     309           0 :                 opt_env.f_configuration_filename = filename.c_str();
     310           0 :                 advgetopt::getopt config_opts(opt_env);
     311             : 
     312           0 :                 if(show_logger_configuration_files)
     313             :                 {
     314           0 :                     advgetopt::string_list_t list(config_opts.get_configuration_filenames(false, false));
     315           0 :                     out << "Logger application configuration filenames:" << std::endl;
     316           0 :                     for(auto n : list)
     317             :                     {
     318           0 :                         out << " . " << n << "\n";
     319             :                     }
     320             :                 }
     321             : 
     322           0 :                 config_opts.parse_configuration_files();
     323           0 :                 config_opts.parse_environment_variable();
     324           0 :                 logger::get_instance()->set_config(config_opts);
     325           0 :             }
     326             :         }
     327           0 :         break;
     328             : 
     329           0 :     case OPTION_NO_LOG:
     330             :         // do nothing
     331           0 :         break;
     332             : 
     333           0 :     case OPTION_LOG_FILE:
     334           0 :         configure_file(opts.get_string("log-file"));
     335           0 :         break;
     336             : 
     337           0 :     case OPTION_LOG_CONFIG:
     338           0 :         configure_config(opts.get_string("log-config"));
     339           0 :         break;
     340             : 
     341           0 :     case OPTION_SYSLOG:
     342           0 :         configure_syslog(opts.get_string("syslog"));
     343           0 :         break;
     344             : 
     345           0 :     case OPTION_CONSOLE:
     346           0 :         configure_console();
     347           0 :         break;
     348             : 
     349           0 :     default:
     350           0 :         cppthread::log << cppthread::log_level_t::error
     351           0 :                        << "only one of --no-log, --log-file, --log-config, --syslog, --console can be used on your command line."
     352           0 :                        << cppthread::end;
     353           0 :         return false;
     354             : 
     355             :     }
     356             : 
     357           0 :     if(show_logger_configuration_files)
     358             :     {
     359           0 :         if(log_config != 0)
     360             :         {
     361           0 :             if(log_config == OPTION_LOG_CONFIG)
     362             :             {
     363           0 :                 out << "Logger application configuration filename:" << std::endl
     364           0 :                     << " . " << opts.get_string("log-config") << std::endl;
     365             :             }
     366             :             else
     367             :             {
     368           0 :                 out << "No logger application configuration filenames available with the current command line options." << std::endl;
     369             :             }
     370             :         }
     371           0 :         throw advgetopt::getopt_exit("logger command processed.", 0);
     372             :     }
     373             : 
     374             :     // SEVERITY
     375             :     //
     376           0 :     int severity_selection(0);
     377           0 :     if(opts.is_defined("trace"))
     378             :     {
     379           0 :         severity_selection |= OPTION_TRACE_SEVERITY;
     380             :     }
     381           0 :     if(opts.is_defined("debug"))
     382             :     {
     383           0 :         severity_selection |= OPTION_DEBUG_SEVERITY;
     384             :     }
     385           0 :     if(opts.is_defined("log-severity"))
     386             :     {
     387           0 :         severity_selection |= OPTION_LOG_SEVERITY;
     388             :     }
     389           0 :     if(opts.is_defined("force-severity"))
     390             :     {
     391           0 :         severity_selection |= OPTION_FORCE_SEVERITY;
     392             :     }
     393             : 
     394           0 :     switch(severity_selection)
     395             :     {
     396           0 :     case 0:
     397             :         // keep as is
     398           0 :         break;
     399             : 
     400           0 :     case OPTION_TRACE_SEVERITY:
     401           0 :         logger::get_instance()->reduce_severity(severity_t::SEVERITY_TRACE);
     402           0 :         configure_console(true);
     403           0 :         break;
     404             : 
     405           0 :     case OPTION_DEBUG_SEVERITY:
     406           0 :         logger::get_instance()->reduce_severity(severity_t::SEVERITY_DEBUG);
     407           0 :         configure_console(true);
     408           0 :         break;
     409             : 
     410           0 :     case OPTION_LOG_SEVERITY:
     411             :         {
     412           0 :             std::string const severity_name(opts.get_string("log-severity"));
     413           0 :             severity::pointer_t sev(get_severity(severity_name));
     414           0 :             if(sev == nullptr)
     415             :             {
     416           0 :                 cppthread::log << cppthread::log_level_t::error
     417           0 :                                << "unknown severity level \""
     418           0 :                                << severity_name
     419           0 :                                << "\"; please check your spelling."
     420           0 :                                << cppthread::end;
     421           0 :                 return false;
     422             :             }
     423           0 :             logger::get_instance()->reduce_severity(sev->get_severity());
     424             :         }
     425           0 :         break;
     426             : 
     427           0 :     case OPTION_FORCE_SEVERITY:
     428             :         {
     429           0 :             std::string const severity_name(opts.get_string("force-severity"));
     430           0 :             severity::pointer_t sev(get_severity(severity_name));
     431           0 :             if(sev == nullptr)
     432             :             {
     433           0 :                 cppthread::log << cppthread::log_level_t::error
     434           0 :                                << "unknown severity level \""
     435           0 :                                << severity_name
     436           0 :                                << "\"; please check your spelling."
     437           0 :                                << cppthread::end;
     438           0 :                 return false;
     439             :             }
     440           0 :             logger::get_instance()->set_severity(sev->get_severity());
     441             :         }
     442           0 :         break;
     443             : 
     444           0 :     default:
     445           0 :         cppthread::log << cppthread::log_level_t::error
     446           0 :                        << "only one of --debug, --log-severity, --force-severity can be used on your command line."
     447           0 :                        << cppthread::end;
     448           0 :         return false;
     449             : 
     450             :     }
     451             : 
     452             :     // FILTERS
     453             :     //
     454           0 :     if(opts.is_defined("log-component"))
     455             :     {
     456           0 :         size_t const max(opts.size("log-component"));
     457           0 :         for(size_t idx(0); idx < max; ++idx)
     458             :         {
     459           0 :             std::string log_component(opts.get_string("log-component", idx));
     460           0 :             if(!log_component.empty())
     461             :             {
     462           0 :                 if(log_component[0] == '!')
     463             :                 {
     464           0 :                     log_component = log_component.substr(1);
     465           0 :                     if(!log_component.empty())
     466             :                     {
     467           0 :                         component::pointer_t comp(get_component(log_component));
     468           0 :                         logger::get_instance()->add_component_to_ignore(comp);
     469             :                     }
     470             :                 }
     471             :                 else
     472             :                 {
     473           0 :                     component::pointer_t comp(get_component(log_component));
     474           0 :                     logger::get_instance()->add_component_to_include(comp);
     475             :                 }
     476             :             }
     477             :         }
     478             :     }
     479             : 
     480           0 :     SNAP_LOG_INFO
     481           0 :             << section(g_normal_component)
     482           0 :             << section(g_self_component)
     483             :             << "--------------------------------------------------"
     484             :             << SNAP_LOG_SEND;
     485           0 :     SNAP_LOG_INFO
     486           0 :             << section(g_normal_component)
     487           0 :             << section(g_self_component)
     488           0 :             << opts.get_project_name()
     489             :             << " v"
     490           0 :             << opts.get_options_environment().f_version
     491             :             << " started."
     492             :             << SNAP_LOG_SEND;
     493             : 
     494           0 :     return true;
     495             : }
     496             : 
     497             : 
     498           6 : } // snaplogger namespace
     499             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13