From 32cb2813e393747d434fe690bbf4701fbb90f12b Mon Sep 17 00:00:00 2001 From: Aman Dhruva Thamminana <48414198+amantham20@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:44:13 -0500 Subject: [PATCH 1/3] updates and logging items added --- source/Logging_main.cpp | 2 + source/core/DEVELOPER_NOTES.md | 1 + source/core/EasyLogging.hpp | 599 +++++++++++++++++++-------------- 3 files changed, 356 insertions(+), 246 deletions(-) diff --git a/source/Logging_main.cpp b/source/Logging_main.cpp index 76740c8d..6b6f5a06 100644 --- a/source/Logging_main.cpp +++ b/source/Logging_main.cpp @@ -10,12 +10,14 @@ void samplefunction() { int main() { + Logger::Log() << Team::TEAM_1 << LogLevel::DEBUG << Color::BLUE << "This is a debug message from Team A." << "aye 2" << std::endl; Logger::Log() << Team::TEAM_2 << LogLevel::INFO << Color::GREEN << "This is an info message from Team B." << std::endl; Logger::Log() << Team::TEAM_3 << LogLevel::ERROR << Color::RED << " RED Error message from Team C." << std::endl; Logger::Log() << Team::TEAM_4 << LogLevel::ERROR << Color::BLUE << "Error message from Team C. no endl"; Logger::Log() << "cont no endl Error message from Team"; + Logger::Log() << std::endl << "Warning message standard overload" << std::endl; samplefunction(); diff --git a/source/core/DEVELOPER_NOTES.md b/source/core/DEVELOPER_NOTES.md index 6abd475c..1d795bf5 100644 --- a/source/core/DEVELOPER_NOTES.md +++ b/source/core/DEVELOPER_NOTES.md @@ -67,6 +67,7 @@ Logger::Log() << Team::TEAM_5 << LogLevel::DEBUG << "The value of variable is: " ### 4. Logging File and Line Information: ```cpp + Logger::Log() << Team::TEAM_6 << LogLevel::INFO << LOG_RELLINE << "This log is from " << __FILE__ << " line " << __LINE__ << Logger::endl; ``` diff --git a/source/core/EasyLogging.hpp b/source/core/EasyLogging.hpp index 9ba5b503..9ab60599 100644 --- a/source/core/EasyLogging.hpp +++ b/source/core/EasyLogging.hpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include + namespace clogged { @@ -9,42 +12,41 @@ namespace clogged { * @brief Log levels for logging * */ -enum class LogLevel { DEBUG, INFO, WARNING, ERROR, NA }; + enum class LogLevel { + NA = 0, DEBUG = 1, INFO = 2, WARNING = 3, ERROR = 4 + }; +//enum class LogLevel { DEBUG, INFO, WARNING, ERROR, NA }; /** * @brief Teams Names for logging * */ -enum class Team { - TEAM_1, - TEAM_2, - TEAM_3, - TEAM_4, - TEAM_5, - TEAM_6, - TEAM_7, - TEAM_8, - TEAM_9, - GENERAL, - NA -}; + enum class Team { + TEAM_1, + TEAM_2, + TEAM_3, + TEAM_4, + TEAM_5, + TEAM_6, + TEAM_7, + TEAM_8, + TEAM_9, + GENERAL, + NA + }; /** * @brief Colors for logging * */ -enum class Color { RESET = 0, BLUE = 34, GREEN = 32, RED = 31 }; - -/** - * @brief Level of logging - * @TODO: Change this to be a flag in the CMakeLists.txt - */ -const LogLevel LOGLEVEL = LogLevel::DEBUG; + enum class Color { + RESET = 0, BLUE = 34, GREEN = 32, RED = 31 + }; /// @brief Ensure that we only log when NDEBUG flg is not set #ifndef NDEBUG -#define LOGLINE "File: " << __FILE__ << "::->::Line(" << __LINE__ << ")" +#define LOG_LINE "File: " << __FILE__ << "::->::Line(" << __LINE__ #define RELATIVE_PATH(file) \ (std::string(file).find_last_of("/\\") != std::string::npos \ @@ -68,216 +70,321 @@ const LogLevel LOGLEVEL = LogLevel::DEBUG; * @details This is a custom logger class that can be used to log messages to * the console. */ -class Logger { - public: - /** - * @brief Sets the Team name for the current log - * - * @param team name of the team - * @return Logger& - */ - Logger &operator<<(Team team) { - currentTeam = team; - metaPrinted = false; - // std::cout << endl; //TODO: Might have to enable this so that we - // can have same line logging when endl is not used - return *this; - } - - /** - * @brief sets the log level for the current log - * - * @param logLevel Level/Type of the log - * @return Logger& - */ - Logger &operator<<(LogLevel logLevel) { - currentLogLevel = logLevel; - metaPrinted = false; - return *this; - } - - /** - * @brief colors of the log - * - * @param color - * @return Logger& - */ - Logger &operator<<(Color color) { - currentColor = color; - return *this; - } - - - /** - * @brief Manipulator for endl so that we can reset the values when a team is - * done logging - * - * @param manipulator - * @return Logger& - */ - Logger &operator<<(std::ostream &(*manipulator)(std::ostream &)) { - typedef std::ostream &(*EndlManipulator)(std::ostream &); - - // Compare the function pointers - if (manipulator == static_cast(std::endl) || - manipulator == endl) { - // Handle std::endl here - currentTeam = Team::NA; - currentLogLevel = LogLevel::DEBUG; - currentColor = Color::RESET; - - std::cout << std::endl; - - metaPrinted = false; - } - - return *this; - } - - /** - * @brief Aye I used a template. - * @brief Function to log the value - * - * @tparam T - * @param value - * @return Logger& - * - * @TODO: Might have to change this so that we only break a team log when a - * new team is set. aka ensure that logger << Team::TEAM_1 << "Hello" << - * "World" << endl; works in one line with one team print - */ - template - Logger &operator<<(const T &value) { - // TODO: Define when to log by loglevel comparison. Goal is to send it in as - // a flag in the CMakeLists.txt - if (currentLogLevel >= LOGLEVEL) { - // added additional flag in case one wants to compile without colors (or) - // if the terminal does not support colors + class Logger { + public: + /** + * @brief Sets the Team name for the current log + * + * @param team name of the team + * @return Logger& + */ + Logger &operator<<(const Team &team) { + currentTeam = team; // NOLINT(unused) + metaPrinted = false; // NOLINT(unused) + + return *this; + } + + /** + * @brief sets the log level for the current log + * + * @param logLevel Level/Type of the log + * @return Logger& + */ + Logger &operator<<(LogLevel logLevel) { + currentLogLevel = logLevel; + metaPrinted = false; // NOLINT(unused) + + return *this; + } + + /** + * @brief colors of the log + * + * @param color + * @return Logger& + */ + Logger &operator<<(Color color) { + currentColor = color; + return *this; + } + + +//#pragma clang diagnostic push +//#pragma ide diagnostic ignored "UnreachableCode" /// TODO: Check if this is valid + /** + * @brief Manipulator for endl so that we can reset the values when a team is + * done logging + * + * @param manipulator + * @return Logger& + */ + Logger &operator<<(std::ostream &(*manipulator)(std::ostream &)) { + typedef std::ostream &(*EndlManipulator)(std::ostream &); + if (static_cast(minLogLevel) <= static_cast(currentLogLevel)) { + + // Compare the function pointers + if (manipulator == static_cast(std::endl) || + manipulator == endl) { + // Handle std::endl here + currentTeam = Team::NA; + currentLogLevel = LogLevel::DEBUG; + currentColor = Color::RESET; + + std::cout << std::endl; + + metaPrinted = false; + } + + } + + return *this; + } +//#pragma clang diagnostic pop + + /** + * @brief Aye I used a template. + * @brief Function to log the value + * + * @tparam T + * @param value + * @return Logger& + * + * @TODO: Might have to change this so that we only break a team log when a + * new team is set. aka ensure that logger << Team::TEAM_1 << "Hello" << + * "World" << endl; works in one line with one team print + */ + template + Logger &operator<<(const T &value) { + // TODO: Define when to log by loglevel comparison. Goal is to send it in as + if (static_cast(minLogLevel) <= static_cast(currentLogLevel)) { + + // a flag in the CMakeLists.txt + // added additional flag in case one wants to compile without colors (or) + // if the terminal does not support colors #ifndef D_ANSI_COLOR_CODES - std::string colorStart = - "\033[" + std::to_string(static_cast(currentColor)) + "m"; - std::string colorEnd = "\033[0m"; + std::string colorStart = + "\033[" + std::to_string(static_cast(currentColor)) + "m"; + std::string colorEnd = "\033[0m"; #else - std::string colorStart = ""; - std::string colorEnd = ""; + std::string colorStart = ""; + std::string colorEnd = ""; #endif - std::ostringstream logMessage; - logMessage << colorStart; - if (!metaPrinted) { - logMessage << teamToString(currentTeam) << logToString(currentLogLevel); - metaPrinted = true; - } - - logMessage << value << colorEnd; - std::cout << logMessage.str(); // << std::endl; //TODO: Might have to - // make enable this so that we can have - // same line logging when endl is not used - } - - return *this; - } - - - /** - * Only instance of the logger once - * Changes requested from Dr.@ofria - * - * @authors @mercere99 - * @return - */ - static Logger& Log() { - static Logger instance; // Guaranteed to be initialized only once. - return instance; - } - - /** - * Only instance of the logger once - * Changes requested from Dr.@ofria - * - * @authors @mercere99 - * @return - */ - template - static Logger & Log(T && arg1, EXTRA_Ts &&... extra_args) { - Log() << std::forward(arg1); // Log the first argument. - if constexpr (sizeof...(EXTRA_Ts) == 0) { // No arguments left. - return Log() << Logger::endl; // Trigger a flush. - } else { - return Log(std::forward(extra_args)...); // Log remaining arguments. - } - } - - - /** - * @brief Custom endl to reset the values - * - * @param os - * @return std::ostream& - */ - static std::ostream &endl(std::ostream &os) { - Log() << std::endl; // Call the custom Logger::endl to reset values - return os; - } - - private: - /// @brief Current team for that is going to log - Team currentTeam = Team::NA; - - /// @brief Current log level for the log - LogLevel currentLogLevel = LogLevel::DEBUG; - - /// @brief Current color for the log - Color currentColor = Color::RESET; - - bool metaPrinted = false; - - /** - * @brief Map to convert Team enum to string - * - */ - std::map teamToStringMap = { - {Team::TEAM_1, "Team 1"}, {Team::TEAM_2, "Team 2"}, - {Team::TEAM_3, "Team 3"}, {Team::TEAM_4, "Team 4"}, - {Team::TEAM_5, "Team 5"}, {Team::TEAM_6, "Team 6"}, - {Team::TEAM_7, "Team 7"}, {Team::TEAM_8, "Team 8"}, - {Team::TEAM_9, "Team 9"}, {Team::GENERAL, "General"}}; - - /** - * @brief Converts Team enum to string - * - * @param team - * @return std::string - */ - std::string teamToString(Team team) { - auto it = teamToStringMap.find(team); - if (it != teamToStringMap.end()) { - return "[" + it->second + "]"; - } - - return ""; - } - - /** - * @brief Converts LogLevel enum to string - * - * @param logLevel - * @return std::string - */ - std::string logToString(LogLevel logLevel) { - if (logLevel == LogLevel::DEBUG) { - return "(DEBUG) "; - } else if (logLevel == LogLevel::INFO) { - return "(INFO) "; - } else if (logLevel == LogLevel::WARNING) { - return "(WARNING) "; - } else if (logLevel == LogLevel::ERROR) { - return "(ERROR) "; - } else { - return ""; - } - } -}; - + std::ostringstream logMessage; + logMessage << colorStart; + if (!metaPrinted) { + logMessage << teamToString(currentTeam) << logToString(currentLogLevel); + metaPrinted = true; + } + + logMessage << value << colorEnd; + std::cout << logMessage.str(); // << std::endl; //TODO: Might have to + // make enable this so that we can have + // same line logging when endl is not used + } + + return *this; + } + + + /** + * Only instance of the logger once + * Changes requested from Dr.@ofria + * + * @authors @mercere99 + * @return + */ + static Logger &Log() { + static Logger instance; // Guaranteed to be initialized only once. + + return instance; + } + + /** + * Only instance of the logger once + * Changes requested from Dr.@ofria + * + * @authors @mercere99 + * @return + */ + template + static Logger &Log(T &&arg1, EXTRA_Ts &&... extra_args) { + Log() << std::forward(arg1); // Log the first argument. + if constexpr (sizeof...(EXTRA_Ts) == 0) { // No arguments left. + return Log() << Logger::endl; // Trigger a flush. + } else { + return Log(std::forward(extra_args)...); // Log remaining arguments. + } + } + + /** + * @brief Returns the current time stamp + * + * @return std::string + */ + [[maybe_unused]] static std::string time() { + auto now = std::chrono::system_clock::now(); + std::time_t currentTime = std::chrono::system_clock::to_time_t(now); + std::string timestamp = std::ctime(¤tTime); + timestamp.pop_back(); // Remove the trailing newline character + + return "[" + timestamp + "] "; + } + + + /** + * @brief Custom endl to reset the values + * + * @param os + * @return std::ostream& + */ + static std::ostream &endl(std::ostream &os) { + Log() << std::endl; // Call the custom Logger::endl to reset values + return os; + } + + /** + * @brief Sets the minimum log level for the logger + * @param level + */ + [[maybe_unused]] static void setMinimumLogLevel(LogLevel level) { + minLogLevel = level; + } + + /** + * @brief Checks if the log level is greater than the minimum log level + * @param level + * @return bool + */ + [[maybe_unused]] static bool checkLogLevel(LogLevel level) { + return static_cast(minLogLevel) <= static_cast(level); + } + + /** + * @brief Sets the global teams for the logger + * @param teams + */ + [[maybe_unused]] static void setPrintTeams(std::set teams) { + currentPrintTeams = std::move(teams); //TODO: Check if this works + } + + /** + * @brief Sets the global teams for the logger + * @param teams + */ + [[maybe_unused]] static void setPrintTeams(const std::vector &teams) { + currentPrintTeams = std::set(teams.begin(), teams.end()); + } + + /** + * @brief Sets the global teams for the logger + * @param team + */ + [[maybe_unused]] static void setPrintTeams(Team team) { + currentPrintTeams.insert(team); + } + + /** + * @brief Sets the global teams for the logger + * @param teams + */ + [[maybe_unused]] static void setPrintTeams(std::initializer_list teams) { + currentPrintTeams = std::set(teams.begin(), teams.end()); + } + + /** + * @brief Checks if the team is in the global teams + * @param team + * @return bool + */ + [[maybe_unused]] static bool checkTeam(Team team) { + return currentPrintTeams.find(team) != currentPrintTeams.end(); + } + + + + private: + /// @brief Current team for that is going to log + Team currentTeam = Team::NA; + + /// @brief Current log level for the log + LogLevel currentLogLevel = LogLevel::DEBUG; + + /// @brief Current color for the log + Color currentColor = Color::RESET; + + static LogLevel minLogLevel; + + /// @brief flag that checks and ensures if the meta data is printed once + bool metaPrinted = false; + + static std::set currentPrintTeams; + + /** + * @brief Map to convert Team enum to string + * + */ + const std::map teamToStringMap = { + {Team::TEAM_1, "Team 1"}, + {Team::TEAM_2, "Team 2"}, + {Team::TEAM_3, "Team 3"}, + {Team::TEAM_4, "Team 4"}, + {Team::TEAM_5, "Team 5"}, + {Team::TEAM_6, "Team 6"}, + {Team::TEAM_7, "Team 7"}, + {Team::TEAM_8, "Team 8"}, + {Team::TEAM_9, "Team 9"}, + {Team::GENERAL, "General"}}; + + /** + * @brief Converts Team enum to string + * + * @param team + * @return std::string + */ + std::string teamToString(Team team) { + auto it = teamToStringMap.find(team); + if (it != teamToStringMap.end()) { + return "[" + it->second + "]"; + } + + return ""; + } + + /** + * @brief Converts LogLevel enum to string + * + * @param logLevel + * @return std::string + */ + static std::string logToString(LogLevel logLevel) { + if (logLevel == LogLevel::DEBUG) { + return "(DEBUG) "; + } else if (logLevel == LogLevel::INFO) { + return "(INFO) "; + } else if (logLevel == LogLevel::WARNING) { + return "(WARNING) "; + } else if (logLevel == LogLevel::ERROR) { + return "(ERROR) "; + } else { + return ""; + } + } + + + [[maybe_unused]] static std::string getCurrentTimestamp() { + auto now = std::chrono::system_clock::now(); + std::time_t currentTime = std::chrono::system_clock::to_time_t(now); + std::string timestamp = std::ctime(¤tTime); + timestamp.pop_back(); // Remove the trailing newline character + return timestamp; + } + }; + + LogLevel Logger::minLogLevel = LogLevel::NA; + std::set Logger::currentPrintTeams = {Team::TEAM_1, Team::TEAM_2, Team::TEAM_3, Team::TEAM_4, Team::TEAM_5, + Team::TEAM_6, Team::TEAM_7, Team::TEAM_8, Team::TEAM_9, Team::GENERAL, + Team::NA}; #else @@ -288,27 +395,27 @@ class Logger { #define log Log() -class Logger { - public: - template - Logger &operator<<(const T &value) { - return *this; - } + class Logger { + public: + template + Logger &operator<<(const T &value) { + return *this; + } - Logger &operator<<(std::ostream &(*manipulator)(std::ostream &)) { - return *this; - } + Logger &operator<<(std::ostream &(*manipulator)(std::ostream &)) { + return *this; + } - static std::ostream &endl(std::ostream &os) { return os; } + static std::ostream &endl(std::ostream &os) { return os; } - static Logger& Log() { - static Logger instance; // Guaranteed to be initialized only once. - return instance; - } + static Logger& Log() { + static Logger instance; // Guaranteed to be initialized only once. + return instance; + } -}; + }; -//Logger Logger::log; + //Logger Logger::log; #endif -} // namespace clogged \ No newline at end of file +} // namespace clogged From cc611983c9187631838abf32e857f46c40a53b1d Mon Sep 17 00:00:00 2001 From: Aman Dhruva Thamminana <48414198+amantham20@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:28:20 -0500 Subject: [PATCH 2/3] added memory include --- source/core/Entity.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core/Entity.hpp b/source/core/Entity.hpp index fa86b030..798e9990 100644 --- a/source/core/Entity.hpp +++ b/source/core/Entity.hpp @@ -10,7 +10,7 @@ #include #include #include - +#include #include "GridPosition.hpp" namespace cse491 { From 10a0f889473ce3f9256c3a3ddf77653e689340b4 Mon Sep 17 00:00:00 2001 From: Aman Dhruva Thamminana <48414198+amantham20@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:34:01 -0500 Subject: [PATCH 3/3] =?UTF-8?q?added=20another=20include=20'algorithm'=20?= =?UTF-8?q?=F0=9F=A4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/core/Entity.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/core/Entity.hpp b/source/core/Entity.hpp index 798e9990..4a2c67d6 100644 --- a/source/core/Entity.hpp +++ b/source/core/Entity.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "GridPosition.hpp" namespace cse491 {