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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | class logger |
| 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: |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 43 | logger(const std::string& prefix, const std::string& filename, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 44 | const size_t line, LogLevel levelIn) : |
| 45 | level(levelIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 47 | #ifdef BMCWEB_ENABLE_LOGGING |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 48 | stringstream << "(" << timestamp() << ") [" << prefix << " " |
| 49 | << std::filesystem::path(filename).filename() << ":" |
| 50 | << line << "] "; |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 51 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | } |
| 53 | ~logger() |
| 54 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | if (level >= get_current_log_level()) |
| 56 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 57 | #ifdef BMCWEB_ENABLE_LOGGING |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | stringstream << std::endl; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 59 | std::cerr << stringstream.str(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 60 | #endif |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 61 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | } |
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 | // |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 65 | template <typename T> |
| 66 | logger& operator<<(T const& value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 67 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | if (level >= get_current_log_level()) |
| 69 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 70 | #ifdef BMCWEB_ENABLE_LOGGING |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | stringstream << value; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | #endif |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 73 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | return *this; |
| 75 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 76 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | // |
| 78 | static void setLogLevel(LogLevel level) |
| 79 | { |
| 80 | getLogLevelRef() = level; |
| 81 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 82 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | static LogLevel get_current_log_level() |
| 84 | { |
| 85 | return getLogLevelRef(); |
| 86 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 87 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | private: |
| 89 | // |
| 90 | static LogLevel& getLogLevelRef() |
| 91 | { |
| 92 | static auto currentLevel = static_cast<LogLevel>(1); |
| 93 | return currentLevel; |
| 94 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 95 | |
| 96 | // |
| 97 | std::ostringstream stringstream; |
| 98 | LogLevel level; |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 99 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | } // namespace crow |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 101 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 102 | #define BMCWEB_LOG_CRITICAL \ |
| 103 | if (crow::logger::get_current_log_level() <= crow::LogLevel::Critical) \ |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 104 | crow::logger("CRITICAL", __FILE__, __LINE__, crow::LogLevel::Critical) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | #define BMCWEB_LOG_ERROR \ |
| 106 | if (crow::logger::get_current_log_level() <= crow::LogLevel::Error) \ |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 107 | crow::logger("ERROR", __FILE__, __LINE__, crow::LogLevel::Error) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | #define BMCWEB_LOG_WARNING \ |
| 109 | if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning) \ |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 110 | crow::logger("WARNING", __FILE__, __LINE__, crow::LogLevel::Warning) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | #define BMCWEB_LOG_INFO \ |
| 112 | if (crow::logger::get_current_log_level() <= crow::LogLevel::Info) \ |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 113 | crow::logger("INFO", __FILE__, __LINE__, crow::LogLevel::Info) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | #define BMCWEB_LOG_DEBUG \ |
| 115 | if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug) \ |
James Feist | a0dba0f | 2019-07-08 10:26:14 -0700 | [diff] [blame] | 116 | crow::logger("DEBUG", __FILE__, __LINE__, crow::LogLevel::Debug) |