Skip to content

Commit

Permalink
added colorization for messages from Qt
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkorsukov committed Jul 10, 2023
1 parent c543239 commit 1f22c24
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/autobot/internal/api/logapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ void LogApi::debug(const QString& message)

void LogApi::error(const QString& tag, const QString& message)
{
LOGE_T(tag.toStdString(), haw::logger::Red)() << message;
LOGE_T(tag.toStdString())() << message;
}

void LogApi::warn(const QString& tag, const QString& message)
{
LOGW_T(tag.toStdString(), haw::logger::Yellow)() << message;
LOGW_T(tag.toStdString())() << message;
}

void LogApi::info(const QString& tag, const QString& message)
{
LOGI_T(tag.toStdString(), haw::logger::Green)() << message;
LOGI_T(tag.toStdString())() << message;
}

void LogApi::debug(const QString& tag, const QString& message)
{
LOGD_T(tag.toStdString(), haw::logger::None)() << message;
LOGD_T(tag.toStdString())() << message;
}
4 changes: 2 additions & 2 deletions src/framework/global/globalmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ void GlobalModule::onPreInit(const IApplication::RunMode& mode)
using namespace haw::profiler;
struct MyPrinter : public Profiler::Printer
{
void printDebug(const std::string& str) override { LOG_STREAM(Logger::DEBG, "Profiler", "", None)() << str; }
void printInfo(const std::string& str) override { LOG_STREAM(Logger::INFO, "Profiler", "", None)() << str; }
void printDebug(const std::string& str) override { LOG_STREAM(Logger::DEBG, "Profiler", "", Color::Magenta)() << str; }
void printInfo(const std::string& str) override { LOG_STREAM(Logger::INFO, "Profiler", "", Color::Magenta)() << str; }
};

Profiler::Options profOpt;
Expand Down
28 changes: 18 additions & 10 deletions src/framework/global/thirdparty/haw_logger/logger/log_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@
#define LOG_STREAM(type, tag, funcInfo, color) haw::logger::LogInput(type, tag, funcInfo, color).stream
#define LOG(type, tag, color) LOG_STREAM(type, tag, FUNCNAME(FUNC_INFO) + ": ", color)

#define LOGE_T(tag, color) IF_LOGLEVEL(haw::logger::Normal) LOG(haw::logger::Logger::ERRR, tag, color)
#define LOGW_T(tag, color) IF_LOGLEVEL(haw::logger::Normal) LOG(haw::logger::Logger::WARN, tag, color)
#define LOGI_T(tag, color) IF_LOGLEVEL(haw::logger::Normal) LOG(haw::logger::Logger::INFO, tag, color)
#define LOGD_T(tag, color) IF_LOGLEVEL(haw::logger::Debug) LOG(haw::logger::Logger::DEBG, tag, color)

#define LOGE LOGE_T(LOG_TAG, haw::logger::Red)
#define LOGW LOGW_T(LOG_TAG, haw::logger::Yellow)
#define LOGI LOGI_T(LOG_TAG, haw::logger::Green)
#define LOGD LOGD_T(LOG_TAG, haw::logger::None)
#define LOGN if (0) LOGD_T(LOG_TAG, haw::logger::None) // compiling, but no output
#define LOGE_T(tag) \
IF_LOGLEVEL(haw::logger::Normal) \
LOG(haw::logger::Logger::ERRR, tag, haw::logger::Logger::colorForType(haw::logger::Logger::ERRR))
#define LOGW_T(tag) \
IF_LOGLEVEL(haw::logger::Normal) \
LOG(haw::logger::Logger::WARN, tag, haw::logger::Logger::colorForType(haw::logger::Logger::WARN))
#define LOGI_T(tag) \
IF_LOGLEVEL(haw::logger::Normal) \
LOG(haw::logger::Logger::INFO, tag, haw::logger::Logger::colorForType(haw::logger::Logger::INFO))
#define LOGD_T(tag) \
IF_LOGLEVEL(haw::logger::Debug) \
LOG(haw::logger::Logger::DEBG, tag, haw::logger::Logger::colorForType(haw::logger::Logger::DEBG))

#define LOGE LOGE_T(LOG_TAG)
#define LOGW LOGW_T(LOG_TAG)
#define LOGI LOGI_T(LOG_TAG)
#define LOGD LOGD_T(LOG_TAG)
#define LOGN if (0) LOGD_T(LOG_TAG) // compiling, but no output

//! Helps
#define DEPRECATED LOGD() << "This function deprecated!!"
Expand Down
18 changes: 18 additions & 0 deletions src/framework/global/thirdparty/haw_logger/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <cassert>
#include <cstdarg>
#include <map>

#include "logdefdest.h"
#include "helpful.h"
Expand All @@ -16,6 +17,13 @@ const Type Logger::WARN("WARN");
const Type Logger::INFO("INFO");
const Type Logger::DEBG("DEBUG");

static const std::map<Type, Color> TYPES_COLOR = {
{ Logger::ERRR, Color::Red },
{ Logger::WARN, Color::Yellow },
{ Logger::INFO, Color::Green },
{ Logger::DEBG, Color::None },
};

// Layout ---------------------------------

static const std::string DATETIME_PATTERN("${datetime}");
Expand Down Expand Up @@ -273,6 +281,15 @@ LogLayout LogDest::layout() const

// Logger ---------------------------------

Color Logger::colorForType(const Type& type)
{
auto it = TYPES_COLOR.find(type);
if (it != TYPES_COLOR.end()) {
return it->second;
}
return Color::None;
}

Logger* Logger::instance()
{
static Logger l;
Expand Down Expand Up @@ -401,6 +418,7 @@ void Logger::logMsgHandler(QtMsgType type, const QMessageLogContext& ctx, const
}

LogMsg logMsg(qtMsgTypeToString(type), Qt, msg);
logMsg.color = Logger::colorForType(logMsg.type);

Logger::instance()->write(logMsg);
}
Expand Down
2 changes: 2 additions & 0 deletions src/framework/global/thirdparty/haw_logger/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class Logger
static const Type INFO;
static const Type DEBG;

static Color colorForType(const Type& type);

void setupDefault();

void setLevel(Level level);
Expand Down

0 comments on commit 1f22c24

Please sign in to comment.