Don't print escape sequences in logs, print raw

This commit is contained in:
sam-astro 2022-05-27 20:14:07 -04:00
parent b25fee150a
commit a8ed99a7b4
3 changed files with 25 additions and 3 deletions

View File

@ -146,7 +146,7 @@ void PrintColored(std::string text, std::string fgColor, std::string bgColor, bo
int LogWarning(const string& warningText)
{
PrintColored("WARNING: ", yellowFGColor, "", true);
PrintColored(warningText, yellowFGColor, "", true);
PrintColored(escaped(warningText), yellowFGColor, "", true);
cerr << std::endl;
//cout << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl;
return 1;
@ -184,7 +184,7 @@ int InterpreterLog(const string& logText)
PrintColored("[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] ", blueFGColor, "", true);
PrintColored("ZSharp: ", yellowFGColor, "", true);
PrintColored(logText, greenFGColor, "", true);
PrintColored(escaped(logText), greenFGColor, "", true);
cout << std::endl;
//cout << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mZSharp: \x1B[32m" << logText << "\033[0m\t\t" << endl;
return 1;
@ -221,7 +221,7 @@ int LogCriticalError(const string& errorText)
PrintColored("[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] ", blueFGColor, "", true);
PrintColored("ZSharp: ", yellowFGColor, "", true);
PrintColored(errorText, redFGColor, "", true);
PrintColored(escaped(errorText), redFGColor, "", true);
cerr << std::endl;
InterpreterLog("Press Enter to Exit...");
cin.ignore();

View File

@ -51,6 +51,26 @@ string unescape(const string& s)
return res;
}
std::string escaped(const std::string& input)
{
std::string output;
output.reserve(input.size());
for (const char c : input) {
switch (c) {
case '\a': output += "\\a"; break;
case '\b': output += "\\b"; break;
case '\f': output += "\\f"; break;
case '\n': output += "\\n"; break;
case '\r': output += "\\r"; break;
case '\t': output += "\\t"; break;
case '\v': output += "\\v"; break;
default: output += c; break;
}
}
return output;
}
string StringRaw(const string& s)
{
string str = trim(s);

View File

@ -69,4 +69,6 @@ bool isEscaped(const string& str, int curChar);
bool startsWith(const string& str, const string& lookFor);
std::string escaped(const std::string& input);
#endif