Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 3 | #include "bmcweb_config.h" |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <array> |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstdlib> |
| 9 | #include <ctime> |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 10 | #include <filesystem> |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 11 | #include <iostream> |
| 12 | #include <sstream> |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 13 | #include <string> |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 14 | #include <string_view> |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
| 17 | { |
| 18 | enum class LogLevel |
| 19 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 20 | Disabled = 0, |
| 21 | Debug, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | Info, |
| 23 | Warning, |
| 24 | Error, |
| 25 | Critical, |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 26 | }; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 27 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 28 | // Mapping of the external loglvl name to internal loglvl |
| 29 | constexpr std::array<std::pair<std::string_view, crow::LogLevel>, 7> |
| 30 | mapLogLevelFromName{{{"disabled", crow::LogLevel::Disabled}, |
| 31 | {"enabled", crow::LogLevel::Debug}, |
| 32 | {"debug", crow::LogLevel::Debug}, |
| 33 | {"info", crow::LogLevel::Info}, |
| 34 | {"warning", crow::LogLevel::Warning}, |
| 35 | {"error", crow::LogLevel::Error}, |
| 36 | {"critical", crow::LogLevel::Critical}}}; |
| 37 | |
| 38 | constexpr crow::LogLevel getLogLevelFromName(std::string_view name) |
| 39 | { |
| 40 | const auto* iter = |
| 41 | std::find_if(begin(mapLogLevelFromName), end(mapLogLevelFromName), |
| 42 | [&name](const auto& v) { return v.first == name; }); |
| 43 | if (iter != end(mapLogLevelFromName)) |
| 44 | { |
| 45 | return iter->second; |
| 46 | } |
| 47 | return crow::LogLevel::Disabled; |
| 48 | } |
| 49 | |
| 50 | // configured bmcweb LogLevel |
| 51 | constexpr crow::LogLevel bmcwebCurrentLoggingLevel = |
| 52 | getLogLevelFromName(bmcwebLoggingLevel); |
| 53 | |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 54 | class Logger |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | { |
| 56 | private: |
| 57 | // |
| 58 | static std::string timestamp() |
| 59 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 60 | std::string date; |
| 61 | date.resize(32, '\0'); |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 62 | time_t t = time(nullptr); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 63 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | tm myTm{}; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 65 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | gmtime_r(&t, &myTm); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 67 | |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 68 | size_t sz = strftime(date.data(), date.size(), "%Y-%m-%d %H:%M:%S", |
| 69 | &myTm); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 70 | date.resize(sz); |
| 71 | return date; |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 72 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | public: |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 75 | Logger([[maybe_unused]] const std::string& prefix, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 76 | [[maybe_unused]] const std::string& filename, |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 77 | [[maybe_unused]] const size_t line) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | { |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 79 | stringstream << "(" << timestamp() << ") [" << prefix << " " |
| 80 | << std::filesystem::path(filename).filename() << ":" |
| 81 | << line << "] "; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | } |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 83 | ~Logger() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 85 | stringstream << std::endl; |
| 86 | std::cerr << stringstream.str(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 88 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 89 | Logger(const Logger&) = delete; |
| 90 | Logger(Logger&&) = delete; |
| 91 | Logger& operator=(const Logger&) = delete; |
| 92 | Logger& operator=(const Logger&&) = delete; |
| 93 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 94 | // |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 95 | template <typename T> |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 96 | Logger& operator<<([[maybe_unused]] const T& value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 98 | // Somewhere in the code we're implicitly casting an array to a |
| 99 | // pointer in logging code. It's non-trivial to find, |
| 100 | // so disable the check here for now |
| 101 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) |
| 102 | stringstream << value; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | return *this; |
| 104 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 105 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 106 | constexpr static LogLevel getCurrentLogLevel() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 108 | return bmcwebCurrentLoggingLevel; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 110 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 111 | constexpr static bool isLoggingEnabled() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 113 | return getCurrentLogLevel() >= crow::LogLevel::Debug; |
| 114 | } |
| 115 | |
| 116 | constexpr static bool checkLoggingLevel(const LogLevel level) |
| 117 | { |
| 118 | return isLoggingEnabled() && (getCurrentLogLevel() <= level); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 120 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | private: |
| 122 | // |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | std::ostringstream stringstream; |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 124 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 125 | } // namespace crow |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 126 | |
Patrick Williams | eb8a399 | 2023-05-12 09:57:16 -0500 | [diff] [blame^] | 127 | // Disable clang-tidy warnings about unused macros. |
| 128 | // NOLINTBEGIN(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros) |
| 129 | |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 130 | // The logging functions currently use macros. Now that we have c++20, ideally |
| 131 | // they'd use source_location with fixed functions, but for the moment, disable |
| 132 | // the check. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 133 | #define BMCWEB_LOG_CRITICAL \ |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 134 | if constexpr (crow::Logger::checkLoggingLevel(crow::LogLevel::Critical)) \ |
| 135 | crow::Logger("CRITICAL", __FILE__, __LINE__) |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 136 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 137 | #define BMCWEB_LOG_ERROR \ |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 138 | if constexpr (crow::Logger::checkLoggingLevel(crow::LogLevel::Error)) \ |
| 139 | crow::Logger("ERROR", __FILE__, __LINE__) |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 140 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | #define BMCWEB_LOG_WARNING \ |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 142 | if constexpr (crow::Logger::checkLoggingLevel(crow::LogLevel::Warning)) \ |
| 143 | crow::Logger("WARNING", __FILE__, __LINE__) |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 144 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | #define BMCWEB_LOG_INFO \ |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 146 | if constexpr (crow::Logger::checkLoggingLevel(crow::LogLevel::Info)) \ |
| 147 | crow::Logger("INFO", __FILE__, __LINE__) |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | #define BMCWEB_LOG_DEBUG \ |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 150 | if constexpr (crow::Logger::checkLoggingLevel(crow::LogLevel::Debug)) \ |
| 151 | crow::Logger("DEBUG", __FILE__, __LINE__) |
Patrick Williams | eb8a399 | 2023-05-12 09:57:16 -0500 | [diff] [blame^] | 152 | |
| 153 | // NOLINTEND(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros) |