Skip to content

Commit

Permalink
Implemented coloured log
Browse files Browse the repository at this point in the history
This commit allows you to print using output in colours, allowing you to easily see output during debugging
  • Loading branch information
HemantAntony authored and igorkorsukov committed Jul 10, 2023
1 parent b8c9211 commit bc8e925
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 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())() << message;
LOGE_T(tag.toStdString(), haw::logger::Red)() << message;
}

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

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

void LogApi::debug(const QString& tag, const QString& message)
{
LOGD_T(tag.toStdString())() << message;
LOGD_T(tag.toStdString(), haw::logger::None)() << message;
}
4 changes: 2 additions & 2 deletions src/framework/draw/utils/drawlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void DrawObjectsLogger::beginObject(const std::string& name)
std::string gap;
gap.resize(m_objects.size());
#ifdef LOG_STREAM
LOG_STREAM(haw::logger::Logger::DEBG, DRAW_OBJ_TAG, "")() << "Begin: " << gap << name;
LOG_STREAM(haw::logger::Logger::DEBG, DRAW_OBJ_TAG, "", haw::logger::None)() << "Begin: " << gap << name;
#else
UNUSED(pagePos);
#endif
Expand All @@ -48,7 +48,7 @@ void DrawObjectsLogger::endObject()
std::string gap;
gap.resize(m_objects.size());
#ifdef LOG_STREAM
LOG_STREAM(haw::logger::Logger::DEBG, DRAW_OBJ_TAG, "")() << "End: " << gap << m_objects.top();
LOG_STREAM(haw::logger::Logger::DEBG, DRAW_OBJ_TAG, "", haw::logger::None)() << "End: " << gap << m_objects.top();
#endif

m_objects.pop();
Expand Down
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", "")() << str; }
void printInfo(const std::string& str) override { LOG_STREAM(Logger::INFO, "Profiler", "")() << str; }
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; }
};

Profiler::Options profOpt;
Expand Down
26 changes: 13 additions & 13 deletions src/framework/global/thirdparty/haw_logger/logger/log_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@

#define IF_LOGLEVEL(level) if (haw::logger::Logger::instance()->isLevel(level))

#define LOG_STREAM(type, tag, funcInfo) haw::logger::LogInput(type, tag, funcInfo).stream
#define LOG(type, tag) LOG_STREAM(type, tag, FUNCNAME(FUNC_INFO) + ": ")

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

#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
#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

//! Helps
#define DEPRECATED LOGD() << "This function deprecated!!"
Expand Down
23 changes: 20 additions & 3 deletions src/framework/global/thirdparty/haw_logger/logger/logdefdest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

#include <iostream>
#include <cassert>
#include <unordered_map>

#ifdef _WIN32
#include <Windows.h>
#endif

using namespace haw::logger;

static const std::unordered_map<Color, std::string> COLOR_CODES = {
{ None, "\033[0m" },
{ Black, "\033[1;30m" },
{ Red, "\033[1;31m" },
{ Green, "\033[1;32m" },
{ Yellow, "\033[1;33m" },
{ Blue, "\033[1;34m" },
{ Magenta, "\033[1;35m" },
{ Cyan, "\033[1;36m" },
{ White, "\033[1;37m" }
};

MemLogDest::MemLogDest(const LogLayout& l)
: LogDest(l)
{
Expand Down Expand Up @@ -75,12 +88,16 @@ std::string ConsoleLogDest::name() const

void ConsoleLogDest::write(const LogMsg& logMsg)
{
std::string msg;
msg.reserve(100);
msg.append(COLOR_CODES.at(logMsg.color));
msg.append(m_layout.output(logMsg));
msg.append("\033[0m");
#ifdef _WIN32
std::string stemp = m_layout.output(logMsg);
std::wstring temp = std::wstring(stemp.begin(), stemp.end());
std::wstring temp = std::wstring(msg.begin(), msg.end());
OutputDebugString(temp.c_str());
OutputDebugString(L"\n");
#else
std::cout << m_layout.output(logMsg) << std::endl;
std::cout << msg << std::endl;
#endif
}
4 changes: 2 additions & 2 deletions src/framework/global/thirdparty/haw_logger/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ void Logger::setIsCatchQtMsg(bool arg)

#endif

LogInput::LogInput(const Type& type, const std::string& tag, const std::string& funcInfo)
: m_msg(type, tag), m_funcInfo(funcInfo)
LogInput::LogInput(const Type& type, const std::string& tag, const std::string& funcInfo, const Color& color)
: m_msg(type, tag, color), m_funcInfo(funcInfo)
{
}

Expand Down
19 changes: 18 additions & 1 deletion src/framework/global/thirdparty/haw_logger/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ enum Level {
Full = 3
};

enum Color {
None,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White
};

struct Date
{
int day = 0;
Expand Down Expand Up @@ -65,6 +77,10 @@ class LogMsg
: type(l), tag(t), datetime(DateTime::now()),
thread(std::this_thread::get_id()) {}

LogMsg(const Type& l, const std::string& t, const Color& color)
: type(l), tag(t), datetime(DateTime::now()),
thread(std::this_thread::get_id()), color(color) {}

LogMsg(const Type& l, const std::string& t, const std::string& m)
: type(l), tag(t), message(m), datetime(DateTime::now()),
thread(std::this_thread::get_id()) {}
Expand All @@ -74,6 +90,7 @@ class LogMsg
std::string message;
DateTime datetime;
std::thread::id thread;
Color color = None;
};

//! Layout ---------------------------------
Expand Down Expand Up @@ -181,7 +198,7 @@ class Logger
class LogInput
{
public:
explicit LogInput(const Type& type, const std::string& tag, const std::string& funcInfo);
explicit LogInput(const Type& type, const std::string& tag, const std::string& funcInfo, const Color& color);
~LogInput();

Stream& stream();
Expand Down

0 comments on commit bc8e925

Please sign in to comment.