diff --git a/src/log.cpp b/src/log.cpp index b3de3e9..fdd286e 100755 --- a/src/log.cpp +++ b/src/log.cpp @@ -10,30 +10,50 @@ namespace gz { const char* COLORS[] = { "\033[0m", // RESET - "\033[30m", // BLACK - "\033[31m", // RED - "\033[32m", // GREEN - "\033[33m", // YELLOW - "\033[34m", // BLUE - "\033[35m", // MAGENTA - "\033[36m", // CYAN - "\033[37m", // WHITE - "\033[1;30m", // BBLACK - "\033[1;31m", // BRED - "\033[1;32m", // BGREEN - "\033[1;33m", // BYELLOW - "\033[1;34m", // BBLUE - "\033[1;35m", // BMAGENTA - "\033[1;36m", // BCYAN - "\033[1;37m", // BWHITE + // normal + "\033[0;30m", // BLACK + "\033[0;31m", // RED + "\033[0;32m", // GREEN + "\033[0;33m", // YELLOW + "\033[0;34m", // BLUE + "\033[0;35m", // MAGENTA + "\033[0;36m", // CYAN + "\033[0;37m", // WHITE + // bold + "\033[0;1;30m", // BO_BLACK + "\033[0;1;31m", // BO_RED + "\033[0;1;32m", // BO_GREEN + "\033[0;1;33m", // BO_YELLOW + "\033[0;1;34m", // BO_BLUE + "\033[0;1;35m", // BO_MAGENTA + "\033[0;1;36m", // BO_CYAN + "\033[0;1;37m", // BO_WHITE + // background + "\033[0;40m", // BO_BLACK + "\033[0;41m", // BO_RED + "\033[0;42m", // BO_GREEN + "\033[0;30;43m", // BO_YELLOW + "\033[0;44m", // BO_BLUE + "\033[0;45m", // BO_MAGENTA + "\033[0;46m", // BO_CYAN + "\033[0;47m", // BO_WHITE + // light + "\033[0;38;5;9m", // LI_RED + "\033[0;38;5;10m", // LI_GREEN + "\033[0;38;5;11m", // LI_YELLOW + "\033[0;38;5;12m", // LI_BLUE + "\033[0;38;5;13m", // LI_MAGENTA + "\033[0;38;5;14m", // LI_CYAN + "\033[0;38;5;15m", // LI_WHITE + "\033[0;38;5;169m", // LI_BLACK }; #ifdef LOG_MULTITHREAD std::mutex Log::mtx; #endif - Log::Log(std::string logfile, bool showLog, bool storeLog, std::string&& prefix_, Color prefixColor, bool clearLogfileOnRestart, unsigned int writeAfterLines) - : iter(0), writeToFileAfterLines(writeAfterLines), showLog(showLog), storeLog(storeLog), prefixColor(prefixColor), prefix(prefix_ + ": "), prefixLength(prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1) { + Log::Log(std::string logfile, bool showLog, bool storeLog, std::string&& prefix_, Color prefixColor, Color timeColor, bool clearLogfileOnRestart, unsigned int writeAfterLines) + : iter(0), writeToFileAfterLines(writeAfterLines), showLog(showLog), storeLog(storeLog), prefixColor(prefixColor), prefix(prefix_ + ": "), prefixLength(prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1), timeColor(timeColor) { // get absolute path to the logfile fs::path logpath(logfile); if (!logpath.is_absolute()) { diff --git a/src/log.hpp b/src/log.hpp index f8ad72d..d34369d 100755 --- a/src/log.hpp +++ b/src/log.hpp @@ -26,7 +26,11 @@ namespace gz { // /// Colors to be used in Log::clog enum Color { - RESET, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, BBLACK, BRED, BGREEN, BYELLOW, BBLUE, BMAGENTA, BCYAN, BWHITE + RESET, + BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, + BO_BLACK, BO_RED, BO_GREEN, BO_YELLOW, BO_BLUE, BO_MAGENTA, BO_CYAN, BO_WHITE, + BG_BLACK, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_MAGENTA, BG_CYAN, BG_WHITE, + LI_RED, LI_GREEN, LI_YELLOW, LI_BLUE, LI_MAGENTA, LI_CYAN, LI_WHITE, LI_BLACK, }; extern const char* COLORS[]; @@ -103,7 +107,7 @@ class Log { * * @note Colors will only be shown when written to stdout, not in the logfile. */ - Log(std::string logfile="log.log", bool showLog=true, bool storeLog=true, std::string&& prefix="", Color prefixColor=RESET, bool clearLogfileOnRestart=true, unsigned int writeAfterLines=100); + Log(std::string logfile="log.log", bool showLog=true, bool storeLog=true, std::string&& prefix="", Color prefixColor=RESET, Color timeColor=RESET, bool clearLogfileOnRestart=true, unsigned int writeAfterLines=100); ~Log(); @@ -127,9 +131,11 @@ class Log { vlog(" ", std::forward(args)...); logLines[iter] += "\n"; - std::cout << std::string_view(logLines[iter].c_str(), LOG_TIMESTAMP_CHAR_COUNT - 1) << - COLORS[prefixColor] << prefix << COLORS[RESET] << - std::string_view(logLines[iter].begin() + prefixLength, logLines[iter].end()); + if (showLog) { + std::cout << COLORS[timeColor] << std::string_view(logLines[iter].c_str(), LOG_TIMESTAMP_CHAR_COUNT - 1) << + COLORS[prefixColor] << prefix << COLORS[RESET] << + std::string_view(logLines[iter].begin() + prefixLength, logLines[iter].end()); + } if (++iter >= writeToFileAfterLines) { iter = 0; writeLog(); @@ -188,14 +194,16 @@ class Log { #endif getTime(); logLines[iter] = std::string(time); - logLines[iter] += prefix + ": " + type + ": "; + logLines[iter] += prefix + type + ": "; vlog(" ", std::forward(args)...); logLines[iter] += "\n"; - std::cout << std::string_view(logLines[iter].c_str(), LOG_TIMESTAMP_CHAR_COUNT - 1) << - COLORS[prefixColor] << prefix << COLORS[typeColor] << - std::string_view(logLines[iter].begin() + prefixLength + LOG_POSTPREFIX_CHAR_COUNT, logLines[iter].begin() + prefixLength + type.size() + 2 * LOG_POSTPREFIX_CHAR_COUNT) << - COLORS[messageColor] << std::string_view(logLines[iter].begin() + prefixLength + type.size() + 2 * LOG_POSTPREFIX_CHAR_COUNT, logLines[iter].end()) << COLORS[RESET]; + if (showLog) { + std::cout << COLORS[timeColor] << std::string_view(logLines[iter].c_str(), LOG_TIMESTAMP_CHAR_COUNT - 1) << + COLORS[prefixColor] << prefix << COLORS[typeColor] << + std::string_view(logLines[iter].begin() + prefixLength + LOG_POSTPREFIX_CHAR_COUNT, logLines[iter].begin() + prefixLength + type.size() + 2 * LOG_POSTPREFIX_CHAR_COUNT) << + COLORS[messageColor] << std::string_view(logLines[iter].begin() + prefixLength + type.size() + 2 * LOG_POSTPREFIX_CHAR_COUNT, logLines[iter].end()) << COLORS[RESET]; + } if (++iter >= writeToFileAfterLines) { iter = 0; writeLog(); @@ -205,11 +213,64 @@ class Log { #endif } + /** + * @brief Log a message in a certain color + * @details + * The message will look like this: + *