colorize log messages written to console correspondingly to severity of

the message
This commit is contained in:
2020-01-24 14:57:20 +04:00
parent f48bf41cbb
commit fdad3480f9
3 changed files with 30 additions and 6 deletions
+3 -3
View File
@@ -45,7 +45,7 @@ public:
//print header if present //print header if present
if(Open() && mHeader.size()) if(Open() && mHeader.size())
{ {
SendMessage((char*)mHeader.c_str()); SendMessage((char*)mHeader.c_str(), severity);
} }
} }
@@ -56,7 +56,7 @@ public:
FormatString(fmt, args); FormatString(fmt, args);
va_end(args); va_end(args);
FormatPostfix(); FormatPostfix();
SendMessage(mString); SendMessage(mString, severity);
mLock.Unlock(); mLock.Unlock();
} }
@@ -121,7 +121,7 @@ public:
protected: protected:
//actually sends message to the source //actually sends message to the source
virtual void SendMessage(const char* msg) = 0; virtual void SendMessage(const char* msg, SC_LogSeverity severity = SEVERITY_DEBUG) = 0;
//format message prefix //format message prefix
virtual void FormatPrefix(SC_LogSeverity severity); virtual void FormatPrefix(SC_LogSeverity severity);
+26 -2
View File
@@ -3,6 +3,11 @@
#include "log.h" #include "log.h"
#define RED "\e[0;31m"
#define GREEN "\e[0;32m"
#define YELLOW "\e[1;33m"
#define NC "\e[0m" // No Color
class SC_LogConsole : public SC_LogBase { class SC_LogConsole : public SC_LogBase {
public: public:
@@ -21,8 +26,27 @@ public:
return true; return true;
} }
protected: protected:
inline virtual void SendMessage(const char* msg) { inline virtual void SendMessage(const char* msg, SC_LogSeverity severity) {
fprintf(stderr, "%s", msg); const char* color = NC;
switch(severity) {
case SEVERITY_DEBUG:
color = NC;
break;
case SEVERITY_ERROR:
color = RED;
break;
case SEVERITY_INFO:
color = GREEN;
break;
case SEVERITY_WARNING:
color = YELLOW;
break;
default:
color = NC;
break;
}
fprintf(stderr, "%s%s%s", color, msg, NC);
} }
}; };
+1 -1
View File
@@ -83,7 +83,7 @@ public:
protected: protected:
inline virtual void SendMessage(const char* msg) inline virtual void SendMessage(const char* msg, SC_LogSeverity severity = SEVERITY_DEBUG)
{ {
if(mFile && mBytesCount < mMaxFileSize) { if(mFile && mBytesCount < mMaxFileSize) {
if(fputs(msg, mFile) >= 0) { if(fputs(msg, mFile) >= 0) {