Skip to content

Commit

Permalink
Merge pull request #523 from ApexAI/iox-#404-fix-verbose-output
Browse files Browse the repository at this point in the history
Iox #404 Fix verbose output and introduce CmdLineArgs_t struct
  • Loading branch information
mossmaurice authored Feb 5, 2021
2 parents a7291d8 + deff5a6 commit 2c0c504
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 162 deletions.
11 changes: 7 additions & 4 deletions iceoryx_examples/iceperf/roudi_main_static_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ int main(int argc, char* argv[])
static constexpr uint32_t ONE_MEGABYTE = 1024U * 1024;

iox::config::CmdLineParserConfigFileOption cmdLineParser;
cmdLineParser.parse(argc, argv);
auto cmdLineArgs = cmdLineParser.parse(argc, argv);
if (cmdLineArgs.has_error() && (cmdLineArgs.get_error() != iox::config::CmdLineParserResult::INFO_OUTPUT_ONLY))
{
iox::LogFatal() << "Unable to parse command line arguments!";
return EXIT_FAILURE;
}

iox::RouDiConfig_t roudiConfig;
// roudiConfig.setDefaults(); can be used if you want to use the default config only.
Expand Down Expand Up @@ -61,9 +66,7 @@ int main(int argc, char* argv[])
/// mempoolConfig})
/// @endcode

cmdLineParser.printParameters();

IceOryxRouDiApp roudi(cmdLineParser, roudiConfig);
IceOryxRouDiApp roudi(cmdLineArgs.value(), roudiConfig);

return roudi.run();
}
59 changes: 59 additions & 0 deletions iceoryx_posh/include/iceoryx_posh/roudi/cmd_line_args.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef IOX_POSH_ROUDI_CMD_LINE_ARGS_HPP
#define IOX_POSH_ROUDI_CMD_LINE_ARGS_HPP

#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/version/compatibility_check_level.hpp"
#include "iceoryx_utils/log/logstream.hpp"

#include <cstdint>

namespace iox
{
namespace config
{
struct CmdLineArgs_t
{
roudi::MonitoringMode monitoringMode{roudi::MonitoringMode::ON};
iox::log::LogLevel logLevel{iox::log::LogLevel::kWarn};
version::CompatibilityCheckLevel compatibilityCheckLevel{version::CompatibilityCheckLevel::PATCH};
units::Duration processKillDelay{roudi::PROCESS_DEFAULT_KILL_DELAY};
cxx::optional<uint16_t> uniqueRouDiId{cxx::nullopt};
bool run{true};
roudi::ConfigFilePathString_t configFilePath;
};

inline iox::log::LogStream& operator<<(iox::log::LogStream& logstream, const CmdLineArgs_t& cmdLineArgs) noexcept
{
logstream << "Log level: " << cmdLineArgs.logLevel << "\n";
logstream << "Monitoring mode: " << cmdLineArgs.monitoringMode << "\n";
logstream << "Compatibility check level: " << cmdLineArgs.compatibilityCheckLevel << "\n";
cmdLineArgs.uniqueRouDiId.and_then([&logstream](auto& id) { logstream << "Unique RouDi ID: " << id << "\n"; })
.or_else([&logstream] { logstream << "Unique RouDi ID: < unset >\n"; });
logstream << "Process kill delay: " << cmdLineArgs.processKillDelay.seconds() << " s\n";
if (!cmdLineArgs.configFilePath.empty())
{
logstream << "Config file used is: " << cmdLineArgs.configFilePath;
}
else
{
logstream << "Config file used is: < none >";
}
return logstream;
}
} // namespace config
} // namespace iox

#endif // IOX_POSH_ROUDI_CMD_LINE_ARGS_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IceOryxRouDiApp : public RouDiApp
/// @brief constructor to create the RouDi daemon with a given config
/// @param[in] Command liner parser object, that provides the settings
/// @param[in] RouDi config for mempool configuration
IceOryxRouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& roudiConfig) noexcept;
IceOryxRouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& roudiConfig) noexcept;

