1 /****************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc.
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ****************************************************************************/
33 #define PATH_MAX _MAX_PATH
36 #define snprintf _snprintf
39 #define vsnprintf _vsnprintf
47 static gzFile g_gzFile = NULL;
48 static char g_szFileName[PATH_MAX];
50 static void _Close(void) {
51 if(g_gzFile != NULL) {
57 static void _Open(const char *szName, const char *szExtension) {
60 static unsigned dwCounter = 0;
66 snprintf(g_szFileName, PATH_MAX, "%s.%u.%s.gz", szName, dwCounter, szExtension);
68 snprintf(g_szFileName, PATH_MAX, "%s.%s.gz", szName, szExtension);
70 file = fopen(g_szFileName, "rb");
79 g_gzFile = gzopen(g_szFileName, "wb");
82 static inline void _ReOpen(void) {
86 static inline void Write(const char *sBuffer, size_t dwBytesToWrite) {
90 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
93 static inline void Write(const char *szText) {
94 Write(szText, strlen(szText));
98 Escape(const char *s) {
104 Indent(unsigned level) {
105 for(unsigned i = 0; i < level; ++i)
115 Tag(const char *name) {
122 BeginTag(const char *name) {
129 BeginTag(const char *name,
130 const char *attr1, const char *value1) {
141 BeginTag(const char *name,
142 const char *attr1, const char *value1,
143 const char *attr2, const char *value2) {
158 BeginTag(const char *name,
159 const char *attr1, const char *value1,
160 const char *attr2, const char *value2,
161 const char *attr3, const char *value3) {
180 EndTag(const char *name) {
186 void Open(const char *name) {
188 Write("<?xml version='1.0' encoding='UTF-8'?>");
190 Write("<?xml-stylesheet type='text/xsl' href='apitrace.xsl'?>");
206 void Text(const char *text) {
210 static void TextChar(char c) {
217 void TextF(const char *format, ...) {
220 va_start(ap, format);
221 vsnprintf(szBuffer, sizeof(szBuffer), format, ap);
226 void BeginCall(const char *function) {
228 BeginTag("call", "name", function);
236 gzflush(g_gzFile, Z_SYNC_FLUSH);
239 void BeginArg(const char *type, const char *name) {
241 BeginTag("arg", "type", type, "name", name);
249 void BeginReturn(const char *type) {
251 BeginTag("ret", "type", type);
254 void EndReturn(void) {
259 void BeginElement(const char *type, const char *name) {
260 BeginTag("elem", "type", type, "name", name);
263 void BeginElement(const char *type) {
264 BeginTag("elem", "type", type);
267 void EndElement(void) {
271 void BeginReference(const char *type, const void *addr) {
273 snprintf(saddr, sizeof(saddr), "%p", addr);
274 BeginTag("ref", "type", type, "addr", saddr);
277 void EndReference(void) {
281 void DumpString(const char *str) {
282 const unsigned char *p = (const unsigned char *)str;
285 while((c = *p++) != 0) {
290 else if(c >= 0x20 && c <= 0x7e)
299 unsigned char octal0 = c & 0x7;
300 unsigned char octal1 = (c >> 3) & 0x7;
301 unsigned char octal2 = (c >> 3) & 0x7;
303 TextF("\\%u%u%u", octal2, octal1, octal0);
305 TextF("\\%u%u", octal1, octal0);
307 TextF("\\%u", octal0);
313 } /* namespace Log */