1 /**************************************************************************
3 * Copyright 2007-2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
41 static gzFile g_gzFile = NULL;
42 static void _Close(void) {
43 if(g_gzFile != NULL) {
49 static void _Open(const char *szName, const char *szExtension) {
52 static unsigned dwCounter = 0;
54 char szProcessName[PATH_MAX];
55 char szFileName[PATH_MAX];
57 OS::GetProcessName(szProcessName, PATH_MAX);
63 snprintf(szFileName, PATH_MAX, "%s.%s.%u.%s.gz", szProcessName, szName, dwCounter, szExtension);
65 snprintf(szFileName, PATH_MAX, "%s.%s.%s.gz", szProcessName, szName, szExtension);
67 file = fopen(szFileName, "rb");
76 fprintf(stderr, "Logging to %s\n", szFileName);
77 g_gzFile = gzopen(szFileName, "wb");
80 static inline void Write(const char *sBuffer, size_t dwBytesToWrite) {
84 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
87 static inline void Write(const char *szText) {
88 Write(szText, strlen(szText));
91 static inline void Write(char c)
97 WriteF(const char *format, ...)
101 va_start(ap, format);
102 vsnprintf(szBuffer, sizeof(szBuffer), format, ap);
136 if (c >= 0x20 && c <= 0x7e) {
145 Escape(const char *s)
148 while((c = *s++) != 0) {
154 Escape(const wchar_t *s)
157 while((c = *s++) != 0) {
163 EscapeF(const char *format, ...)
167 va_start(ap, format);
168 vsnprintf(szBuffer, sizeof(szBuffer), format, ap);
174 Indent(unsigned level) {
175 for(unsigned i = 0; i < level; ++i)
185 Tag(const char *name) {
192 BeginTag(const char *name) {
199 BeginTag(const char *name,
200 const char *attr1, const char *value1) {
211 BeginTag(const char *name,
212 const char *attr1, const char *value1,
213 const char *attr2, const char *value2) {
228 BeginTag(const char *name,
229 const char *attr1, const char *value1,
230 const char *attr2, const char *value2,
231 const char *attr3, const char *value3) {
250 EndTag(const char *name) {
256 void Open(const char *name) {
258 Write("<?xml version='1.0' encoding='UTF-8'?>");
260 Write("<?xml-stylesheet type='text/xsl' href='apitrace.xsl'?>");
272 void BeginCall(const char *function) {
275 BeginTag("call", "name", function);
283 gzflush(g_gzFile, Z_SYNC_FLUSH);
287 void BeginArg(const char *type, const char *name) {
289 BeginTag("arg", "type", type, "name", name);
297 void BeginReturn(const char *type) {
299 BeginTag("ret", "type", type);
302 void EndReturn(void) {
307 void BeginArray(const char *type, size_t length)
309 BeginTag("array", "type", type);
317 void BeginElement(const char *type)
319 BeginTag("elem", "type", type);
322 void EndElement(void)
327 void BeginStruct(const char *type)
329 BeginTag("struct", "type", type);
337 void BeginMember(const char *type, const char *name)
339 BeginTag("member", "type", type, "name", name);
347 void BeginBitmask(const char *type)
352 void EndBitmask(void)
357 void BeginPointer(const char *type, const void *addr)
360 snprintf(saddr, sizeof(saddr), "%p", addr);
361 BeginTag("ref", "type", type, "addr", saddr);
364 void EndPointer(void)
369 void LiteralBool(bool value)
372 WriteF("%u", value ? 0 : 1);
376 void LiteralSInt(signed long long value)
379 WriteF("%lli", value);
383 void LiteralUInt(unsigned long long value)
386 WriteF("%llu", value);
390 void LiteralFloat(double value)
397 void LiteralString(const char *str)
408 void LiteralWString(const wchar_t *str)
419 void LiteralNamedConstant(const char *str)
426 void LiteralNull(void)
431 void LiteralOpaque(void)
436 } /* namespace Log */