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