xref: /openbmc/bmcweb/src/webserver_cli.cpp (revision c6178aba)
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>
7 #include <sdbusplus/bus.hpp>
8 
9 #include <string>
10 
11 // Override default log option:
12 static void cliLogLevel(const std::string& logLevel)
13 {
14     crow::getBmcwebCurrentLoggingLevel() = crow::getLogLevelFromName(logLevel);
15 }
16 
17 int main(int argc, char** argv) noexcept(false)
18 {
19     CLI::App app("BMCWeb SetLogLevel CLI");
20 
21     cliLogLevel("INFO");
22 
23     // Define sdbus interfaces:
24     std::string service = "xyz.openbmc_project.bmcweb";
25     std::string path = "/xyz/openbmc_project/bmcweb";
26     std::string iface = "xyz.openbmc_project.bmcweb";
27     std::string method = "SetLogLevel";
28 
29     std::string loglevel;
30     app.add_option("-l,--loglevel", loglevel, "Set bmcweb log level");
31 
32     CLI11_PARSE(app, argc, argv)
33 
34     BMCWEB_LOG_INFO("Working on log-level: {}", loglevel);
35 
36     // Set up dbus connection:
37     boost::asio::io_context io;
38     auto conn = std::make_shared<sdbusplus::asio::connection>(io);
39 
40     // Attempt to async_call to set logging level
41     conn->async_method_call(
42         [&io](boost::system::error_code& ec) mutable {
43             if (ec)
44             {
45                 BMCWEB_LOG_ERROR("SetLogLevel returned error with {}", ec);
46                 return;
47             }
48             BMCWEB_LOG_INFO("Successfully changed log-level ");
49             io.stop();
50         },
51         service, path, iface, method, loglevel);
52 
53     io.run();
54 
55     return 0;
56 }
57