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