blob: f6642c79f51c1ed122433c2fc45fbd56308bc422 [file] [log] [blame]
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -07001#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 Nagarkattibd1299b2024-08-12 17:11:04 -07007
Aushim Nagarkattia5532912024-08-29 14:23:20 -07008#include <algorithm>
9#include <array>
10#include <cctype>
Ed Tanous41fe81c2024-09-02 15:08:41 -070011#include <memory>
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070012#include <string>
13
14// Override default log option:
15static void cliLogLevel(const std::string& logLevel)
16{
17 crow::getBmcwebCurrentLoggingLevel() = crow::getLogLevelFromName(logLevel);
18}
19
Aushim Nagarkattia5532912024-08-29 14:23:20 -070020static constexpr std::array<std::string, 7> levels{
21 "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
22
23// Check if debug level is valid
24static 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
35static 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 Nagarkattibd1299b2024-08-12 17:11:04 -070047int 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 Nagarkattia5532912024-08-29 14:23:20 -070060 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 Nagarkattibd1299b2024-08-12 17:11:04 -070069
70 CLI11_PARSE(app, argc, argv)
71
Aushim Nagarkattia5532912024-08-29 14:23:20 -070072 std::transform(loglevel.begin(), loglevel.end(), loglevel.begin(),
73 ::toupper);
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070074 // 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 Nagarkattia5532912024-08-29 14:23:20 -070080 [&io, &loglevel](boost::system::error_code& ec) mutable {
Ed Tanous7a16ddc2024-08-25 12:48:43 -070081 if (ec)
82 {
83 BMCWEB_LOG_ERROR("SetLogLevel returned error with {}", ec);
84 return;
85 }
Aushim Nagarkattia5532912024-08-29 14:23:20 -070086 BMCWEB_LOG_INFO("logging level changed to: {}", loglevel);
Ed Tanous7a16ddc2024-08-25 12:48:43 -070087 io.stop();
88 },
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070089 service, path, iface, method, loglevel);
90
91 io.run();
92
93 return 0;
94}