Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iox #404 Fix verbose output and introduce CmdLineArgs_t struct #523

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 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,7 @@ int main(int argc, char* argv[])
static constexpr uint32_t ONE_MEGABYTE = 1024U * 1024;

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

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

cmdLineParser.printParameters();

IceOryxRouDiApp roudi(cmdLineParser, roudiConfig);
IceOryxRouDiApp roudi(cmdLineArgs, 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) 2019, 2021 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved.
mossmaurice marked this conversation as resolved.
Show resolved Hide resolved
//
// 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;
bool run{false};
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved.
// 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.
Expand All @@ -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 Down Expand Up @@ -45,11 +46,11 @@ 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;
/// @param[out] Result of the parsed arguments as CmdLineArgs_t struct
virtual CmdLineArgs_t
parse(int argc,
char* argv[],
const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept;

bool getRun() const noexcept;
iox::log::LogLevel getLogLevel() const noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ 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;
/// @param[out] Result of the parsed arguments as CmdLineArgs_t struct
CmdLineArgs_t
parse(int argc,
char* argv[],
const CmdLineArgumentParsingMode cmdLineParsingMode = CmdLineArgumentParsingMode::ALL) noexcept override;

roudi::ConfigFilePathString_t getConfigFilePath() const;

void printParameters() noexcept;

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
9 changes: 4 additions & 5 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,9 +25,9 @@ int main(int argc, char* argv[])
using iox::roudi::IceOryxRouDiApp;

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

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

iox::RouDiConfig_t roudiConfig =
configFileProvider.parse()
Expand All @@ -38,9 +39,7 @@ int main(int argc, char* argv[])
})
.value();

cmdLineParser.printParameters();

IceOryxRouDiApp roudi(cmdLineParser, roudiConfig);
IceOryxRouDiApp roudi(cmdLineArgs, roudiConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to only have the roudiConfig here. Ideally such things like the LogLevel could also set by TOML but the cmd line would have a higher precedence. Maybe something for another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. For me this is related to the RouDi refactorings for safety certificiation. This epic should be started with #230


return roudi.run();
}
16 changes: 3 additions & 13 deletions iceoryx_posh/source/roudi/roudi_cmd_line_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace iox
{
namespace config
{
void CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept
CmdLineArgs_t CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMode cmdLineParsingMode) noexcept
mossmaurice marked this conversation as resolved.
Show resolved Hide resolved
{
constexpr option longOptions[] = {{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 'v'},
Expand Down Expand Up @@ -209,6 +209,8 @@ void CmdLineParser::parse(int argc, char* argv[], const CmdLineArgumentParsingMo
break;
}
}
return CmdLineArgs_t{
m_monitoringMode, m_logLevel, m_compatibilityCheckLevel, m_processKillDelay, m_uniqueRouDiId, m_run, ""};
} // namespace roudi
bool CmdLineParser::getRun() const noexcept
{
Expand Down Expand Up @@ -238,17 +240,5 @@ units::Duration CmdLineParser::getProcessKillDelay() const noexcept
return m_processKillDelay;
}

void CmdLineParser::printParameters() const noexcept
{
LogVerbose() << "Command line parameters are..";
LogVerbose() << "Log level: " << m_logLevel;
LogVerbose() << "Monitoring mode: " << m_monitoringMode;
LogVerbose() << "Compatibility check level: " << m_compatibilityCheckLevel;
m_uniqueRouDiId.and_then([](auto& id) { LogVerbose() << "Unique RouDi ID: " << id; }).or_else([] {
LogVerbose() << "Unique RouDi ID: < unset >";
});
LogVerbose() << "Process kill delay: " << m_processKillDelay.seconds() << " s";
}

} // namespace config
} // namespace iox
Loading