summaryrefslogtreecommitdiff
path: root/src/utils.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2012-03-18 01:01:30 -0500
committerJohn Wiegley <johnw@newartisans.com>2012-03-18 01:01:30 -0500
commitf9088f88360019bb4be8743dd8091036502adb9c (patch)
tree8a4fe8e1bba0ccc65a1809874ebbd249577fba57 /src/utils.cc
parente7d26d53cfc73cc706a6b4903f5c5d6641bdeae8 (diff)
downloadfork-ledger-f9088f88360019bb4be8743dd8091036502adb9c.tar.gz
fork-ledger-f9088f88360019bb4be8743dd8091036502adb9c.tar.bz2
fork-ledger-f9088f88360019bb4be8743dd8091036502adb9c.zip
Added --verify-memory and missing TRACE_[CD]TOR calls
Diffstat (limited to 'src/utils.cc')
-rw-r--r--src/utils.cc110
1 files changed, 84 insertions, 26 deletions
diff --git a/src/utils.cc b/src/utils.cc
index 628fb158..5a364008 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -270,13 +270,79 @@ void operator delete[](void * ptr, const std::nothrow_t&) throw() {
namespace ledger {
-inline void report_count_map(std::ostream& out, object_count_map& the_map)
-{
- foreach (object_count_map::value_type& pair, the_map)
- out << " " << std::right << std::setw(12) << pair.second.first
- << " " << std::right << std::setw(7) << pair.second.second
- << " " << std::left << pair.first
- << std::endl;
+namespace {
+ void stream_commified_number(std::ostream& out, std::size_t num)
+ {
+ std::ostringstream buf;
+ std::ostringstream obuf;
+
+ buf << num;
+
+ int integer_digits = 0;
+ // Count the number of integer digits
+ for (const char * p = buf.str().c_str(); *p; p++) {
+ if (*p == '.')
+ break;
+ else if (*p != '-')
+ integer_digits++;
+ }
+
+ for (const char * p = buf.str().c_str(); *p; p++) {
+ if (*p == '.') {
+ obuf << *p;
+ assert(integer_digits <= 3);
+ }
+ else if (*p == '-') {
+ obuf << *p;
+ }
+ else {
+ obuf << *p;
+
+ if (integer_digits > 3 && --integer_digits % 3 == 0)
+ obuf << ',';
+ }
+ }
+
+ out << obuf.str();
+ }
+
+ void stream_memory_size(std::ostream& out, std::size_t size)
+ {
+ std::ostringstream obuf;
+
+ if (size > 10 * 1024 * 1024)
+ obuf << "\033[1m";
+ if (size > 100 * 1024 * 1024)
+ obuf << "\033[31m";
+
+ obuf << std::setw(7);
+
+ if (size < 1024)
+ obuf << size << 'b';
+ else if (size < (1024 * 1024))
+ obuf << int(double(size) / 1024.0) << 'K';
+ else if (size < (1024 * 1024 * 1024))
+ obuf << int(double(size) / (1024.0 * 1024.0)) << 'M';
+ else
+ obuf << int(double(size) / (1024.0 * 1024.0 * 1024.0)) << 'G';
+
+ if (size > 10 * 1024 * 1024)
+ obuf << "\033[0m";
+
+ out << obuf.str();
+ }
+
+ void report_count_map(std::ostream& out, object_count_map& the_map)
+ {
+ foreach (object_count_map::value_type& pair, the_map) {
+ out << " " << std::right << std::setw(12);
+ stream_commified_number(out, pair.second.first);
+ out << " " << std::right << std::setw(7);
+ stream_memory_size(out, pair.second.second);
+ out << " " << std::left << pair.first
+ << std::endl;
+ }
+ }
}
std::size_t current_objects_size()
@@ -354,7 +420,7 @@ void trace_dtor_func(void * ptr, const char * cls_name, std::size_t cls_size)
void report_memory(std::ostream& out, bool report_all)
{
- if (! live_memory || ! memory_tracing_active) return;
+ if (! live_memory) return;
if (live_memory_count->size() > 0) {
out << "NOTE: There may be memory held by Boost "
@@ -366,11 +432,13 @@ void report_memory(std::ostream& out, bool report_all)
if (live_memory->size() > 0) {
out << "Live memory:" << std::endl;
- foreach (const memory_map::value_type& pair, *live_memory)
+ foreach (const memory_map::value_type& pair, *live_memory) {
out << " " << std::right << std::setw(12) << pair.first
- << " " << std::right << std::setw(7) << pair.second.second
- << " " << std::left << pair.second.first
+ << " " << std::right << std::setw(7);
+ stream_memory_size(out, pair.second.second);
+ out << " " << std::left << pair.second.first
<< std::endl;
+ }
}
if (report_all && total_memory_count->size() > 0) {
@@ -386,11 +454,13 @@ void report_memory(std::ostream& out, bool report_all)
if (live_objects->size() > 0) {
out << "Live objects:" << std::endl;
- foreach (const objects_map::value_type& pair, *live_objects)
+ foreach (const objects_map::value_type& pair, *live_objects) {
out << " " << std::right << std::setw(12) << pair.first
- << " " << std::right << std::setw(7) << pair.second.second
- << " " << std::left << pair.second.first
+ << " " << std::right << std::setw(7);
+ stream_memory_size(out, pair.second.second);
+ out << " " << std::left << pair.second.first
<< std::endl;
+ }
}
if (report_all) {
@@ -529,18 +599,6 @@ std::ostringstream _log_buffer;
uint8_t _trace_level;
#endif
-static inline void stream_memory_size(std::ostream& out, std::size_t size)
-{
- if (size < 1024)
- out << size << 'b';
- else if (size < (1024 * 1024))
- out << (double(size) / 1024.0) << 'K';
- else if (size < (1024 * 1024 * 1024))
- out << (double(size) / (1024.0 * 1024.0)) << 'M';
- else
- out << (double(size) / (1024.0 * 1024.0 * 1024.0)) << 'G';
-}
-
static bool logger_has_run = false;
static ptime logger_start;