/// @brief starts the execution of the RouDi daemon
/// @return Return code for programm execution
Expand Down
21 changes: 2 additions & 19 deletions iceoryx_posh/include/iceoryx_posh/roudi/roudi_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

#include "iceoryx_posh/iceoryx_posh_config.hpp"
#include "iceoryx_posh/mepoo/mepoo_config.hpp"
#include "iceoryx_posh/roudi/roudi_cmd_line_parser.hpp"
#include "iceoryx_posh/roudi/roudi_config_file_provider.hpp"
#include "iceoryx_posh/version/compatibility_check_level.hpp"
#include "iceoryx_posh/roudi/cmd_line_args.hpp"
#include "iceoryx_utils/log/logcommon.hpp"
#include "iceoryx_utils/posix_wrapper/semaphore.hpp"

Expand All @@ -39,7 +37,7 @@ class RouDiApp
/// @brief C'tor with command line parser, which has already parsed the command line parameters
/// @param[in] cmdLineParser reference to a command line parser object
/// @param[in] config the configuration to use
RouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& config) noexcept;
RouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& config) noexcept;

virtual ~RouDiApp() noexcept {};

Expand All @@ -48,24 +46,9 @@ class RouDiApp
virtual uint8_t run() noexcept = 0;

protected:
/// @brief this is needed for the child classes for custom CmdLineParser
/// @param[in] config the configuration to use
RouDiApp(const RouDiConfig_t& config) noexcept;

/// @brief Tells the OS which signals shall be hooked
void registerSigHandler() noexcept;

void parseCmdLineArguments(int argc,
char* argv[],
config::CmdLineParser::CmdLineArgumentParsingMode cmdLineParsingMode =
config::CmdLineParser::CmdLineArgumentParsingMode::ALL) noexcept;

/// @brief Extracts from CmdLineParser and sets them
void setCmdLineParserResults(const config::CmdLineParser& cmdLineParser) noexcept;

/// @brief initialize the RouDi daemon
void init() noexcept;

/// @brief waits for the next signal to RouDi daemon
bool waitForSignal() const noexcept;

Expand Down
25 changes: 13 additions & 12 deletions iceoryx_posh/include/iceoryx_posh/roudi/roudi_cmd_line_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define IOX_POSH_ROUDI_ROUDI_CMD_LINE_PARSER_HPP

