2009-01-09 20:01:25 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2009-01-09 20:01:25 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-09 20:01:25 +01:00
|
|
|
*/
|
|
|
|
|
2009-02-01 16:47:36 +01:00
|
|
|
#include "errorlogger.h"
|
2009-01-08 18:31:07 +01:00
|
|
|
#include "tokenize.h"
|
|
|
|
#include "token.h"
|
2009-01-03 14:51:55 +01:00
|
|
|
|
2009-02-01 16:47:36 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
ErrorLogger::ErrorMessage::ErrorMessage()
|
2010-07-14 22:11:32 +02:00
|
|
|
:_severity(Severity::none)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
#include <iostream>
|
2010-07-14 22:11:32 +02:00
|
|
|
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id)
|
2009-02-10 22:51:52 +01:00
|
|
|
{
|
|
|
|
_callStack = callStack;
|
2010-07-14 22:11:32 +02:00
|
|
|
_severity = severity;
|
2009-02-10 22:51:52 +01:00
|
|
|
_msg = msg;
|
|
|
|
_id = id;
|
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
std::string ErrorLogger::ErrorMessage::serialize() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << _id.length() << " " << _id;
|
2010-07-14 18:36:34 +02:00
|
|
|
oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity);
|
2009-02-19 23:21:18 +01:00
|
|
|
oss << _msg.length() << " " << _msg;
|
|
|
|
oss << _callStack.size() << " ";
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
std::ostringstream smallStream;
|
2009-05-31 10:12:19 +02:00
|
|
|
smallStream << (*tok).line << ":" << (*tok).getfile();
|
2009-02-19 23:21:18 +01:00
|
|
|
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<std::string> results;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (iss.good())
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
unsigned int len = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!(iss >> len))
|
2009-02-19 23:21:18 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
iss.get();
|
|
|
|
std::string temp;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < len && iss.good(); ++i)
|
2009-03-08 19:50:19 +01:00
|
|
|
{
|
2009-07-20 22:24:23 +02:00
|
|
|
char c = static_cast<char>(iss.get());
|
2009-03-08 19:50:19 +01:00
|
|
|
temp.append(1, c);
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
|
|
|
results.push_back(temp);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (results.size() == 3)
|
2009-02-19 23:21:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_id = results[0];
|
2010-07-14 18:36:34 +02:00
|
|
|
_severity = Severity::fromString(results[1]);
|
2009-02-19 23:21:18 +01:00
|
|
|
_msg = results[2];
|
|
|
|
|
|
|
|
unsigned int stackSize = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!(iss >> stackSize))
|
2009-02-19 23:21:18 +01:00
|
|
|
return false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (iss.good())
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
unsigned int len = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!(iss >> len))
|
2009-02-19 23:21:18 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
iss.get();
|
|
|
|
std::string temp;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < len && iss.good(); ++i)
|
2009-03-08 19:50:19 +01:00
|
|
|
{
|
2009-07-20 22:24:23 +02:00
|
|
|
char c = static_cast<char>(iss.get());
|
2009-03-08 19:50:19 +01:00
|
|
|
temp.append(1, c);
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.file = temp.substr(temp.find(':') + 1);
|
|
|
|
temp = temp.substr(0, temp.find(':'));
|
|
|
|
std::istringstream fiss(temp);
|
|
|
|
fiss >> loc.line;
|
|
|
|
|
|
|
|
_callStack.push_back(loc);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_callStack.size() >= stackSize)
|
2009-02-19 23:21:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-15 20:36:39 +02:00
|
|
|
std::string ErrorLogger::ErrorMessage::getXMLHeader()
|
|
|
|
{
|
|
|
|
return "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<results>";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ErrorLogger::ErrorMessage::getXMLFooter()
|
|
|
|
{
|
|
|
|
return "</results>";
|
|
|
|
}
|
|
|
|
|
2009-10-27 22:10:14 +01:00
|
|
|
static std::string stringToXml(std::string s)
|
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = s.find_first_of("<>&\"", pos)) != std::string::npos)
|
2009-10-27 22:10:14 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[pos] == '<')
|
2009-10-27 22:10:14 +01:00
|
|
|
s.insert(pos + 1, "<");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (s[pos] == '>')
|
2009-10-27 22:10:14 +01:00
|
|
|
s.insert(pos + 1, ">");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (s[pos] == '&')
|
2009-10-27 22:10:14 +01:00
|
|
|
s.insert(pos + 1, "&");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (s[pos] == '"')
|
2009-10-27 22:10:14 +01:00
|
|
|
s.insert(pos + 1, """);
|
|
|
|
s.erase(pos, 1);
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-02-10 22:51:52 +01:00
|
|
|
std::string ErrorLogger::ErrorMessage::toXML() const
|
|
|
|
{
|
|
|
|
std::ostringstream xml;
|
|
|
|
xml << "<error";
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!_callStack.empty())
|
2009-04-23 21:59:26 +02:00
|
|
|
{
|
2009-10-27 22:10:14 +01:00
|
|
|
xml << " file=\"" << stringToXml(_callStack.back().getfile()) << "\"";
|
2009-04-23 21:59:26 +02:00
|
|
|
xml << " line=\"" << _callStack.back().line << "\"";
|
|
|
|
}
|
2009-02-10 22:51:52 +01:00
|
|
|
xml << " id=\"" << _id << "\"";
|
2010-07-14 18:36:34 +02:00
|
|
|
xml << " severity=\"" << Severity::toString(_severity) << "\"";
|
2009-10-27 22:10:14 +01:00
|
|
|
xml << " msg=\"" << stringToXml(_msg) << "\"";
|
2009-02-10 22:51:52 +01:00
|
|
|
xml << "/>";
|
|
|
|
return xml.str();
|
|
|
|
}
|
|
|
|
|
2009-09-05 21:01:49 +02:00
|
|
|
void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
|
2009-02-10 22:51:52 +01:00
|
|
|
{
|
2009-09-05 21:01:49 +02:00
|
|
|
std::string::size_type index = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((index = source.find(searchFor, index)) != std::string::npos)
|
2009-09-05 21:01:49 +02:00
|
|
|
{
|
|
|
|
source.replace(index, searchFor.length(), replaceWith);
|
|
|
|
index += replaceWith.length() - searchFor.length() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-14 17:31:05 +02:00
|
|
|
std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat) const
|
2009-09-05 21:01:49 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (outputFormat.length() == 0)
|
2009-09-05 21:01:49 +02:00
|
|
|
{
|
|
|
|
std::ostringstream text;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!_callStack.empty())
|
2009-09-05 21:01:49 +02:00
|
|
|
text << callStackToString(_callStack) << ": ";
|
2010-07-14 18:36:34 +02:00
|
|
|
if (_severity != Severity::none)
|
|
|
|
text << "(" << Severity::toString(_severity) << ") ";
|
2009-09-05 21:01:49 +02:00
|
|
|
text << _msg;
|
|
|
|
return text.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string result = outputFormat;
|
|
|
|
findAndReplace(result, "{id}", _id);
|
2010-07-14 18:36:34 +02:00
|
|
|
findAndReplace(result, "{severity}", Severity::toString(_severity));
|
2009-09-05 21:01:49 +02:00
|
|
|
findAndReplace(result, "{message}", _msg);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!_callStack.empty())
|
2009-09-05 21:01:49 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2009-02-10 22:51:52 +01:00
|
|
|
}
|
|
|
|
|
2009-07-10 22:38:26 +02:00
|
|
|
std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack)
|
2009-02-09 21:51:04 +01:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
|
2009-07-10 22:38:26 +02:00
|
|
|
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).getfile() << ":" << (*tok).line << "]";
|
2009-02-09 21:51:04 +01:00
|
|
|
return ostr.str();
|
2009-02-01 16:47:36 +01:00
|
|
|
}
|
2009-05-31 10:12:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const
|
|
|
|
{
|
|
|
|
std::string f(file);
|
|
|
|
|
2010-07-16 21:47:46 +02:00
|
|
|
// replace "/ab/../" with "/"..
|
2009-05-31 10:12:19 +02:00
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = f.find("..", pos + 1)) != std::string::npos)
|
2009-05-31 10:12:19 +02:00
|
|
|
{
|
|
|
|
// position must be at least 4..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos < 4)
|
2009-05-31 10:12:19 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Previous char must be a separator..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (f[pos-1] != '/' && f[pos-2] != '\\')
|
2009-05-31 10:12:19 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Next char must be a separator..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (f[pos+2] != '/' && f[pos+2] != '\\')
|
2009-05-31 10:12:19 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Locate previous separator..
|
|
|
|
std::string::size_type sep = f.find_last_of("/\\", pos - 2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (sep == std::string::npos)
|
2009-05-31 10:12:19 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Delete substring..
|
|
|
|
f.erase(sep, pos + 2 - sep);
|
|
|
|
pos = sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|