summaryrefslogtreecommitdiff
path: root/http/logging.h
diff options
context:
space:
mode:
authorEd Tanous <ed.tanous@intel.com>2019-10-11 01:39:33 +0300
committerEd Tanous <ed.tanous@intel.com>2019-10-18 19:20:11 +0300
commitc94ad49bc747e7a7170287b9f4c859e3638cf432 (patch)
tree17ca83806e5b546f20c3478862fe1aa699e2ae22 /http/logging.h
parent789771dda22c256afa9e46ffe4c859bb87532af1 (diff)
downloadbmcweb-c94ad49bc747e7a7170287b9f4c859e3638cf432.tar.xz
Make references to crow less obvious
Recently, a number of people in the community have made the (admittedly easy) mistake that we use a significant portion of crow. Today, we use crow for the router, and the "app" structure, and even those have been significantly modified to meet the bmc needs. All other components have been replaced with Boost beast. This commit removes the crow mentions from the Readme, and moves the crow folder to "http" to camouflage it a little. No code content has changed. Tested: Code compiles. No functional change made to any executable code. Signed-off-by: Ed Tanous <ed.tanous@intel.com> Change-Id: Iceb57b26306cc8bdcfc77f3874246338864fd118
Diffstat (limited to 'http/logging.h')
-rw-r--r--http/logging.h115
1 files changed, 115 insertions, 0 deletions
diff --git a/http/logging.h b/http/logging.h
new file mode 100644
index 0000000000..a608f1f1f5
--- /dev/null
+++ b/http/logging.h
@@ -0,0 +1,115 @@
+#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(0);
+
+ 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(const std::string& prefix, const std::string& filename,
+ 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 >= get_current_log_level())
+ {
+#ifdef BMCWEB_ENABLE_LOGGING
+ stringstream << std::endl;
+ std::cerr << stringstream.str();
+#endif
+ }
+ }
+
+ //
+ template <typename T> logger& operator<<(T const& value)
+ {
+ if (level >= get_current_log_level())
+ {
+#ifdef BMCWEB_ENABLE_LOGGING
+ stringstream << value;
+#endif
+ }
+ return *this;
+ }
+
+ //
+ static void setLogLevel(LogLevel level)
+ {
+ getLogLevelRef() = level;
+ }
+
+ static LogLevel get_current_log_level()
+ {
+ 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::get_current_log_level() <= crow::LogLevel::Critical) \
+ crow::logger("CRITICAL", __FILE__, __LINE__, crow::LogLevel::Critical)
+#define BMCWEB_LOG_ERROR \
+ if (crow::logger::get_current_log_level() <= crow::LogLevel::Error) \
+ crow::logger("ERROR", __FILE__, __LINE__, crow::LogLevel::Error)
+#define BMCWEB_LOG_WARNING \
+ if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning) \
+ crow::logger("WARNING", __FILE__, __LINE__, crow::LogLevel::Warning)
+#define BMCWEB_LOG_INFO \
+ if (crow::logger::get_current_log_level() <= crow::LogLevel::Info) \
+ crow::logger("INFO", __FILE__, __LINE__, crow::LogLevel::Info)
+#define BMCWEB_LOG_DEBUG \
+ if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug) \
+ crow::logger("DEBUG", __FILE__, __LINE__, crow::LogLevel::Debug)