forked from mathieu/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorlogger.cpp
More file actions
178 lines (152 loc) · 5.34 KB
/
Copy patherrorlogger.cpp
File metadata and controls
178 lines (152 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* 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, Vesa Pikki
*
* 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/
*/
#include "errorlogger.h"
#include "tokenize.h"
#include "token.h"
#include <sstream>
ErrorLogger::ErrorMessage::ErrorMessage()
{
}
#include <iostream>
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::serialize() const
{
std::ostringstream oss;
oss << _id.length() << " " << _id;
oss << _severity.length() << " " << _severity;
oss << _msg.length() << " " << _msg;
oss << _callStack.size() << " ";
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok)
{
std::ostringstream smallStream;
smallStream << (*tok).line << ":" << (*tok).file;
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;
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 = iss.get();
temp.append(1, c);
}
results.push_back(temp);
if (results.size() == 3)
break;
}
_id = results[0];
_severity = results[1];
_msg = 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 = iss.get();
temp.append(1, c);
}
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);
if (_callStack.size() >= stackSize)
break;
}
return true;
}
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();
}
void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string &msg, const std::string &id)
{
std::list<const Token *> callstack;
callstack.push_back(tok);
_writemsg(tokenizer, callstack, severity, msg, id);
}
void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string &msg, const std::string &id)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok)
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = tokenizer->file(*tok);
loc.line = (*tok)->linenr();
locationList.push_back(loc);
}
reportErr(ErrorLogger::ErrorMessage(locationList, severity, msg, id));
}
void ErrorLogger::_writemsg(const std::string &msg, const std::string &id)
{
std::ostringstream xml;
xml << "<error";
xml << " id=\"" << id << "\"";
xml << " msg=\"" << msg << "\"";
xml << ">";
std::list<ErrorLogger::ErrorMessage::FileLocation> empty;
empty.push_back(ErrorLogger::ErrorMessage::FileLocation());
reportErr(ErrorLogger::ErrorMessage(empty, "severity", msg, "id"));
}
std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack)
{
std::ostringstream ostr;
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).file << ":" << (*tok).line << "]";
return ostr.str();
}