#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/roudi/cmd_line_args.hpp"
#include "iceoryx_posh/version/compatibility_check_level.hpp"
#include "iceoryx_utils/cxx/expected.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
Expand All @@ -25,6 +26,12 @@ namespace iox
{
namespace config
{
enum class CmdLineParserResult
{
UNKNOWN_OPTION_USED,
INFO_OUTPUT_ONLY /// @todo use this instead of CmdLineArgs_t.run after modularisation of RouDi
};

class CmdLineParser
{
public:
Expand All @@ -45,18 +52,12 @@ class CmdLineParser
/// @param[in] argc forwarding of command line arguments
/// @param[in] argv forwarding of command line arguments
/// @param[in] cmdLineParsingMode selects to parse a single option or all options
virtual void parse(int argc,
char* argv[],
const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept;

void printParameters() const noexcept;

bool getRun() const noexcept;
iox::log::LogLevel getLogLevel() const noexcept;
roudi::MonitoringMode getMonitoringMode() const noexcept;
version::CompatibilityCheckLevel getCompatibilityCheckLevel() const noexcept;
cxx::optional<uint16_t> getUniqueRouDiId() const noexcept;
units::Duration getProcessKillDelay() const noexcept;
/// @param[out] Result wrapped in an cxx::expected, either the parsed arguments as CmdLineArgs_t struct or
/// CmdLineParserResult
virtual cxx::expected<CmdLineArgs_t, CmdLineParserResult>
parse(int argc,
char* argv[],
const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept;

protected:
bool m_run{true};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ class CmdLineParserConfigFileOption : public CmdLineParser
/// @param[in] argc forwarding of command line arguments
/// @param[in] argv forwarding of command line arguments
/// @param[in] cmdLineParsingMode selects to parse a single option or all options
void parse(int argc,
char* argv[],
const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept override;

roudi::ConfigFilePathString_t getConfigFilePath() const;

void printParameters() noexcept;
/// @param[out] Result wrapped in an cxx::expected, either the parsed arguments as CmdLineArgs_t struct or
/// CmdLineParserResult
cxx::expected<CmdLineArgs_t, CmdLineParserResult>
parse(int argc,
char* argv[],
const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept override;

protected:
roudi::ConfigFilePathString_t m_customConfigFilePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#ifndef IOX_POSH_ROUDI_ROUDI_CONFIG_TOML_FILE_PROVIDER_HPP
#define IOX_POSH_ROUDI_ROUDI_CONFIG_TOML_FILE_PROVIDER_HPP

#include "iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp"
#include "iceoryx_posh/roudi/cmd_line_args.hpp"
#include "iceoryx_posh/roudi/roudi_config_file_provider.hpp"

namespace iox
Expand All @@ -26,7 +26,7 @@ static constexpr char defaultConfigFilePath[] = "/etc/iceoryx/roudi_config.toml"
class TomlRouDiConfigFileProvider : public iox::roudi::RouDiConfigFileProvider
{
public:
TomlRouDiConfigFileProvider(CmdLineParserConfigFileOption& cmdLineParserValue);
TomlRouDiConfigFileProvider(iox::config::CmdLineArgs_t& cmdLineArgs);

iox::cxx::expected<iox::RouDiConfig_t, iox::roudi::RouDiConfigFileParseError> parse() override;
};
Expand Down
4 changes: 2 additions & 2 deletions iceoryx_posh/source/roudi/application/iceoryx_roudi_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace iox
{
namespace roudi
{
IceOryxRouDiApp::IceOryxRouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& roudiConfig) noexcept
: RouDiApp(cmdLineParser, roudiConfig)
IceOryxRouDiApp::IceOryxRouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& roudiConfig) noexcept
: RouDiApp(cmdLineArgs, roudiConfig)
{
}

Expand Down
59 changes: 23 additions & 36 deletions iceoryx_posh/source/roudi/application/roudi_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "iceoryx_posh/internal/log/posh_logging.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/typed_unique_id.hpp"
#include "iceoryx_posh/internal/roudi/roudi.hpp"
#include "iceoryx_posh/roudi/cmd_line_args.hpp"
#include "iceoryx_utils/cxx/helplets.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
#include "iceoryx_utils/internal/posix_wrapper/shared_memory_object/memory_map.hpp"
Expand Down Expand Up @@ -90,17 +91,30 @@ void RouDiApp::registerSigHandler() noexcept
}
}

RouDiApp::RouDiApp(const config::CmdLineParser& cmdLineParser, const RouDiConfig_t& config) noexcept
: RouDiApp(config)
{
setCmdLineParserResults(cmdLineParser);
init();
}

RouDiApp::RouDiApp(const RouDiConfig_t& config) noexcept
: m_run(checkAndOptimizeConfig(config))
RouDiApp::RouDiApp(const config::CmdLineArgs_t& cmdLineArgs, const RouDiConfig_t& config) noexcept
: m_logLevel(cmdLineArgs.logLevel)
, m_monitoringMode(cmdLineArgs.monitoringMode)
, m_run(checkAndOptimizeConfig(config))
, m_config(config)
, m_compatibilityCheckLevel(cmdLineArgs.compatibilityCheckLevel)
, m_processKillDelay(cmdLineArgs.processKillDelay)
{
// the "and" is intentional, just in case the the provided RouDiConfig_t is empty
m_run &= cmdLineArgs.run;
if (cmdLineArgs.uniqueRouDiId)
{
popo::internal::setUniqueRouDiId(cmdLineArgs.uniqueRouDiId.value());
}

// be silent if not running
if (m_run)
{
iox::log::LogManager::GetLogManager().SetDefaultLogLevel(m_logLevel);

registerSigHandler();

LogVerbose() << "Command line parameters are:\n" << cmdLineArgs;
}
}

bool RouDiApp::checkAndOptimizeConfig(const RouDiConfig_t& config) noexcept
Expand All @@ -123,37 +137,10 @@ bool RouDiApp::checkAndOptimizeConfig(const RouDiConfig_t& config) noexcept
return true;
}

void RouDiApp::init() noexcept
{
// be silent if not running
if (m_run)
{
iox::log::LogManager::GetLogManager().SetDefaultLogLevel(m_logLevel);

registerSigHandler();
}
}

bool RouDiApp::waitForSignal() const noexcept
{
return !m_semaphore.wait().has_error();
}

void RouDiApp::setCmdLineParserResults(const config::CmdLineParser& cmdLineParser) noexcept
{
m_monitoringMode = cmdLineParser.getMonitoringMode();
m_logLevel = cmdLineParser.getLogLevel();
// the "and" is intentional, just in case the the provided RouDiConfig_t is empty
m_run &= cmdLineParser.getRun();
m_compatibilityCheckLevel = cmdLineParser.getCompatibilityCheckLevel();
m_processKillDelay = cmdLineParser.getProcessKillDelay();
auto uniqueId = cmdLineParser.getUniqueRouDiId();
if (uniqueId)
{
popo::internal::setUniqueRouDiId(*uniqueId);
}
}


} // namespace roudi
} // namespace iox
37 changes: 20 additions & 17 deletions iceoryx_posh/source/roudi/application/roudi_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "iceoryx_posh/iceoryx_posh_config.hpp"
#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/internal/log/posh_logging.hpp"
#include "iceoryx_posh/roudi/cmd_line_args.hpp"
#include "iceoryx_posh/roudi/iceoryx_roudi_app.hpp"
#include "iceoryx_posh/roudi/roudi_cmd_line_parser_config_file_option.hpp"
#include "iceoryx_posh/roudi/roudi_config_toml_file_provider.hpp"
Expand All @@ -24,23 +25,25 @@ int main(int argc, char* argv[])
using iox::roudi::IceOryxRouDiApp;

iox::config::CmdLineParserConfigFileOption cmdLineParser;
cmdLineParser.parse(argc, argv);

iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineParser);

iox::RouDiConfig_t roudiConfig =
configFileProvider.parse()
.or_else([](iox::roudi::RouDiConfigFileParseError& parseResult) {
iox::LogFatal() << "Couldn't parse config file. Error: "
<< iox::cxx::convertEnumToString(iox::roudi::ROUDI_CONFIG_FILE_PARSE_ERROR_STRINGS,
parseResult);
std::terminate();
})
.value();

cmdLineParser.printParameters();

IceOryxRouDiApp roudi(cmdLineParser, roudiConfig);
auto cmdLineArgs = cmdLineParser.parse(argc, argv);
if (cmdLineArgs.has_error() && (cmdLineArgs.get_error() != iox::config::CmdLineParserResult::INFO_OUTPUT_ONLY))
{
iox::LogFatal() << "Unable to parse command line arguments!";
return EXIT_FAILURE;
}

iox::config::TomlRouDiConfigFileProvider configFileProvider(cmdLineArgs.value());

auto roudiConfig = configFileProvider.parse();
if (roudiConfig.has_error())
{
iox::LogFatal() << "Couldn't parse config file. Error: "
<< iox::cxx::convertEnumToString(iox::roudi::ROUDI_CONFIG_FILE_PARSE_ERROR_STRINGS,
roudiConfig.get_error());
return EXIT_FAILURE;
}

IceOryxRouDiApp roudi(cmdLineArgs.value(), roudiConfig.value());

return roudi.run();
}
Loading

0 comments on commit 2c0c504

Please sign in to comment.