104e438cbSEd Tanous #pragma once 204e438cbSEd Tanous 3662aa6e3SMyung Bae #include "bmcweb_config.h" 4662aa6e3SMyung Bae 562598e31SEd Tanous #include <bit> 662598e31SEd Tanous #include <format> 704e438cbSEd Tanous #include <iostream> 862598e31SEd Tanous #include <source_location> 9662aa6e3SMyung Bae #include <string_view> 1062598e31SEd Tanous #include <system_error> 1162598e31SEd Tanous 1262598e31SEd Tanous // NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp) 1362598e31SEd Tanous template <> 1462598e31SEd Tanous struct std::formatter<void*> 1562598e31SEd Tanous { 1662598e31SEd Tanous constexpr auto parse(std::format_parse_context& ctx) 1762598e31SEd Tanous { 1862598e31SEd Tanous return ctx.begin(); 1962598e31SEd Tanous } 2062598e31SEd Tanous auto format(const void*& ptr, auto& ctx) const 2162598e31SEd Tanous { 2262598e31SEd Tanous return std::format_to(ctx.out(), "{}", 2362598e31SEd Tanous std::to_string(std::bit_cast<size_t>(ptr))); 2462598e31SEd Tanous } 2562598e31SEd Tanous }; 2662598e31SEd Tanous // NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp) 2704e438cbSEd Tanous 2804e438cbSEd Tanous namespace crow 2904e438cbSEd Tanous { 3004e438cbSEd Tanous enum class LogLevel 3104e438cbSEd Tanous { 32662aa6e3SMyung Bae Disabled = 0, 3304e438cbSEd Tanous Critical, 34e7245fe8SEd Tanous Error, 35e7245fe8SEd Tanous Warning, 36e7245fe8SEd Tanous Info, 37e7245fe8SEd Tanous Debug, 38e7245fe8SEd Tanous Enabled, 3904e438cbSEd Tanous }; 4004e438cbSEd Tanous 41662aa6e3SMyung Bae // Mapping of the external loglvl name to internal loglvl 42e7245fe8SEd Tanous constexpr std::array<std::string_view, 7> mapLogLevelFromName{ 43e7245fe8SEd Tanous "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"}; 44662aa6e3SMyung Bae 45662aa6e3SMyung Bae constexpr crow::LogLevel getLogLevelFromName(std::string_view name) 46662aa6e3SMyung Bae { 47e7245fe8SEd Tanous const auto* iter = std::ranges::find(mapLogLevelFromName, name); 48e7245fe8SEd Tanous if (iter != mapLogLevelFromName.end()) 49662aa6e3SMyung Bae { 50e7245fe8SEd Tanous return static_cast<LogLevel>(iter - mapLogLevelFromName.begin()); 51662aa6e3SMyung Bae } 52662aa6e3SMyung Bae return crow::LogLevel::Disabled; 53662aa6e3SMyung Bae } 54662aa6e3SMyung Bae 55662aa6e3SMyung Bae // configured bmcweb LogLevel 56662aa6e3SMyung Bae constexpr crow::LogLevel bmcwebCurrentLoggingLevel = 5725b54dbaSEd Tanous getLogLevelFromName(BMCWEB_LOGGING_LEVEL); 58662aa6e3SMyung Bae 5962598e31SEd Tanous template <typename T> 6062598e31SEd Tanous const void* logPtr(T p) 6162598e31SEd Tanous { 6262598e31SEd Tanous static_assert(std::is_pointer<T>::value, 6362598e31SEd Tanous "Can't use logPtr without pointer"); 6462598e31SEd Tanous return std::bit_cast<const void*>(p); 6562598e31SEd Tanous } 6662598e31SEd Tanous 676ea90760SEd Tanous template <LogLevel level, typename... Args> 68*17c47245SEd Tanous inline void vlog(std::format_string<Args...>&& format, Args&&... args, 696ea90760SEd Tanous const std::source_location& loc) noexcept 7062598e31SEd Tanous { 71d518a9beSMyung Bae if constexpr (bmcwebCurrentLoggingLevel < level) 7262598e31SEd Tanous { 7362598e31SEd Tanous return; 7462598e31SEd Tanous } 7562598e31SEd Tanous constexpr size_t stringIndex = static_cast<size_t>(level); 7662598e31SEd Tanous static_assert(stringIndex < mapLogLevelFromName.size(), 7762598e31SEd Tanous "Missing string for level"); 78e7245fe8SEd Tanous constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; 796ea90760SEd Tanous std::string_view filename = loc.file_name(); 80e7245fe8SEd Tanous filename = filename.substr(filename.rfind('/') + 1); 81*17c47245SEd Tanous std::string logLocation; 82*17c47245SEd Tanous logLocation = std::format("[{} {}:{}] ", levelString, filename, loc.line()); 83*17c47245SEd Tanous try 84*17c47245SEd Tanous { 85*17c47245SEd Tanous // TODO, multiple static analysis tools flag that this could potentially 86*17c47245SEd Tanous // throw Based on the documentation, it shouldn't throw, so long as none 87*17c47245SEd Tanous // of the formatters throw, so unclear at this point why this try/catch 88*17c47245SEd Tanous // is required, but add it to silence the static analysis tools. 89*17c47245SEd Tanous logLocation += std::format(std::move(format), 90*17c47245SEd Tanous std::forward<Args>(args)...); 91*17c47245SEd Tanous } 92*17c47245SEd Tanous catch (const std::format_error& /*error*/) 93*17c47245SEd Tanous { 94*17c47245SEd Tanous logLocation += "Failed to format"; 95*17c47245SEd Tanous // Nothing more we can do here if logging is broken. 96*17c47245SEd Tanous } 97*17c47245SEd Tanous logLocation += '\n'; 98*17c47245SEd Tanous // Intentionally ignore error return. 99*17c47245SEd Tanous fwrite(logLocation.data(), sizeof(std::string::value_type), 100*17c47245SEd Tanous logLocation.size(), stdout); 101*17c47245SEd Tanous fflush(stdout); 10262598e31SEd Tanous } 10304e438cbSEd Tanous } // namespace crow 10404e438cbSEd Tanous 10562598e31SEd Tanous template <typename... Args> 1066ea90760SEd Tanous struct BMCWEB_LOG_CRITICAL 10762598e31SEd Tanous { 1086ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1096ea90760SEd Tanous BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args, 1106ea90760SEd Tanous const std::source_location& loc = 1116ea90760SEd Tanous std::source_location::current()) noexcept 1126ea90760SEd Tanous { 113*17c47245SEd Tanous crow::vlog<crow::LogLevel::Critical, Args...>( 114*17c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 11562598e31SEd Tanous } 1166ea90760SEd Tanous }; 117eb8a3998SPatrick Williams 11862598e31SEd Tanous template <typename... Args> 1196ea90760SEd Tanous struct BMCWEB_LOG_ERROR 12062598e31SEd Tanous { 1216ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1226ea90760SEd Tanous BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args, 1236ea90760SEd Tanous const std::source_location& loc = 1246ea90760SEd Tanous std::source_location::current()) noexcept 1256ea90760SEd Tanous { 126*17c47245SEd Tanous crow::vlog<crow::LogLevel::Error, Args...>( 127*17c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 12862598e31SEd Tanous } 1296ea90760SEd Tanous }; 130600d2394SEd Tanous 13162598e31SEd Tanous template <typename... Args> 1326ea90760SEd Tanous struct BMCWEB_LOG_WARNING 13362598e31SEd Tanous { 1346ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1356ea90760SEd Tanous BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args, 1366ea90760SEd Tanous const std::source_location& loc = 1376ea90760SEd Tanous std::source_location::current()) noexcept 1386ea90760SEd Tanous { 139*17c47245SEd Tanous crow::vlog<crow::LogLevel::Warning, Args...>( 140*17c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 14162598e31SEd Tanous } 1426ea90760SEd Tanous }; 143600d2394SEd Tanous 14462598e31SEd Tanous template <typename... Args> 1456ea90760SEd Tanous struct BMCWEB_LOG_INFO 14662598e31SEd Tanous { 1476ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1486ea90760SEd Tanous BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args, 1496ea90760SEd Tanous const std::source_location& loc = 1506ea90760SEd Tanous std::source_location::current()) noexcept 1516ea90760SEd Tanous { 152*17c47245SEd Tanous crow::vlog<crow::LogLevel::Info, Args...>( 153*17c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 15462598e31SEd Tanous } 1556ea90760SEd Tanous }; 156600d2394SEd Tanous 15762598e31SEd Tanous template <typename... Args> 1586ea90760SEd Tanous struct BMCWEB_LOG_DEBUG 15962598e31SEd Tanous { 1606ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1616ea90760SEd Tanous BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args, 1626ea90760SEd Tanous const std::source_location& loc = 1636ea90760SEd Tanous std::source_location::current()) noexcept 1646ea90760SEd Tanous { 165*17c47245SEd Tanous crow::vlog<crow::LogLevel::Debug, Args...>( 166*17c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 16762598e31SEd Tanous } 1686ea90760SEd Tanous }; 1696ea90760SEd Tanous 1706ea90760SEd Tanous template <typename... Args> 1716ea90760SEd Tanous BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...) 1726ea90760SEd Tanous -> BMCWEB_LOG_CRITICAL<Args...>; 1736ea90760SEd Tanous 1746ea90760SEd Tanous template <typename... Args> 1756ea90760SEd Tanous BMCWEB_LOG_ERROR(std::format_string<Args...>, Args&&...) 1766ea90760SEd Tanous -> BMCWEB_LOG_ERROR<Args...>; 1776ea90760SEd Tanous 1786ea90760SEd Tanous template <typename... Args> 1796ea90760SEd Tanous BMCWEB_LOG_WARNING(std::format_string<Args...>, Args&&...) 1806ea90760SEd Tanous -> BMCWEB_LOG_WARNING<Args...>; 1816ea90760SEd Tanous 1826ea90760SEd Tanous template <typename... Args> 1836ea90760SEd Tanous BMCWEB_LOG_INFO(std::format_string<Args...>, Args&&...) 1846ea90760SEd Tanous -> BMCWEB_LOG_INFO<Args...>; 1856ea90760SEd Tanous 1866ea90760SEd Tanous template <typename... Args> 1876ea90760SEd Tanous BMCWEB_LOG_DEBUG(std::format_string<Args...>, Args&&...) 1886ea90760SEd Tanous -> BMCWEB_LOG_DEBUG<Args...>; 189