Commit 9ed8c428 authored by Martin Marinov's avatar Martin Marinov
Browse files

Cosmetic changes to the CallStack debug output, rendering the text somewhat...

Cosmetic changes to the CallStack debug output, rendering the text somewhat shorter and removing the depth indentation.
parent cf012644
......@@ -10,7 +10,7 @@ namespace Debug {
namespace {
// Replace interior of < > in function name with . for brevity
void compact_name(const char* _func, std::string& _str)
void append_function(std::string& _str, const char* const _func)
{
int cnt = 0;
const char* ptr = _func;
......@@ -29,29 +29,25 @@ void compact_name(const char* _func, std::string& _str)
}
// Add to the string the call stack element string
void add_to_string(const Enter* _deb, std::string& _str,
const bool _strip_angled, const bool _with_counts,
const Enter* _prev)
void append_entry(std::string& _str, const Enter* const _entr,
const bool _cmpct_tmpl, const bool _entr_nmbr, const Enter* const _prev_entr)
{
if (_prev == nullptr || strcmp(_prev->function(), _deb->function()) != 0)
{
// Writes the function name if it is not the same as previous
// function in the call stack.
if (_prev != nullptr)
_str.append("->");
if (_strip_angled)
compact_name(_deb->function(), _str);
else
_str.append(_deb->function());
if (_prev_entr != nullptr && // there is a previous entry and it is the same?
strcmp(_prev_entr->function(), _entr->function()) != 0)
{// ... so do nothing
return;
}
_str.append("[");
if (!_with_counts)
_str.append("*");
if (_prev_entr != nullptr)
_str.append("/");
if (_cmpct_tmpl)
append_function(_str, _entr->function());
else
_str.append(std::to_string(_deb->number()));
_str.append("]");
_str.append(_entr->function());
if (_entr_nmbr)
_str.append('.' + std::to_string(_entr->number()));
}
}//namespace
......@@ -82,11 +78,11 @@ const CallStack& CallStack::query()
//}
//
void CallStack::append(std::string& _str, const bool _with_counts) const
void CallStack::append(std::string& _str, const bool _entr_nmbr) const
{
const Enter* prev = nullptr;
for (size_t i = 0, n = depth(); i < n; prev = calls_[i++])
add_to_string(calls_[i], _str, true, _with_counts, prev);
append_entry(_str, calls_[i], true, _entr_nmbr, prev);
}
void CallStack::append_indent(std::string& _str, const int _indt,
......
......@@ -38,7 +38,7 @@ public:
/*!
Append the full call stack.
*/
void append(std::string& _str, const bool _with_counts = true) const;
void append(std::string& _str, const bool _entr_nmbr = true) const;
/*!
Append and indent to the string
......
......@@ -48,11 +48,10 @@ public:
public:
Impl(const char* const _flnm, const uint _flags = APPEND | RETAIN)
: flags_(_flags), num_flush_(0)
: flags_(_flags), num_flush_(0), indt_(2), at_line_start_(false)
{
set_filename(_flnm);
indent_size_ = 3;
at_line_start_ = false; // Don't want to indent header
}
~Impl()
......@@ -87,11 +86,6 @@ public:
return true;
}
int indent_size()
{
return indent_size_;
}
bool file_is_open() const
{
return file_stream_.is_open();
......@@ -120,34 +114,27 @@ public:
void clear()
{
current_.clear();
bffr_.clear();
output_.clear();
flnm_.clear();
}
char prev_char() const
{
if (!current_.empty())
return current_.back();
if (!bffr_.empty())
return bffr_.back();
if (!output_.empty())
return output_.back();
return '\0';
}
void indent(bool _full_text)
{
if (indent_size() == 0)
return;
CallStack::query().append_indent(current_, indent_size(),
_full_text && is_html());
}
void indent(bool /*_full_text*/) { bffr_.append(indt_, ' '); }
void line_break(bool _with_indent = false)
{
if (is_html())
current_.append("<br>"); // Don't bother with matching </br>
current_.append("\n", 1);
bffr_.append("<br>"); // Don't bother with matching </br>
bffr_.append(1, '\n');
if (_with_indent)
indent(false);
......@@ -157,7 +144,7 @@ public:
void print_direct(const std::string& _s)
{
current_.append(_s);
bffr_.append(_s);
}
void print(const char _c)
......@@ -183,17 +170,17 @@ public:
if (_c == -89) // 167 = -89
{
current_.append("&sect;");
bffr_.append("&sect;");
return;
}
if (_c == -80) // -80
{
current_.append("&deg;");
bffr_.append("&deg;");
return;
}
}
current_.append(&_c, 1);
bffr_.append(&_c, 1);
}
void print_to_ostream(const char* const _s, std::ostream& os)
......@@ -428,60 +415,55 @@ public:
return num_flush_ == 0 ;
}
int flush()
bool flush()
{
int res = 0;
if (!current_.empty())
if (bffr_.empty())
return true;
const char* const flnm = filename();
if (flnm == nullptr && !file_is_open())
return false;
if (!file_is_open())
{
file_stream_.open(flnm, std::fstream::out |
((is_appended() && !is_first_flush()) ?
std::fstream::app : std::fstream::trunc));
}
if (!file_is_open()) // failed opening the file
return false;
std::string hdr;
if (!is_appended())
{// Re-output entire file
header(hdr);
output_.append(hdr);
file_stream_ << output_;
}
else
{
const char* fname = filename();
if ((fname != nullptr) || file_is_open())
if (is_first_flush())
{
if (!file_is_open())
{
file_stream_.open(fname,
std::fstream::out | ((is_appended() && !is_first_flush()) ?
std::fstream::app : std::fstream::trunc));
}
if (file_stream_.is_open())
{
std::string hdr;
if (!is_appended())
{
// Reoutput entire file
header(hdr);
output_.append(hdr);
file_stream_ << output_;
}
else
{
if (is_first_flush())
{
header(hdr);
if (is_retained())
output_.append(hdr);
file_stream_ << hdr;
}
++num_flush_;
}
file_stream_ << current_;
if (is_retained())
output_.append(current_);
current_.clear();
if (!is_kept_open())
file_stream_.close();
} // endif fs.is_open
else
res = -1;
header(hdr);
if (is_retained())
output_.append(hdr);
file_stream_ << hdr;
}
} // endif current empty
return res;
} // endfunc flush
++num_flush_;
}
file_stream_ << bffr_;
// Use with extreme caution.
const std::string& string() const { return current_; }
if (is_retained())
output_.append(bffr_);
bffr_.clear();
if (!is_kept_open())
file_stream_.close();
return true;
}
void close()
{
......@@ -489,7 +471,7 @@ public:
flush();
}
void set_filename(const char* _flnm)
void set_filename(const char* const _flnm)
{
flnm_ = _flnm != nullptr ? _flnm : "";
if (is_html_filename(_flnm))
......@@ -504,7 +486,6 @@ public:
void enter(const int _id)
{
// First DEB_out in this function so output call-stack, and flush.
indent(true);
std::string str;
if (is_html())
......@@ -513,10 +494,7 @@ public:
anchor(str, _id, "enter", true);
}
else
{
// .txt call stack lead in
str.append("****>");
}
str.append("*>"); // .txt call stack lead in
CallStack::query().append(str);
......@@ -532,11 +510,11 @@ private:
uint flags_;
int num_flush_;
int priority_; // Last permission granted
int indent_size_;
size_t indt_;
bool at_line_start_;
std::string current_;
std::string bffr_;
std::string output_;
std::string flnm_;
std::fstream file_stream_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment