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 179 180
|
#include "errorstack.hh"
#include <QFileInfo>
#include <logger.hh>
/* ********************************************************************************************* *
* Implementation of ErrorStack::Message
* ********************************************************************************************* */
ErrorStack::Message::Message()
: _file(), _line(0), _message()
{
// pass...
}
ErrorStack::Message::Message(const QString &file, unsigned line, const QString &message)
: _file(file), _line(line), _message(message)
{
// pass...
}
const QString &
ErrorStack::Message::file() const {
return _file;
}
unsigned
ErrorStack::Message::line() const {
return _line;
}
const QString &
ErrorStack::Message::message() const {
return _message;
}
QString
ErrorStack::Message::format() const {
return QString("In %1:%2: %3").arg(QFileInfo(file()).fileName()).arg(line()).arg(message());
}
/* ********************************************************************************************* *
* Implementation of ErrorStack::MessageStream
* ********************************************************************************************* */
ErrorStack::MessageStream::MessageStream(const ErrorStack &stack, const QString &file, unsigned line)
: QTextStream(&_message), _stack(stack), _file(file), _line(line), _message()
{
// pass...
}
ErrorStack::MessageStream::~MessageStream() {
_stack.push(Message(_file, _line, _message));
LogMessage(LogMessage::ERROR, _file, _line) << _message;
}
/* ********************************************************************************************* *
* Implementation of ErrorStack::Stack
* ********************************************************************************************* */
ErrorStack::Stack::Stack() noexcept
: _refcount(1), _errorMessageStack()
{
// pass...
}
void
ErrorStack::Stack::push(const Message &msg) {
_errorMessageStack.push_front(msg);
}
void
ErrorStack::Stack::push(const Stack &other) {
if (other.isEmpty())
return;
for (int i=(other.count()-1); i>=0; i--) {
push(other.message(i));
}
}
bool
ErrorStack::Stack::isEmpty() const {
return 0 == count();
}
unsigned
ErrorStack::Stack::count() const {
return _errorMessageStack.count();
}
const ErrorStack::Message &
ErrorStack::Stack::message(unsigned i) const {
return _errorMessageStack.at(i);
}
QString
ErrorStack::Stack::format(const QString &indent) const {
QString res;
if (isEmpty())
return res;
res += indent + message(0).format();
for (unsigned i=1; i<count(); i++)
res += QString("\n%1%2").arg(indent).arg(message(i).format());
return res;
}
void
ErrorStack::Stack::clear() {
_errorMessageStack.clear();
}
ErrorStack::Stack *
ErrorStack::Stack::ref() {
_refcount++;
return this;
}
void
ErrorStack::Stack::unref() {
if (0 < _refcount)
_refcount--;
if (0 == _refcount)
delete this;
}
/* ********************************************************************************************* *
* Implementation of ErrorStack
* ********************************************************************************************* */
ErrorStack::ErrorStack() noexcept
: _stack(new ErrorStack::Stack())
{
// pass...
}
ErrorStack::ErrorStack(const ErrorStack &other)
: _stack(other._stack->ref())
{
// pass...
}
ErrorStack::~ErrorStack() {
_stack->unref();
_stack = nullptr;
}
ErrorStack &
ErrorStack::operator =(const ErrorStack &other) {
_stack->unref();
_stack = other._stack->ref();
return *this;
}
bool
ErrorStack::isEmpty() const {
return _stack->isEmpty();
}
unsigned
ErrorStack::count() const {
return _stack->count();
}
const ErrorStack::Message &
ErrorStack::message(unsigned i) const {
return _stack->message(i);
}
void
ErrorStack::push(const Message &msg) const {
_stack->push(msg);
}
void
ErrorStack::take(const ErrorStack &other) const {
_stack->push(*other._stack);
other._stack->clear();
}
QString
ErrorStack::format(const QString &indent) const {
return _stack->format(indent);
}
|