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