summaryrefslogtreecommitdiff
path: root/src/debug.cpp
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2019-11-24 14:56:07 +0800
committerJohn Hodge <tpg@mutabah.net>2019-11-24 14:56:07 +0800
commit25aff26fb8ced9c032c5593ad6acdd78b09d225f (patch)
tree1d47f26073416460b020d2e7714d8b1988ba8b27 /src/debug.cpp
parent6e4f7d44b72ebb911420c8dd6279b6b57189a2fd (diff)
downloadmrust-25aff26fb8ced9c032c5593ad6acdd78b09d225f.tar.gz
main - Move debug code to `debug.cpp` (preparation for moving all logic to a library)
Diffstat (limited to 'src/debug.cpp')
-rw-r--r--src/debug.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/debug.cpp b/src/debug.cpp
index 8a0d1f3c..81bb0688 100644
--- a/src/debug.cpp
+++ b/src/debug.cpp
@@ -5,7 +5,18 @@
* debug.cpp
* - Debug printing (with indenting)
*/
+#include <debug_inner.hpp>
#include <debug.hpp>
+#include <set>
+#include <iostream>
+#include <iomanip>
+#include <common.hpp> // FmtEscaped
+
+
+int g_debug_indent_level = 0;
+bool g_debug_enabled = true;
+::std::string g_cur_phase;
+::std::set< ::std::string> g_debug_disable_map;
TraceLog::TraceLog(const char* tag, ::std::function<void(::std::ostream&)> info_cb, ::std::function<void(::std::ostream&)> ret):
m_tag(tag),
@@ -50,3 +61,130 @@ TraceLog::~TraceLog() {
os << ")" << ::std::endl;
}
}
+
+
+
+bool debug_enabled_update() {
+ if( g_debug_disable_map.count(g_cur_phase) != 0 ) {
+ return false;
+ }
+ else {
+ return true;
+ }
+}
+bool debug_enabled()
+{
+ return g_debug_enabled;
+}
+::std::ostream& debug_output(int indent, const char* function)
+{
+ return ::std::cout << g_cur_phase << "- " << RepeatLitStr { " ", indent } << function << ": ";
+}
+
+DebugTimedPhase::DebugTimedPhase(const char* name):
+ m_name(name)
+{
+ ::std::cout << m_name << ": V V V" << ::std::endl;
+ g_cur_phase = m_name;
+ g_debug_enabled = debug_enabled_update();
+ m_start = clock();
+}
+DebugTimedPhase::~DebugTimedPhase()
+{
+ auto end = clock();
+ g_cur_phase = "";
+ g_debug_enabled = debug_enabled_update();
+
+ // TODO: Show wall time too?
+ ::std::cout << "(" << ::std::fixed << ::std::setprecision(2) << static_cast<double>(end - m_start) / static_cast<double>(CLOCKS_PER_SEC) << " s) ";
+ ::std::cout << m_name << ": DONE";
+ ::std::cout << ::std::endl;
+}
+
+extern void debug_init_phases(const char* env_var_name, std::initializer_list<const char*> il)
+{
+ for(const char* e : il)
+ {
+ g_debug_disable_map.insert(e);
+ }
+
+ // Mutate this map using an environment variable
+ const char* debug_string = ::std::getenv(env_var_name);
+ if( debug_string )
+ {
+ while( debug_string[0] )
+ {
+ const char* end = strchr(debug_string, ':');
+
+ ::std::string s;
+ if( end )
+ {
+ s = ::std::string { debug_string, end };
+ debug_string = end + 1;
+ }
+ else
+ {
+ s = debug_string;
+ }
+ if( g_debug_disable_map.erase(s) == 0 )
+ {
+ ::std::cerr << "WARN: Unknown compiler phase '" << s << "' in $" << env_var_name << ::std::endl;
+ }
+ if( !end ) {
+ break;
+ }
+ }
+ }
+}
+
+
+::std::ostream& operator<<(::std::ostream& os, const FmtEscaped& x)
+{
+ os << ::std::hex;
+ for(auto s = x.s; *s != '\0'; s ++)
+ {
+ switch(*s)
+ {
+ case '\0': os << "\\0"; break;
+ case '\n': os << "\\n"; break;
+ case '\\': os << "\\\\"; break;
+ case '"': os << "\\\""; break;
+ default:
+ uint8_t v = *s;
+ if( v < 0x80 )
+ {
+ if( v < ' ' || v > 0x7F )
+ os << "\\u{" << ::std::hex << (unsigned int)v << "}";
+ else
+ os << v;
+ }
+ else if( v < 0xC0 )
+ ;
+ else if( v < 0xE0 )
+ {
+ uint32_t val = (uint32_t)(v & 0x1F) << 6;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 6;
+ os << "\\u{" << ::std::hex << val << "}";
+ }
+ else if( v < 0xF0 )
+ {
+ uint32_t val = (uint32_t)(v & 0x0F) << 12;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 12;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 6;
+ os << "\\u{" << ::std::hex << val << "}";
+ }
+ else if( v < 0xF8 )
+ {
+ uint32_t val = (uint32_t)(v & 0x07) << 18;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 18;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 12;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 6;
+ os << "\\u{" << ::std::hex << val << "}";
+ }
+ break;
+ }
+ }
+ os << ::std::dec;
+ return os;
+}
+