diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2022-06-24 13:44:12 (GMT) |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2022-06-28 14:03:22 (GMT) |
commit | 23bbac941a94391548b57303f9ab371fa5a82b96 (patch) | |
tree | 89904fe6d1db1488414b4c5dae54696a8782bec3 /Source/cmake.cxx | |
parent | 2b6ef864e0906480e2eab4ed90a273fa629aaa62 (diff) | |
download | CMake-23bbac941a94391548b57303f9ab371fa5a82b96.zip CMake-23bbac941a94391548b57303f9ab371fa5a82b96.tar.gz CMake-23bbac941a94391548b57303f9ab371fa5a82b96.tar.bz2 |
Add cmake_language(GET_MESSAGE_LOG_LEVEL) sub command
The new sub-command writes a string representation of the
current log level to the output variable given to the
sub-command.
Given that the log-level might be set either via the --log-level
command line option or via the CMAKE_MESSAGE_LOG_LEVEL
cache / regular variables, the priority for each of the log level
sources is as follows, with the first one being the highest:
1) --log-level
2) CMAKE_MESSAGE_LOG_LEVEL regular variable
3) CMAKE_MESSAGE_LOG_LEVEL cache variable
4) default log level (STATUS)
Fixes: #23572
Diffstat (limited to 'Source/cmake.cxx')
-rw-r--r-- | Source/cmake.cxx | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 026c3d4..efb2520 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -3,6 +3,7 @@ #include "cmake.h" #include <algorithm> +#include <array> #include <cstdio> #include <cstdlib> #include <cstring> @@ -1402,21 +1403,32 @@ void cmake::SetArgs(const std::vector<std::string>& args) #endif } -Message::LogLevel cmake::StringToLogLevel(const std::string& levelStr) -{ - using LevelsPair = std::pair<std::string, Message::LogLevel>; - static const std::vector<LevelsPair> levels = { - { "error", Message::LogLevel::LOG_ERROR }, - { "warning", Message::LogLevel::LOG_WARNING }, - { "notice", Message::LogLevel::LOG_NOTICE }, - { "status", Message::LogLevel::LOG_STATUS }, - { "verbose", Message::LogLevel::LOG_VERBOSE }, - { "debug", Message::LogLevel::LOG_DEBUG }, - { "trace", Message::LogLevel::LOG_TRACE } +namespace { +using LevelsPair = std::pair<cm::string_view, Message::LogLevel>; +using LevelsPairArray = std::array<LevelsPair, 7>; +const LevelsPairArray& getStringToLogLevelPairs() +{ + static const LevelsPairArray levels = { + { { "error", Message::LogLevel::LOG_ERROR }, + { "warning", Message::LogLevel::LOG_WARNING }, + { "notice", Message::LogLevel::LOG_NOTICE }, + { "status", Message::LogLevel::LOG_STATUS }, + { "verbose", Message::LogLevel::LOG_VERBOSE }, + { "debug", Message::LogLevel::LOG_DEBUG }, + { "trace", Message::LogLevel::LOG_TRACE } } }; + return levels; +} +} // namespace + +Message::LogLevel cmake::StringToLogLevel(cm::string_view levelStr) +{ + const LevelsPairArray& levels = getStringToLogLevelPairs(); - const auto levelStrLowCase = cmSystemTools::LowerCase(levelStr); + const auto levelStrLowCase = + cmSystemTools::LowerCase(std::string{ levelStr }); + // NOLINTNEXTLINE(readability-qualified-auto) const auto it = std::find_if(levels.cbegin(), levels.cend(), [&levelStrLowCase](const LevelsPair& p) { return p.first == levelStrLowCase; @@ -1424,6 +1436,21 @@ Message::LogLevel cmake::StringToLogLevel(const std::string& levelStr) return (it != levels.cend()) ? it->second : Message::LogLevel::LOG_UNDEFINED; } +std::string cmake::LogLevelToString(Message::LogLevel level) +{ + const LevelsPairArray& levels = getStringToLogLevelPairs(); + + // NOLINTNEXTLINE(readability-qualified-auto) + const auto it = + std::find_if(levels.cbegin(), levels.cend(), + [&level](const LevelsPair& p) { return p.second == level; }); + const cm::string_view levelStrLowerCase = + (it != levels.cend()) ? it->first : "undefined"; + std::string levelStrUpperCase = + cmSystemTools::UpperCase(std::string{ levelStrLowerCase }); + return levelStrUpperCase; +} + cmake::TraceFormat cmake::StringToTraceFormat(const std::string& traceStr) { using TracePair = std::pair<std::string, TraceFormat>; |