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
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
|
|
|
* Leandro Penz, Kimmo Varis
|
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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
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-10 22:51:52 +01:00
|
|
|
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id)
|
|
|
|
{
|
|
|
|
_callStack = callStack;
|
|
|
|
_severity = severity;
|
|
|
|
_msg = msg;
|
|
|
|
_id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ErrorLogger::ErrorMessage::toXML() const
|
|
|
|
{
|
|
|
|
std::ostringstream xml;
|
|
|
|
xml << "<error";
|
|
|
|
xml << " file=\"" << _callStack.back().file << "\"";
|
|
|
|
xml << " line=\"" << _callStack.back().line << "\"";
|
|
|
|
xml << " id=\"" << _id << "\"";
|
|
|
|
xml << " severity=\"" << _severity << "\"";
|
|
|
|
xml << " msg=\"" << _msg << "\"";
|
|
|
|
xml << "/>";
|
|
|
|
return xml.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ErrorLogger::ErrorMessage::toText() const
|
|
|
|
{
|
|
|
|
std::ostringstream text;
|
|
|
|
text << callStackToString(_callStack) << ": (" << _severity << ") " << _msg;
|
|
|
|
return text.str();
|
|
|
|
}
|
|
|
|
|
2009-02-08 22:20:35 +01:00
|
|
|
void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string msg, const std::string &id)
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
2009-02-01 19:00:47 +01:00
|
|
|
std::list<const Token *> callstack;
|
|
|
|
callstack.push_back(tok);
|
2009-02-08 22:20:35 +01:00
|
|
|
_writemsg(tokenizer, callstack, severity, msg, id);
|
2009-02-01 16:47:36 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 22:20:35 +01:00
|
|
|
void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string msg, const std::string &id)
|
2009-01-03 14:51:55 +01:00
|
|
|
{
|
2009-02-10 22:51:52 +01:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
2009-02-09 21:51:04 +01:00
|
|
|
for (std::list<const Token *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok)
|
|
|
|
{
|
2009-02-10 22:51:52 +01:00
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
2009-02-09 21:51:04 +01:00
|
|
|
loc.file = tokenizer->file(*tok);
|
|
|
|
loc.line = (*tok)->linenr();
|
|
|
|
locationList.push_back(loc);
|
|
|
|
}
|
|
|
|
|
2009-02-10 22:51:52 +01:00
|
|
|
reportErr(ErrorLogger::ErrorMessage(locationList, severity, msg, id));
|
2009-01-03 14:51:55 +01:00
|
|
|
}
|
|
|
|
|
2009-01-08 18:31:07 +01:00
|
|
|
|
2009-02-08 22:20:35 +01:00
|
|
|
void ErrorLogger::_writemsg(const std::string msg, const std::string &id)
|
2009-02-01 16:47:36 +01:00
|
|
|
{
|
2009-02-01 19:00:47 +01:00
|
|
|
std::ostringstream xml;
|
|
|
|
xml << "<error";
|
|
|
|
xml << " id=\"" << id << "\"";
|
|
|
|
xml << " msg=\"" << msg << "\"";
|
|
|
|
xml << ">";
|
|
|
|
|
2009-02-10 22:51:52 +01:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> empty;
|
|
|
|
empty.push_back(ErrorLogger::ErrorMessage::FileLocation());
|
|
|
|
reportErr(ErrorLogger::ErrorMessage(empty, "severity", msg, "id"));
|
2009-02-09 21:51:04 +01:00
|
|
|
}
|
|
|
|
|
2009-02-10 22:51:52 +01:00
|
|
|
std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack)
|
2009-02-09 21:51:04 +01:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2009-02-10 22:51:52 +01:00
|
|
|
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
|
2009-02-09 21:51:04 +01:00
|
|
|
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).file << ":" << (*tok).line << "]";
|
|
|
|
return ostr.str();
|
2009-02-01 16:47:36 +01:00
|
|
|
}
|