KUDr@7618: /* $Id$ */ KUDr@7614: rubidium@10429: /** @file dbg_helpers.cpp Helpers for outputting debug information. */ rubidium@10429: KUDr@7614: #include "../stdafx.h" rubidium@8619: #include "../openttd.h" rubidium@8596: #include "../direction_type.h" smatz@10473: #include "../strings_type.h" KUDr@7614: #include "../rail.h" KUDr@7614: #include "../rail_map.h" KUDr@7614: #include "dbg_helpers.h" KUDr@7614: KUDr@7614: /** Trackdir & TrackdirBits short names. */ KUDr@7614: static const char* trackdir_names[] = { KUDr@7614: "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse", KUDr@7614: "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw", KUDr@7614: }; KUDr@7614: KUDr@7614: /** Return name of given Trackdir. */ KUDr@7614: CStrA ValueStr(Trackdir td) KUDr@7614: { KUDr@7614: CStrA out; KUDr@7614: out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV")); KUDr@7614: return out.Transfer(); KUDr@7614: } KUDr@7614: KUDr@7614: /** Return composed name of given TrackdirBits. */ KUDr@7614: CStrA ValueStr(TrackdirBits td_bits) KUDr@7614: { KUDr@7614: CStrA out; KUDr@7614: out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data()); KUDr@7614: return out.Transfer(); KUDr@7614: } KUDr@7614: KUDr@7614: KUDr@7614: /** DiagDirection short names. */ KUDr@7614: static const char* diagdir_names[] = { KUDr@7614: "NE", "SE", "SW", "NW", KUDr@7614: }; KUDr@7614: KUDr@7614: /** Return name of given DiagDirection. */ KUDr@7614: CStrA ValueStr(DiagDirection dd) KUDr@7614: { KUDr@7614: CStrA out; KUDr@7614: out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV")); KUDr@7614: return out.Transfer(); KUDr@7614: } KUDr@7614: KUDr@7614: KUDr@7614: /** SignalType short names. */ KUDr@7614: static const char* signal_type_names[] = { KUDr@7614: "NORMAL", "ENTRY", "EXIT", "COMBO", KUDr@7614: }; KUDr@7614: KUDr@7614: /** Return name of given SignalType. */ KUDr@7614: CStrA ValueStr(SignalType t) KUDr@7614: { KUDr@7614: CStrA out; KUDr@7614: out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK")); KUDr@7614: return out.Transfer(); KUDr@7614: } KUDr@7614: KUDr@7614: KUDr@7614: /** Translate TileIndex into string. */ KUDr@7614: CStrA TileStr(TileIndex tile) KUDr@7614: { KUDr@7614: CStrA out; KUDr@7614: out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile)); KUDr@7614: return out.Transfer(); KUDr@7614: } KUDr@7614: KUDr@7614: /** Keep track of the last assigned type_id. Used for anti-recursion. */ KUDr@7614: /*static*/ size_t& DumpTarget::LastTypeId() KUDr@7614: { KUDr@7614: static size_t last_type_id = 0; KUDr@7614: return last_type_id; KUDr@7614: } KUDr@7614: KUDr@7614: /** Return structured name of the current class/structure. */ KUDr@7614: CStrA DumpTarget::GetCurrentStructName() KUDr@7614: { KUDr@7614: CStrA out; KUDr@7614: if (!m_cur_struct.empty()) { KUDr@7614: // we are inside some named struct, return its name KUDr@7614: out = m_cur_struct.top(); KUDr@7614: } KUDr@7614: return out.Transfer(); KUDr@7614: } KUDr@7614: KUDr@7614: /** KUDr@7614: * Find the given instance in our anti-recursion repository. KUDr@7614: * Return true and set name when object was found. KUDr@7614: */ KUDr@7614: bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name) KUDr@7614: { KUDr@7614: KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr)); KUDr@7614: if (it != m_known_names.end()) { KUDr@7614: /* we have found it */ KUDr@7614: name = (*it).second; KUDr@7614: return true; KUDr@7614: } KUDr@7614: return false; KUDr@7614: } KUDr@7614: KUDr@7614: /** Write some leading spaces into the output. */ KUDr@7614: void DumpTarget::WriteIndent() KUDr@7614: { KUDr@7614: int num_spaces = 2 * m_indent; KUDr@7614: memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces); KUDr@7614: } KUDr@7614: KUDr@7614: /** Write a line with indent at the beginning and at the end. */ KUDr@7614: void DumpTarget::WriteLine(const char *format, ...) KUDr@7614: { KUDr@7614: WriteIndent(); KUDr@7614: va_list args; KUDr@7614: va_start(args, format); KUDr@7614: m_out.AddFormatL(format, args); KUDr@7614: va_end(args); KUDr@7614: m_out.AppendStr("\n"); KUDr@7614: } KUDr@7614: KUDr@7614: /** Write 'name = value' with indent and new-line. */ KUDr@7614: void DumpTarget::WriteValue(const char *name, const char *value_str) KUDr@7614: { KUDr@7614: WriteIndent(); KUDr@7614: m_out.AddFormat("%s = %s\n", name, value_str); KUDr@7614: } KUDr@7614: KUDr@7614: /** Write name & TileIndex to the output. */ KUDr@7614: void DumpTarget::WriteTile(const char *name, TileIndex tile) KUDr@7614: { KUDr@7614: WriteIndent(); KUDr@7614: m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data()); KUDr@7614: } KUDr@7614: KUDr@7614: /** KUDr@7614: * Open new structure (one level deeper than the current one) 'name = {'. KUDr@7614: */ KUDr@7614: void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr) KUDr@7614: { KUDr@7614: /* make composite name */ KUDr@7614: CStrA cur_name = GetCurrentStructName().Transfer(); KUDr@7614: if (cur_name.Size() > 0) { KUDr@7614: /* add name delimiter (we use structured names) */ KUDr@7614: cur_name.AppendStr("."); KUDr@7614: } KUDr@7614: cur_name.AppendStr(name); KUDr@7614: KUDr@7614: /* put the name onto stack (as current struct name) */ KUDr@7614: m_cur_struct.push(cur_name); KUDr@7614: KUDr@7614: /* put it also to the map of known structures */ KUDr@7614: m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name)); KUDr@7614: KUDr@7614: WriteIndent(); KUDr@7614: m_out.AddFormat("%s = {\n", name); KUDr@7614: m_indent++; KUDr@7614: } KUDr@7614: KUDr@7614: /** KUDr@7614: * Close structure '}'. KUDr@7614: */ KUDr@7614: void DumpTarget::EndStruct() KUDr@7614: { KUDr@7614: m_indent--; KUDr@7614: WriteIndent(); KUDr@7614: m_out.AddFormat("}\n"); KUDr@7614: KUDr@7614: /* remove current struct name from the stack */ KUDr@7614: m_cur_struct.pop(); KUDr@7614: } KUDr@7614: