diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..2dd85ff --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,60 @@ + +#include "log.h" +#include + +const char *getLevelName(LogLevel level){ + switch(level){ + case LogLevel::DEBUG: + return "DEBUG"; + case LogLevel::INFO: + return "INFO"; + case LogLevel::WARNING: + return "WARNING"; + case LogLevel::ERROR: + return "ERROR"; + case LogLevel::CRITICAL: + return "CRITICAL"; + default: + return "NON"; + } +} + +const char *getLevelColor(LogLevel level){ + switch(level){ + case LogLevel::DEBUG: + return "0;37"; + case LogLevel::INFO: + return "0"; + case LogLevel::WARNING: + return "1;33"; + case LogLevel::ERROR: + return "1;31"; + case LogLevel::CRITICAL: + return "1;35"; + default: + return "0"; + } +} + +LogLevel logLevel = LogLevel::INFO; + +void log(const std::string &msg, LogLevel level) { + if(level >= logLevel) { + std::string levelName = getLevelName(level); + int maxLevelNameLength = 8; + + std::cout << "["; + //color and level name;lo + std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; + std::cout << "] "; + for (int i = levelName.size(); i < maxLevelNameLength; i++) { + std::cout << " "; + } + std::cout << msg << "\n"; + } +} + +void setLogLevel(LogLevel level) { + logLevel = level; +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..b7248b1 --- /dev/null +++ b/src/log.h @@ -0,0 +1,19 @@ +#ifndef LIBRARY_LOG_H +#define LIBRARY_LOG_H + +#include + +enum LogLevel{ + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4, + CRITICAL = 5, + OFF = 6 +}; + +void log(const std::string &msg, LogLevel level = LogLevel::INFO); + +void setLogLevel(LogLevel level); + +#endif //LIBRARY_LOG_H