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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | // |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 66 | template <typename T> |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 67 | Logger& operator<<([[maybe_unused]] T const& value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | { |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 69 | if (level >= getCurrentLogLevel()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 71 | #ifdef BMCWEB_ENABLE_LOGGING |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | stringstream << value; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | #endif |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 74 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | return *this; |
| 76 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 77 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | // |
| 79 | static void setLogLevel(LogLevel level) |
| 80 | { |
| 81 | getLogLevelRef() = level; |
| 82 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 83 | |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 84 | static LogLevel getCurrentLogLevel() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | { |
| 86 | return getLogLevelRef(); |
| 87 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 88 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 89 | private: |
| 90 | // |
| 91 | static LogLevel& getLogLevelRef() |
| 92 | { |
| 93 | static auto currentLevel = static_cast<LogLevel>(1); |
| 94 | return currentLevel; |
| 95 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 96 | |
| 97 | // |
| 98 | std::ostringstream stringstream; |
| 99 | LogLevel level; |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 100 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | } // namespace crow |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 102 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | #define BMCWEB_LOG_CRITICAL \ |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 104 | if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Critical) \ |
| 105 | crow::Logger("CRITICAL", __FILE__, __LINE__, crow::LogLevel::Critical) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | #define BMCWEB_LOG_ERROR \ |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 107 | if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Error) \ |
| 108 | crow::Logger("ERROR", __FILE__, __LINE__, crow::LogLevel::Error) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | #define BMCWEB_LOG_WARNING \ |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 110 | if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Warning) \ |
| 111 | crow::Logger("WARNING", __FILE__, __LINE__, crow::LogLevel::Warning) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | #define BMCWEB_LOG_INFO \ |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 113 | if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Info) \ |
| 114 | crow::Logger("INFO", __FILE__, __LINE__, crow::LogLevel::Info) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | #define BMCWEB_LOG_DEBUG \ |
Ed Tanous | eb643c2 | 2020-10-02 14:51:22 -0700 | [diff] [blame] | 116 | if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Debug) \ |
| 117 | crow::Logger("DEBUG", __FILE__, __LINE__, crow::LogLevel::Debug) |