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> 6817c47245SEd 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(); 80*ea5e2242SEd Tanous filename = filename.substr(filename.rfind('/')); 81*ea5e2242SEd Tanous if (!filename.empty()) 82*ea5e2242SEd Tanous { 83*ea5e2242SEd Tanous filename.remove_prefix(1); 84*ea5e2242SEd Tanous } 8517c47245SEd Tanous std::string logLocation; 8617c47245SEd Tanous try 8717c47245SEd Tanous { 8817c47245SEd Tanous // TODO, multiple static analysis tools flag that this could potentially 8917c47245SEd Tanous // throw Based on the documentation, it shouldn't throw, so long as none 9017c47245SEd Tanous // of the formatters throw, so unclear at this point why this try/catch 9117c47245SEd Tanous // is required, but add it to silence the static analysis tools. 92c8491cb0SEd Tanous logLocation = std::format("[{} {}:{}] ", levelString, filename, 93c8491cb0SEd Tanous loc.line()); 9417c47245SEd Tanous logLocation += std::format(std::move(format), 9517c47245SEd Tanous std::forward<Args>(args)...); 9617c47245SEd Tanous } 9717c47245SEd Tanous catch (const std::format_error& /*error*/) 9817c47245SEd Tanous { 9917c47245SEd Tanous logLocation += "Failed to format"; 10017c47245SEd Tanous // Nothing more we can do here if logging is broken. 10117c47245SEd Tanous } 10217c47245SEd Tanous logLocation += '\n'; 10317c47245SEd Tanous // Intentionally ignore error return. 10417c47245SEd Tanous fwrite(logLocation.data(), sizeof(std::string::value_type), 10517c47245SEd Tanous logLocation.size(), stdout); 10617c47245SEd Tanous fflush(stdout); 10762598e31SEd Tanous } 10804e438cbSEd Tanous } // namespace crow 10904e438cbSEd Tanous 11062598e31SEd Tanous template <typename... Args> 1116ea90760SEd Tanous struct BMCWEB_LOG_CRITICAL 11262598e31SEd Tanous { 1136ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1146ea90760SEd Tanous BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args, 1156ea90760SEd Tanous const std::source_location& loc = 1166ea90760SEd Tanous std::source_location::current()) noexcept 1176ea90760SEd Tanous { 11817c47245SEd Tanous crow::vlog<crow::LogLevel::Critical, Args...>( 11917c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 12062598e31SEd Tanous } 1216ea90760SEd Tanous }; 122eb8a3998SPatrick Williams 12362598e31SEd Tanous template <typename... Args> 1246ea90760SEd Tanous struct BMCWEB_LOG_ERROR 12562598e31SEd Tanous { 1266ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1276ea90760SEd Tanous BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args, 1286ea90760SEd Tanous const std::source_location& loc = 1296ea90760SEd Tanous std::source_location::current()) noexcept 1306ea90760SEd Tanous { 13117c47245SEd Tanous crow::vlog<crow::LogLevel::Error, Args...>( 13217c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 13362598e31SEd Tanous } 1346ea90760SEd Tanous }; 135600d2394SEd Tanous 13662598e31SEd Tanous template <typename... Args> 1376ea90760SEd Tanous struct BMCWEB_LOG_WARNING 13862598e31SEd Tanous { 1396ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1406ea90760SEd Tanous BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args, 1416ea90760SEd Tanous const std::source_location& loc = 1426ea90760SEd Tanous std::source_location::current()) noexcept 1436ea90760SEd Tanous { 14417c47245SEd Tanous crow::vlog<crow::LogLevel::Warning, Args...>( 14517c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 14662598e31SEd Tanous } 1476ea90760SEd Tanous }; 148600d2394SEd Tanous 14962598e31SEd Tanous template <typename... Args> 1506ea90760SEd Tanous struct BMCWEB_LOG_INFO 15162598e31SEd Tanous { 1526ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1536ea90760SEd Tanous BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args, 1546ea90760SEd Tanous const std::source_location& loc = 1556ea90760SEd Tanous std::source_location::current()) noexcept 1566ea90760SEd Tanous { 15717c47245SEd Tanous crow::vlog<crow::LogLevel::Info, Args...>( 15817c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 15962598e31SEd Tanous } 1606ea90760SEd Tanous }; 161600d2394SEd Tanous 16262598e31SEd Tanous template <typename... Args> 1636ea90760SEd Tanous struct BMCWEB_LOG_DEBUG 16462598e31SEd Tanous { 1656ea90760SEd Tanous // NOLINTNEXTLINE(google-explicit-constructor) 1666ea90760SEd Tanous BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args, 1676ea90760SEd Tanous const std::source_location& loc = 1686ea90760SEd Tanous std::source_location::current()) noexcept 1696ea90760SEd Tanous { 17017c47245SEd Tanous crow::vlog<crow::LogLevel::Debug, Args...>( 17117c47245SEd Tanous std::move(format), std::forward<Args>(args)..., loc); 17262598e31SEd Tanous } 1736ea90760SEd Tanous }; 1746ea90760SEd Tanous 1756ea90760SEd Tanous template <typename... Args> 1766ea90760SEd Tanous BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...) 1776ea90760SEd Tanous -> BMCWEB_LOG_CRITICAL<Args...>; 1786ea90760SEd Tanous 1796ea90760SEd Tanous template <typename... Args> 1806ea90760SEd Tanous BMCWEB_LOG_ERROR(std::format_string<Args...>, Args&&...) 1816ea90760SEd Tanous -> BMCWEB_LOG_ERROR<Args...>; 1826ea90760SEd Tanous 1836ea90760SEd Tanous template <typename... Args> 1846ea90760SEd Tanous BMCWEB_LOG_WARNING(std::format_string<Args...>, Args&&...) 1856ea90760SEd Tanous -> BMCWEB_LOG_WARNING<Args...>; 1866ea90760SEd Tanous 1876ea90760SEd Tanous template <typename... Args> 1886ea90760SEd Tanous BMCWEB_LOG_INFO(std::format_string<Args...>, Args&&...) 1896ea90760SEd Tanous -> BMCWEB_LOG_INFO<Args...>; 1906ea90760SEd Tanous 1916ea90760SEd Tanous template <typename... Args> 1926ea90760SEd Tanous BMCWEB_LOG_DEBUG(std::format_string<Args...>, Args&&...) 1936ea90760SEd Tanous -> BMCWEB_LOG_DEBUG<Args...>; 194