forked from Psiphon-Inc/psicash-lib-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.cpp
91 lines (74 loc) · 2.33 KB
/
error.cpp
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
/*
* Copyright (c) 2018, Psiphon Inc.
* All rights reserved.
*
* 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 <sstream>
#include <memory>
#include "error.hpp"
using namespace std;
namespace psicash {
namespace error {
Error::Error()
: is_error_(false), critical_(false) {
}
Error::Error(bool critical, const std::string& message, const std::string& filename,
const std::string& function, int line)
: is_error_(true), critical_(critical) {
Wrap(message, filename, function, line);
}
Error& Error::Wrap(const std::string& message, const std::string& filename,
const std::string& function, int line) {
if (!is_error_) {
// This is a non-error, so there's nothing to wrap.
return *this;
}
// We don't want the full absolute file path.
string f = filename;
auto last_slash = f.find_last_of("/\\");
if (last_slash != string::npos) {
f = f.substr(last_slash + 1);
}
stack_.push_back({message, f, function, line});
return *this;
}
Error& Error::Wrap(const std::string& filename, const std::string& function, int line) {
return Wrap("", filename, function, line);
}
string Error::ToString() const {
if (!is_error_) {
return "(nonerror)";
}
bool first = true;
ostringstream os;
if (Critical()) {
os << "CRITICAL: ";
}
for (const auto& sf : stack_) {
if (!first) {
os << endl;
}
first = false;
os << sf.message << " (" << sf.filename << ":" << sf.function << ":" << sf.line << ")";
}
return os.str();
}
std::ostream& operator<<(std::ostream& os, const Error& err) {
os << err.ToString();
return os;
}
} // namespace error
} // namespace psicash