Refactor ErrorMessage's severity to Severity::SeverityType.

This commit is contained in:
Kimmo Varis 2010-07-14 19:36:34 +03:00
parent fa0f6edacf
commit 5f8af2e1e8
3 changed files with 20 additions and 11 deletions

View File

@ -23,6 +23,7 @@
#include <sstream> #include <sstream>
ErrorLogger::ErrorMessage::ErrorMessage() ErrorLogger::ErrorMessage::ErrorMessage()
:_severity(Severity::none)
{ {
} }
@ -30,7 +31,7 @@ ErrorLogger::ErrorMessage::ErrorMessage()
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id) ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id)
{ {
_callStack = callStack; _callStack = callStack;
_severity = severity; _severity = Severity::fromString(severity);
_msg = msg; _msg = msg;
_id = id; _id = id;
} }
@ -39,7 +40,7 @@ std::string ErrorLogger::ErrorMessage::serialize() const
{ {
std::ostringstream oss; std::ostringstream oss;
oss << _id.length() << " " << _id; oss << _id.length() << " " << _id;
oss << _severity.length() << " " << _severity; oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity);
oss << _msg.length() << " " << _msg; oss << _msg.length() << " " << _msg;
oss << _callStack.size() << " "; oss << _callStack.size() << " ";
@ -77,7 +78,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
} }
_id = results[0]; _id = results[0];
_severity = results[1]; _severity = Severity::fromString(results[1]);
_msg = results[2]; _msg = results[2];
unsigned int stackSize = 0; unsigned int stackSize = 0;
@ -153,7 +154,7 @@ std::string ErrorLogger::ErrorMessage::toXML() const
xml << " line=\"" << _callStack.back().line << "\""; xml << " line=\"" << _callStack.back().line << "\"";
} }
xml << " id=\"" << _id << "\""; xml << " id=\"" << _id << "\"";
xml << " severity=\"" << _severity << "\""; xml << " severity=\"" << Severity::toString(_severity) << "\"";
xml << " msg=\"" << stringToXml(_msg) << "\""; xml << " msg=\"" << stringToXml(_msg) << "\"";
xml << "/>"; xml << "/>";
return xml.str(); return xml.str();
@ -176,8 +177,8 @@ std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat)
std::ostringstream text; std::ostringstream text;
if (!_callStack.empty()) if (!_callStack.empty())
text << callStackToString(_callStack) << ": "; text << callStackToString(_callStack) << ": ";
if (!_severity.empty()) if (_severity != Severity::none)
text << "(" << _severity << ") "; text << "(" << Severity::toString(_severity) << ") ";
text << _msg; text << _msg;
return text.str(); return text.str();
} }
@ -185,7 +186,7 @@ std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat)
{ {
std::string result = outputFormat; std::string result = outputFormat;
findAndReplace(result, "{id}", _id); findAndReplace(result, "{id}", _id);
findAndReplace(result, "{severity}", _severity); findAndReplace(result, "{severity}", Severity::toString(_severity));
findAndReplace(result, "{message}", _msg); findAndReplace(result, "{message}", _msg);
if (!_callStack.empty()) if (!_callStack.empty())

View File

@ -33,7 +33,7 @@ class Tokenizer;
class Severity class Severity
{ {
public: public:
enum SeverityType { error, style }; enum SeverityType { none, error, style };
static std::string toString(SeverityType severity) static std::string toString(SeverityType severity)
{ {
switch (severity) switch (severity)
@ -45,6 +45,14 @@ public:
}; };
return "???"; return "???";
} }
static SeverityType fromString(const std::string &severity)
{
if (severity == "error")
return error;
if (severity == "style")
return style;
return none;
}
}; };
/** /**
@ -102,7 +110,7 @@ public:
std::string serialize() const; std::string serialize() const;
bool deserialize(const std::string &data); bool deserialize(const std::string &data);
std::list<FileLocation> _callStack; std::list<FileLocation> _callStack;
std::string _severity; Severity::SeverityType _severity;
std::string _msg; std::string _msg;
std::string _id; std::string _id;
}; };

View File

@ -757,7 +757,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
loc.file = filename; loc.file = filename;
loc.line = linenr; loc.line = linenr;
errmsg._callStack.push_back(loc); errmsg._callStack.push_back(loc);
errmsg._severity = "error"; errmsg._severity = Severity::fromString("error");
errmsg._msg = "mismatching number of '(' and ')' in this line: " + def; errmsg._msg = "mismatching number of '(' and ')' in this line: " + def;
errmsg._id = "preprocessor" + lineStream.str(); errmsg._id = "preprocessor" + lineStream.str();
_errorLogger->reportErr(errmsg); _errorLogger->reportErr(errmsg);
@ -902,7 +902,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
loc.file = filename; loc.file = filename;
loc.line = 1; loc.line = 1;
errmsg._callStack.push_back(loc); errmsg._callStack.push_back(loc);
errmsg._severity = "error"; errmsg._severity = Severity::fromString("error");
errmsg._msg = "Error parsing this: " + s; errmsg._msg = "Error parsing this: " + s;
errmsg._id = "preprocessor" + lineStream.str(); errmsg._id = "preprocessor" + lineStream.str();
_errorLogger->reportErr(errmsg); _errorLogger->reportErr(errmsg);