Skip to content

Commit

Permalink
Finished initial implementation of ColoredLogSink
Browse files Browse the repository at this point in the history
azonenberg committed Nov 1, 2016
1 parent 2605e97 commit fe8976e
Showing 3 changed files with 80 additions and 17 deletions.
55 changes: 55 additions & 0 deletions ColoredSTDLogSink.cpp
Original file line number Diff line number Diff line change
@@ -49,5 +49,60 @@ ColoredSTDLogSink::~ColoredSTDLogSink()

void ColoredSTDLogSink::PreprocessLine(std::string& line)
{
//strings we make red
static string red_strings[] =
{
"INTERNAL ERROR",
"ERROR",
"Error",
"error",
};

//ANSI escape codes
static string red = "\033[31;1m";
static string yellow = "\033[33;1m";
static string clear = "\033[0m";

//Bold red errors. If there's a colon after the message, do that too
for(auto s : red_strings)
{
line = replace(s + ":", red, clear, line);
//line = replace(s, red + s + clear, line);
}

//strings we make yellow
static string yellow_strings[] =
{
"WARNING",
"Warning",
"warning"
};

//Bold yellow warnings. If there's a colon after the message, do that too
for(auto s : yellow_strings)
{
line = replace(s + ":", yellow, clear, line);
//line = replace(s, yellow + s + clear, line);
}
}

//TODO: less copying
string ColoredSTDLogSink::replace(
const string& search,
const string& before,
const string& after,
string subject)
{
//If not found, return unchanged
size_t pos = subject.find(search);
if(pos == string::npos)
return subject;

//If found, change color starting beginning of the line until end of the string
size_t end = pos + search.length();
string ret = before;
ret += subject.substr(0, end);
ret += after;
ret += subject.substr(end);
return ret;
}
37 changes: 20 additions & 17 deletions log.cpp
Original file line number Diff line number Diff line change
@@ -75,34 +75,37 @@ string LogSink::WrapString(string str)

//Split the string into lines
string tmp;
if(m_lastMessageWasNewline)
tmp = indent;
bool firstLine = true;
for(size_t i=0; i<str.length(); i++)
{
//Append it
char ch = str[i];
tmp += ch;

//If the pending line is longer than m_termWidth, break it up
if(tmp.length() == m_termWidth)
{
PreprocessLine(tmp);
ret += tmp;
//Unless line is overly long, or done, nothing to do
if( ( (tmp.length() + indent.length() ) < m_termWidth) && (ch != '\n') )
continue;

//We're ending this line
//Only indent the first line if the previous message ended in \n
if( (firstLine && m_lastMessageWasNewline) || !firstLine )
ret += indent;
firstLine = false;

//Add the line after preprocessing as needed
PreprocessLine(tmp);
ret += tmp;

//If we're wrapping due to a long line, add a \n to force it
if(ch != '\n')
ret += "\n";
tmp = indent;
}

//If we hit a newline, wrap and indent the next line
if(ch == '\n')
{
PreprocessLine(tmp);
ret += tmp;
tmp = indent;
}
//Either way, we're done with the current line
tmp = "";
}

//If we have any remaining stuff, append it
if(tmp != indent)
if(tmp != "")
ret += tmp;

//Done
5 changes: 5 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -136,6 +136,11 @@ class ColoredSTDLogSink : public STDLogSink

protected:
void PreprocessLine(std::string& line) override;
std::string replace(
const std::string& search,
const std::string& before,
const std::string& after,
std::string subject);
};

/**

0 comments on commit fe8976e

Please sign in to comment.