diff options
author | Ed Tanous <ed@tanous.net> | 2020-10-03 18:06:26 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2020-10-23 18:03:17 +0300 |
commit | 04e438cbad66838724d78ce12f28aff1fb892a63 (patch) | |
tree | 8d8c42a8b3d3e9f8e10c108dd6273e8185d04530 /http/logging.hpp | |
parent | dc511aa73001a593a16dbcdaa5d53f320e4c7818 (diff) | |
download | bmcweb-04e438cbad66838724d78ce12f28aff1fb892a63.tar.xz |
fix include names
cppcheck isn't smart enough to recognize these are c++ headers, not c
headers. Considering we're already inconsistent about our naming, it's
easier to just be consistent, and move the last few files to use .hpp
instead of .h.
Tested:
Code builds, no changes.
Signed-off-by: Ed Tanous <ed@tanous.net>
Change-Id: Ic348d695f8527fa4a0ded53f433e1558c319db40
Diffstat (limited to 'http/logging.hpp')
-rw-r--r-- | http/logging.hpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/http/logging.hpp b/http/logging.hpp new file mode 100644 index 0000000000..0121729542 --- /dev/null +++ b/http/logging.hpp @@ -0,0 +1,117 @@ +#pragma once + +#include <cstdio> +#include <cstdlib> +#include <ctime> +#include <filesystem> +#include <iostream> +#include <sstream> +#include <string> + +namespace crow +{ +enum class LogLevel +{ + Debug = 0, + Info, + Warning, + Error, + Critical, +}; + +class Logger +{ + private: + // + static std::string timestamp() + { + std::string date; + date.resize(32, '\0'); + time_t t = time(nullptr); + + tm myTm{}; + + gmtime_r(&t, &myTm); + + size_t sz = + strftime(date.data(), date.size(), "%Y-%m-%d %H:%M:%S", &myTm); + date.resize(sz); + return date; + } + + public: + Logger([[maybe_unused]] const std::string& prefix, + [[maybe_unused]] const std::string& filename, + [[maybe_unused]] const size_t line, LogLevel levelIn) : + level(levelIn) + { +#ifdef BMCWEB_ENABLE_LOGGING + stringstream << "(" << timestamp() << ") [" << prefix << " " + << std::filesystem::path(filename).filename() << ":" + << line << "] "; +#endif + } + ~Logger() + { + if (level >= getCurrentLogLevel()) + { +#ifdef BMCWEB_ENABLE_LOGGING + stringstream << std::endl; + std::cerr << stringstream.str(); +#endif + } + } + + // + template <typename T> + Logger& operator<<([[maybe_unused]] T const& value) + { + if (level >= getCurrentLogLevel()) + { +#ifdef BMCWEB_ENABLE_LOGGING + stringstream << value; +#endif + } + return *this; + } + + // + static void setLogLevel(LogLevel level) + { + getLogLevelRef() = level; + } + + static LogLevel getCurrentLogLevel() + { + return getLogLevelRef(); + } + + private: + // + static LogLevel& getLogLevelRef() + { + static auto currentLevel = static_cast<LogLevel>(1); + return currentLevel; + } + + // + std::ostringstream stringstream; + LogLevel level; +}; +} // namespace crow + +#define BMCWEB_LOG_CRITICAL \ + if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Critical) \ + crow::Logger("CRITICAL", __FILE__, __LINE__, crow::LogLevel::Critical) +#define BMCWEB_LOG_ERROR \ + if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Error) \ + crow::Logger("ERROR", __FILE__, __LINE__, crow::LogLevel::Error) +#define BMCWEB_LOG_WARNING \ + if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Warning) \ + crow::Logger("WARNING", __FILE__, __LINE__, crow::LogLevel::Warning) +#define BMCWEB_LOG_INFO \ + if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Info) \ + crow::Logger("INFO", __FILE__, __LINE__, crow::LogLevel::Info) +#define BMCWEB_LOG_DEBUG \ + if (crow::Logger::getCurrentLogLevel() <= crow::LogLevel::Debug) \ + crow::Logger("DEBUG", __FILE__, __LINE__, crow::LogLevel::Debug) |