File: DebugSerializer.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (179 lines) | stat: -rw-r--r-- 4,732 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2011 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "DebugSerializer.h"

#include "scriptinterface/ScriptInterface.h"

#include "lib/secure_crt.h"
#include "lib/utf8.h"
#include "ps/CStr.h"

#include <sstream>
#include <iomanip>

/*
 * The output format here is intended to be compatible with YAML,
 * so it is human readable and usable in diff and can also be parsed with
 * external tools.
 */

// MSVC and GCC give slightly different serializations of floats
// (e.g. "1e+010" vs "1e+10"). To make the debug serialization easily comparable
// across platforms, we want to convert to a canonical form.
// TODO: we just do e+0xx now; ought to handle varying precisions and inf and nan etc too
template<typename T>
std::string canonfloat(T value, int prec)
{
	std::stringstream str;
	str << std::setprecision(prec) << value;
	std::string r = str.str();
	size_t e = r.find('e');
	if (e == r.npos) // no "e"
		return r;
	if (e == r.length() - 5 && r[e + 2] == '0') // e.g. "1e+010"
		r.erase(e + 2, 1);
	return r;
}

CDebugSerializer::CDebugSerializer(ScriptInterface& scriptInterface, std::ostream& stream, bool includeDebugInfo) :
	m_ScriptInterface(scriptInterface), m_Stream(stream), m_IsDebug(includeDebugInfo), m_Indent(0)
{
}

void CDebugSerializer::Indent(int spaces)
{
	m_Indent += spaces;
}

void CDebugSerializer::Dedent(int spaces)
{
	ENSURE(spaces <= m_Indent);
	m_Indent -= spaces;
}

#define INDENT std::string(m_Indent, ' ')

void CDebugSerializer::Comment(const std::string& comment)
{
	m_Stream << INDENT << "# " << comment << "\n";
}

void CDebugSerializer::TextLine(const std::string& text)
{
	m_Stream << INDENT << text << "\n";
}

void CDebugSerializer::PutNumber(const char* name, uint8_t value)
{
	m_Stream << INDENT << name << ": " << (int)value << "\n";
}

void CDebugSerializer::PutNumber(const char* name, int8_t value)
{
	m_Stream << INDENT << name << ": " << (int)value << "\n";
}

void CDebugSerializer::PutNumber(const char* name, uint16_t value)
{
	m_Stream << INDENT << name << ": " << value << "\n";
}

void CDebugSerializer::PutNumber(const char* name, int16_t value)
{
	m_Stream << INDENT << name << ": " << value << "\n";
}

void CDebugSerializer::PutNumber(const char* name, uint32_t value)
{
	m_Stream << INDENT << name << ": " << value << "\n";
}

void CDebugSerializer::PutNumber(const char* name, int32_t value)
{
	m_Stream << INDENT << name << ": " << value << "\n";
}

void CDebugSerializer::PutNumber(const char* name, float value)
{
	m_Stream << INDENT << name << ": " << canonfloat(value, 8) << "\n";
}

void CDebugSerializer::PutNumber(const char* name, double value)
{
	m_Stream << INDENT << name << ": " << canonfloat(value, 17) << "\n";
}

void CDebugSerializer::PutNumber(const char* name, fixed value)
{
	m_Stream << INDENT << name << ": " << value.ToString() << "\n";
}

void CDebugSerializer::PutBool(const char* name, bool value)
{
	m_Stream << INDENT << name << ": " << (value ? "true" : "false") << "\n";
}

void CDebugSerializer::PutString(const char* name, const std::string& value)
{
	std::string escaped;
	escaped.reserve(value.size());
	for (size_t i = 0; i < value.size(); ++i)
		if (value[i] == '"')
			escaped += "\\\"";
		else if (value[i] == '\\')
			escaped += "\\\\";
		else if (value[i] == '\n')
			escaped += "\\n";
		else
			escaped += value[i];

	m_Stream << INDENT << name << ": " << "\"" << escaped << "\"\n";
}

void CDebugSerializer::PutScriptVal(const char* name, JS::MutableHandleValue value)
{
	std::wstring source = m_ScriptInterface.ToString(value, true);

	m_Stream << INDENT << name << ": " << utf8_from_wstring(source) << "\n";
}

void CDebugSerializer::PutRaw(const char* name, const u8* data, size_t len)
{
	m_Stream << INDENT << name << ": (" << len << " bytes)";

	char buf[4];
	for (size_t i = 0; i < len; ++i)
	{
		sprintf_s(buf, ARRAY_SIZE(buf), " %02x", (unsigned int)data[i]);
		m_Stream << buf;
	}

	m_Stream << "\n";
}

bool CDebugSerializer::IsDebug() const
{
	return m_IsDebug;
}

std::ostream& CDebugSerializer::GetStream()
{
	return m_Stream;
}