/* * Cppcheck - A tool for static C/C++ code analysis * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "errorlogger.h" #include "path.h" #include "cppcheck.h" #include #include ErrorLogger::ErrorMessage::ErrorMessage() :_severity(Severity::none) { } ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id) { _callStack = callStack; _severity = severity; setmsg(msg); _id = id; } void ErrorLogger::ErrorMessage::setmsg(const std::string &msg) { const std::string::size_type pos = msg.find("\n"); if (pos == std::string::npos) { _shortMessage = msg; _verboseMessage = msg; } else { _shortMessage = msg.substr(0, pos); _verboseMessage = msg.substr(pos + 1); } } std::string ErrorLogger::ErrorMessage::serialize() const { std::ostringstream oss; oss << _id.length() << " " << _id; oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity); oss << _shortMessage.length() << " " << _shortMessage; oss << _callStack.size() << " "; for (std::list::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok) { std::ostringstream smallStream; smallStream << (*tok).line << ":" << (*tok).getfile(); oss << smallStream.str().length() << " " << smallStream.str(); } return oss.str(); } bool ErrorLogger::ErrorMessage::deserialize(const std::string &data) { _callStack.clear(); std::istringstream iss(data); std::vector results; while (iss.good()) { unsigned int len = 0; if (!(iss >> len)) return false; iss.get(); std::string temp; for (unsigned int i = 0; i < len && iss.good(); ++i) { char c = static_cast(iss.get()); temp.append(1, c); } results.push_back(temp); if (results.size() == 3) break; } _id = results[0]; _severity = Severity::fromString(results[1]); _shortMessage = results[2]; unsigned int stackSize = 0; if (!(iss >> stackSize)) return false; while (iss.good()) { unsigned int len = 0; if (!(iss >> len)) return false; iss.get(); std::string temp; for (unsigned int i = 0; i < len && iss.good(); ++i) { char c = static_cast(iss.get()); temp.append(1, c); } ErrorLogger::ErrorMessage::FileLocation loc; loc.setfile(temp.substr(temp.find(':') + 1)); temp = temp.substr(0, temp.find(':')); std::istringstream fiss(temp); fiss >> loc.line; _callStack.push_back(loc); if (_callStack.size() >= stackSize) break; } return true; } std::string ErrorLogger::ErrorMessage::getXMLHeader(int xml_version) { std::ostringstream ostr; ostr << "\n"; if (xml_version <= 1) { ostr << ""; } else { ostr << "\n"; ostr << " " << CppCheck::version() << ""; } return ostr.str(); } std::string ErrorLogger::ErrorMessage::getXMLFooter() { return ""; } static std::string stringToXml(std::string s) { std::string::size_type pos = 0; while ((pos = s.find_first_of("<>&\"\n", pos)) != std::string::npos) { if (s[pos] == '<') s.insert(pos + 1, "<"); else if (s[pos] == '>') s.insert(pos + 1, ">"); else if (s[pos] == '&') s.insert(pos + 1, "&"); else if (s[pos] == '"') s.insert(pos + 1, """); else if (s[pos] == '\n') s.insert(pos + 1, " "); s.erase(pos, 1); ++pos; } return s; } std::string ErrorLogger::ErrorMessage::toXML(bool verbose, int version) const { std::ostringstream xml; if (version == 1) { xml << ""; } else if (version == 2) { xml << " " << std::endl; for (std::list::const_reverse_iterator it = _callStack.rbegin(); it != _callStack.rend(); ++it) { xml << " " << std::endl; } xml << " "; } return xml.str(); } void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith) { std::string::size_type index = 0; while ((index = source.find(searchFor, index)) != std::string::npos) { source.replace(index, searchFor.length(), replaceWith); index += replaceWith.length() - searchFor.length() + 1; } } std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string &outputFormat) const { if (outputFormat.length() == 0) { std::ostringstream text; if (!_callStack.empty()) text << callStackToString(_callStack) << ": "; if (_severity != Severity::none) text << "(" << Severity::toString(_severity) << ") "; text << (verbose ? _verboseMessage : _shortMessage); return text.str(); } else { std::string result = outputFormat; findAndReplace(result, "{id}", _id); findAndReplace(result, "{severity}", Severity::toString(_severity)); findAndReplace(result, "{message}", verbose ? _verboseMessage : _shortMessage); if (!_callStack.empty()) { std::ostringstream oss; oss << _callStack.back().line; findAndReplace(result, "{line}", oss.str()); findAndReplace(result, "{file}", _callStack.back().getfile()); } else { findAndReplace(result, "{file}", ""); findAndReplace(result, "{line}", ""); } return result; } } std::string ErrorLogger::callStackToString(const std::list &callStack) { std::ostringstream ostr; for (std::list::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).getfile() << ":" << (*tok).line << "]"; return ostr.str(); } std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const { std::string f = Path::simplifyPath(_file.c_str()); if (convert) f = Path::toNativeSeparators(f); return f; } void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file) { _file = file; _file = Path::fromNativeSeparators(_file); }