Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 1 | #include "boost_formatters.hpp" |
| 2 | #include "logging.hpp" |
| 3 | |
| 4 | #include <CLI/CLI.hpp> |
| 5 | #include <boost/asio/io_context.hpp> |
| 6 | #include <sdbusplus/asio/connection.hpp> |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 7 | |
Aushim Nagarkatti | a553291 | 2024-08-29 14:23:20 -0700 | [diff] [blame^] | 8 | #include <algorithm> |
| 9 | #include <array> |
| 10 | #include <cctype> |
Ed Tanous | 41fe81c | 2024-09-02 15:08:41 -0700 | [diff] [blame] | 11 | #include <memory> |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| 14 | // Override default log option: |
| 15 | static void cliLogLevel(const std::string& logLevel) |
| 16 | { |
| 17 | crow::getBmcwebCurrentLoggingLevel() = crow::getLogLevelFromName(logLevel); |
| 18 | } |
| 19 | |
Aushim Nagarkatti | a553291 | 2024-08-29 14:23:20 -0700 | [diff] [blame^] | 20 | static constexpr std::array<std::string, 7> levels{ |
| 21 | "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"}; |
| 22 | |
| 23 | // Check if debug level is valid |
| 24 | static std::string validateLogLevel(std::string& input) |
| 25 | { |
| 26 | std::transform(input.begin(), input.end(), input.begin(), ::toupper); |
| 27 | const std::string* iter = std::ranges::find(levels, input); |
| 28 | if (iter == levels.end()) |
| 29 | { |
| 30 | return {"Invalid log level"}; |
| 31 | } |
| 32 | return {}; |
| 33 | } |
| 34 | |
| 35 | static std::string helpMsg() |
| 36 | { |
| 37 | std::string help = "\nLog levels to choose from:\n"; |
| 38 | for (const std::string& prompt : levels) |
| 39 | { |
| 40 | std::string level = prompt; |
| 41 | std::transform(level.begin(), level.end(), level.begin(), ::tolower); |
| 42 | help.append(level + "\n"); |
| 43 | } |
| 44 | return help; |
| 45 | } |
| 46 | |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 47 | int main(int argc, char** argv) noexcept(false) |
| 48 | { |
| 49 | CLI::App app("BMCWeb SetLogLevel CLI"); |
| 50 | |
| 51 | cliLogLevel("INFO"); |
| 52 | |
| 53 | // Define sdbus interfaces: |
| 54 | std::string service = "xyz.openbmc_project.bmcweb"; |
| 55 | std::string path = "/xyz/openbmc_project/bmcweb"; |
| 56 | std::string iface = "xyz.openbmc_project.bmcweb"; |
| 57 | std::string method = "SetLogLevel"; |
| 58 | |
| 59 | std::string loglevel; |
Aushim Nagarkatti | a553291 | 2024-08-29 14:23:20 -0700 | [diff] [blame^] | 60 | app.require_subcommand(1); |
| 61 | |
| 62 | const CLI::Validator levelValidator = |
| 63 | CLI::Validator(validateLogLevel, "valid level"); |
| 64 | |
| 65 | CLI::App* sub = app.add_subcommand("loglevel", "Set bmcweb log level"); |
| 66 | sub->add_option("level", loglevel, helpMsg()) |
| 67 | ->required() |
| 68 | ->check(levelValidator); |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 69 | |
| 70 | CLI11_PARSE(app, argc, argv) |
| 71 | |
Aushim Nagarkatti | a553291 | 2024-08-29 14:23:20 -0700 | [diff] [blame^] | 72 | std::transform(loglevel.begin(), loglevel.end(), loglevel.begin(), |
| 73 | ::toupper); |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 74 | // Set up dbus connection: |
| 75 | boost::asio::io_context io; |
| 76 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 77 | |
| 78 | // Attempt to async_call to set logging level |
| 79 | conn->async_method_call( |
Aushim Nagarkatti | a553291 | 2024-08-29 14:23:20 -0700 | [diff] [blame^] | 80 | [&io, &loglevel](boost::system::error_code& ec) mutable { |
Ed Tanous | 7a16ddc | 2024-08-25 12:48:43 -0700 | [diff] [blame] | 81 | if (ec) |
| 82 | { |
| 83 | BMCWEB_LOG_ERROR("SetLogLevel returned error with {}", ec); |
| 84 | return; |
| 85 | } |
Aushim Nagarkatti | a553291 | 2024-08-29 14:23:20 -0700 | [diff] [blame^] | 86 | BMCWEB_LOG_INFO("logging level changed to: {}", loglevel); |
Ed Tanous | 7a16ddc | 2024-08-25 12:48:43 -0700 | [diff] [blame] | 87 | io.stop(); |
| 88 | }, |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 89 | service, path, iface, method, loglevel); |
| 90 | |
| 91 | io.run(); |
| 92 | |
| 93 | return 0; |
| 94 | } |