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 _ReOpen(void) {
84 static inline void Write(const char *sBuffer, size_t dwBytesToWrite) {
88 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
91 static inline void Write(const char *szText) {
92 Write(szText, strlen(szText));
95 static inline void Write(char c)
101 WriteF(const char *format, ...)
105 va_start(ap, format);
106 vsnprintf(szBuffer, sizeof(szBuffer), format, ap);
140 if (c >= 0x20 && c <= 0x7e) {
149 Escape(const char *s)
152 while((c = *s++) != 0) {
158 Escape(const wchar_t *s)
161 while((c = *s++) != 0) {
167 EscapeF(const char *format, ...)
171 va_start(ap, format);
172 vsnprintf(szBuffer, sizeof(szBuffer), format, ap);
178 Indent(unsigned level) {
179 for(unsigned i = 0; i < level; ++i)
189 Tag(const char *name) {
196 BeginTag(const char *name) {
203 BeginTag(const char *name,
204 const char *attr1, const char *value1) {
215 BeginTag(const char *name,
216 const char *attr1, const char *value1,
217 const char *attr2, const char *value2) {
232 BeginTag(const char *name,
233 const char *attr1, const char *value1,
234 const char *attr2, const char *value2,
235 const char *attr3, const char *value3) {
254 EndTag(const char *name) {
260 void Open(const char *name) {
262 Write("<?xml version='1.0' encoding='UTF-8'?>");
264 Write("<?xml-stylesheet type='text/xsl' href='apitrace.xsl'?>");
280 void BeginCall(const char *function) {
283 BeginTag("call", "name", function);
291 gzflush(g_gzFile, Z_SYNC_FLUSH);
295 void BeginArg(const char *type, const char *name) {
297 BeginTag("arg", "type", type, "name", name);
305 void BeginReturn(const char *type) {
307 BeginTag("ret", "type", type);
310 void EndReturn(void) {
315 void BeginArray(const char *type, size_t length)
317 BeginTag("array", "type", type);
325 void BeginElement(const char *type)
327 BeginTag("elem", "type", type);
330 void EndElement(void)
335 void BeginStruct(const char *type)
337 BeginTag("struct", "type", type);
345 void BeginMember(const char *type, const char *name)
347 BeginTag("member", "type", type, "name", name);
355 void BeginBitmask(const char *type)
360 void EndBitmask(void)
365 void BeginReference(const char *type, const void *addr)
368 snprintf(saddr, sizeof(saddr), "%p", addr);
369 BeginTag("ref", "type", type, "addr", saddr);
372 void EndReference(void)
377 void LiteralBool(bool value)
380 WriteF("%u", value ? 0 : 1);
384 void LiteralSInt(signed long long value)
387 WriteF("%lli", value);
391 void LiteralUInt(unsigned long long value)
394 WriteF("%llu", value);
398 void LiteralFloat(double value)
405 void LiteralString(const char *str)
416 void LiteralWString(const wchar_t *str)
427 void LiteralNamedConstant(const char *str)
434 void LiteralOpaque(const void *addr)
441 snprintf(saddr, sizeof(saddr), "%p", addr);
442 BeginTag("opaque", "addr", saddr);
446 void LiteralNull(void)
451 } /* namespace Log */