blob: 58fbacb58eb1ed436d652684ce36fe7f57cd4897 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -07003#include "boost_formatters.hpp"
4#include "logging.hpp"
5
6#include <CLI/CLI.hpp>
7#include <boost/asio/io_context.hpp>
8#include <sdbusplus/asio/connection.hpp>
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -07009
Aushim Nagarkattia5532912024-08-29 14:23:20 -070010#include <algorithm>
11#include <array>
12#include <cctype>
Ed Tanous41fe81c2024-09-02 15:08:41 -070013#include <memory>
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070014#include <string>
15
16// Override default log option:
17static void cliLogLevel(const std::string& logLevel)
18{
19 crow::getBmcwebCurrentLoggingLevel() = crow::getLogLevelFromName(logLevel);
20}
21
Aushim Nagarkattia5532912024-08-29 14:23:20 -070022static constexpr std::array<std::string, 7> levels{
23 "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
24
25// Check if debug level is valid
26static std::string validateLogLevel(std::string& input)
27{
28 std::transform(input.begin(), input.end(), input.begin(), ::toupper);
29 const std::string* iter = std::ranges::find(levels, input);
30 if (iter == levels.end())
31 {
32 return {"Invalid log level"};
33 }
34 return {};
35}
36
37static std::string helpMsg()
38{
39 std::string help = "\nLog levels to choose from:\n";
40 for (const std::string& prompt : levels)
41 {
42 std::string level = prompt;
43 std::transform(level.begin(), level.end(), level.begin(), ::tolower);
44 help.append(level + "\n");
45 }
46 return help;
47}
48
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070049int main(int argc, char** argv) noexcept(false)
50{
51 CLI::App app("BMCWeb SetLogLevel CLI");
52
53 cliLogLevel("INFO");
54
55 // Define sdbus interfaces:
56 std::string service = "xyz.openbmc_project.bmcweb";
57 std::string path = "/xyz/openbmc_project/bmcweb";
58 std::string iface = "xyz.openbmc_project.bmcweb";
59 std::string method = "SetLogLevel";
60
61 std::string loglevel;
Aushim Nagarkattia5532912024-08-29 14:23:20 -070062 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 Nagarkattibd1299b2024-08-12 17:11:04 -070071
72 CLI11_PARSE(app, argc, argv)
73
Aushim Nagarkattia5532912024-08-29 14:23:20 -070074 std::transform(loglevel.begin(), loglevel.end(), loglevel.begin(),
75 ::toupper);
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070076 // Set up dbus connection:
77 boost::asio::io_context io;
78 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
79
80 // Attempt to async_call to set logging level
81 conn->async_method_call(
Aushim Nagarkattia5532912024-08-29 14:23:20 -070082 [&io, &loglevel](boost::system::error_code& ec) mutable {
Ed Tanous7a16ddc2024-08-25 12:48:43 -070083 if (ec)
84 {
85 BMCWEB_LOG_ERROR("SetLogLevel returned error with {}", ec);
86 return;
87 }
Aushim Nagarkattia5532912024-08-29 14:23:20 -070088 BMCWEB_LOG_INFO("logging level changed to: {}", loglevel);
Ed Tanous7a16ddc2024-08-25 12:48:43 -070089 io.stop();
90 },
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070091 service, path, iface, method, loglevel);
92
93 io.run();
94
95 return 0;
96}